How To Build Working CentOS Server with Nginx and PHP

Friday, August 19th 2016. | how to

#howto ,#tutorial #VPS – How To Build Working CentOS Server with Nginx and PHP – This tutorial explains step by step guide (with pics) about How To Build Working CentOS Server with Nginx and PHP

How To Install and Setup Nginx on CentOS Server – Are you currently using Apache serving your CentOS server but you feel it so slow and sluggish? Then you have to ditch Apache and switch to Nginx. As a refresh for you who don’t really know. Nginx is a free lightweight web server with excellent performance even for heaviest sites. Its performance is more than twice what Apache is capable of. Nginx is pretty good to handle static files. So far its only best rival is LiteSpeed that’s not free. Nginx is pronounced as Engine X. In this page you’ll see step by step tutorial how to build a CentOS server with Nginx, PHP5 and MySQL or simply called LNMP aka LEMP stack.

Prerequisite

  1. A working CentOS server. In this guide I am using CentOS 6.3 x32 (see my playground server)
  2. Windows users should download Putty while Mac and Linux users can simply use Terminal
  3. You’ll also need a basic skill to use Putty and to navigate through SSH. Read: Most common Unix commands used to SSH.
  4. LNMP Stack (Linux, Nginx, PHP, MySQL): CentOS itself is a Linux so what you’ll need are NginX PHP and MySQL.
  5. About 15 minutes of your time and a cup of coffee if you like.

How to install NginX on CentOS

Step 1 – Login to your server and follow my previous guide about Basic setup for CentOS before you build a live web server. You may and may not follow that tutorial but if you followed, it will give you some basic security tweak to your server.

Before you proceed to the next steps, it is better to explain that all commands in this tutorial are written without the “sudo” prefix. However if you disabled root login and you logged in using another username with root privilege, you can add the “sudo” prefix all by your self. Alternatively you can simply type su, hit Enter and type in your password twice to switch login as root.

switch-root-login

You may also need to type this command to go to the root directory:

cd ~

Step 2 – Add two important extra repository to download Nginx and PHP-fpm. FYI: All CentOS versions under 6.4 do not come with Nginx and php-fpm. Hence you have to add another repo by Issuing following command syntax:

again, if your server is running CentOS 6.4, you don’t have to follow this step (adding extra repo).

rpm --import https://fedoraproject.org/static/0608B895.txt
rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

also this one:

rpm --import http://rpms.famillecollet.com/RPM-GPG-KEY-remi
rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

screenshot:

add repo url

and

add repo url

now install Yum Priorities plugin:

yum install yum-priorities

Answer Y if asked to confirm.

install yum priorities plugin

now edit epel.repo file. You can use either vi or Nano, in this example I will always use Nano editor.

nano /etc/yum.repos.d/epel.repo

Right in [epel] section below the “Enabled=1” add following line:

priority=10

It looks like this:

add priority

Hit Control+O to save then Control+X to exit. Next, also edit remi.repo file:

nano /etc/yum.repos.d/remi.repo

Change “Enabled=0” to “Enabled=1” then add following line under that line:

priority=10

So it looks like this:

edit remi repo

Once done, hit Control+O to save then Control+X to exit.

Step 3 – Next, issue the main command syntax to install Nginx on CentOS via yum:

yum install nginx

screenshot:

yum install nginx

It will then ask you to confirm the install process, simply type Y and hit Enter.

install nginx on centos

Once done you’ll see the “Complete!” message.

How to Install PHP5 on CentOS

Step 1 – To make your server capable to process any php files, you have to also install PHP5 with php-fpm. For your information, PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites. PHP5 also has many other modules can be installed along with php-fpm. Read my previous guide if you want to check all available modules and how to install it. Below is the command to install PHP5 with php-fpm and common modules.

yum install php-fpm php-cli php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-magickwand php-magpierss php-mbstring php-mcrypt php-mssql php-shout php-snmp php-soap php-tidy

if asked to confirm, simply hit Y.

yum install php5 php-fpm

Step 2 – Now edit php.ini file and find “cgi.fix_pathinfo” section then set it to 0.

nano /etc/php.ini

It should look like this:

cgi.fix_path

Step 3 – Also find the [date] section and set your server’s timezone. You can check it at php.net site:

set timezone

Once done editing php.ini file, hit Control+O to save then Control+X to exit.

You can check the installed php version by using this command:

php -v

check php version

In this example is PHP v5.4.13

How to configure Nginx

Step 1 – Default Nginx configuration file in CentOS is located at /etc/nginx/ so you can edit it using this command:

nano /etc/nginx/nginx.conf

That configuration file is default config file of Nginx but each site you host should has its own ,.conf file.. What you can edit to adjust its value are:

  • worker_processes: Change 1 to a reasonable value to increase its performance. But make sure your server has great hardware.
  • keepalive_timeout: Change its default value to your the value you want.

You can find full example of nginx.conf configuration here.

Step 2 – Edit Nginx default virtual hosts. Use eithet nano or vi:

nano /etc/nginx/conf.d/default.conf

The add “index.php” within the index line:

add index.php

You may also change “server_name _;” to your actual domain name or IP address

server {
       listen       80;
       server_name domain.com;

This is optional but you better do this: Find out the section that says “# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000” and uncomment (remove the # symbol) in every lines of the config so it will look like following:

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
  location ~ .php$ {
      root           /usr/share/nginx/html;
      try_files $uri =404;
      fastcgi_pass   127.0.0.1:9000;
      fastcgi_index  index.php;
      fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
      include        fastcgi_params;
  }

screenshot:

pass to php fpm

Once done, hit Control+O to save then Control+X to exit.

Configure PHP-FPM

Step 1 – Edit www.conf file that holds php-fpm configuration. You can use vi or Nano to edit it:

nano /etc/php-fpm.d/www.conf

Step 2 – Now find the section called “Unix user/group of processes” then change Apache to Nginx:

user group nginx

Last steps

Step 1 – Test Nginx configuration file:

nginx -t

It should give you the OK message:

test nginx

Step 2 – So you can now start Nginx service using one of these commands:

service nginx start

or,..

/etc/init.d/nginx start

It should also give you with OK message:

start nginx

Step 3 – Make Nginx to run automatically each time your server boots or reboots. Issue this command:

chkconfig --levels 235 nginx on

Step 4 – Now start the PHP-FPM service using one of these commands:

/etc/init.d/php-fpm start

or,..

service php-fpm start

It should also give you with a green OK message:

start php-fpm

Step 5 – Issue this Unix command to make PHP-FPM service runs automatically every time your server boots or reboots:

chkconfig --levels 235 php-fpm on

What’s Next?

Of course you can now give it a test. Open up your favorite web browser and access your server from it. You can simply type the IP address or domain name. How to make your domain resolves to your server? There are two ways:

  1. Locally – Simply edit your computer’s hosts file.
  2. Edit DNS records of your domain.

If everything goes find, you should see something like mine below:

Nginx test page

So you now have Linux, Nginx and PHP already but is there something missing? Yup, it’s MySQL. I’ve posted separate guide about how to install MySQL server on CentOS here.

That’s it. Enjoy installing Nginx on your CentOS VPS or Dedicated server. Leave your comment below to ask something regarding this topic. Do not also forget to follow me on Twitter for faster update.

This post How To Build Working CentOS Server with Nginx and PHP is part of ServerMom.

Source link