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
- A working CentOS server. In this guide I am using CentOS 6.3 x32 (see my playground server)
- Windows users should download Putty while Mac and Linux users can simply use Terminal
- You’ll also need a basic skill to use Putty and to navigate through SSH. Read: Most common Unix commands used to SSH.
- LNMP Stack (Linux, Nginx, PHP, MySQL): CentOS itself is a Linux so what you’ll need are NginX PHP and MySQL.
- 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.
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:
and
now install Yum Priorities plugin:
yum install yum-priorities
Answer Y if asked to confirm.
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:
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:
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:
It will then ask you to confirm the install process, simply type Y and hit Enter.
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.
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:
Step 3 – Also find the [date] section and set your server’s timezone. You can check it at php.net site:
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
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:
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:
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:
Last steps
Step 1 – Test Nginx configuration file:
nginx -t
It should give you the OK message:
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:
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:
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:
- Locally – Simply edit your computer’s hosts file.
- Edit DNS records of your domain.
If everything goes find, you should see something like mine below:
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.
I can recommend.
Thanks for your guides.
I’ve just followed this one, but am finding that when I try to browse to a PHP file Nginx is sending ‘No input file specified.’ to the browser.
I suspect PHP-FPM is running, but not executing my file (index.php in my case).
Is this a problem in my nginx default config file?
Try investigating from nginx config file.
I have got same problem. After installing nginx on my CentOS 6.4 when I put my VPS ip on browser I don’t get any response. Don’t know what’s wrong 🙁
Try to restart nginx. It should return with [OK] message. If not, well, definitely something’s wrong
I tried for days on the problem the people posted before me mentioned.
I found the solution to actually be on this page but not referred top.
In How to configure Nginx – step 2
there is a difference between the screen shot posted and the copy and pasted information.
This is the corrext configuration which is the copy and pasted information NOT the screenshot.
# 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;
The default, and the screenshot do not work for me. You need to change the line to read this….
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
and now everything should be fine.
when i start nginx with command “service nginx start”, i meet this error
“[root@vps ~]# service nginx start
Starting nginx: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
”
Before i install apache, may be conflict port but i don’t know solve that. i am a newer to VPS.
Thank for your post
you should at least stop apache before running nginx. Or, you may also want to remove it
I am having the same issue. I have removed apache ‘yum erase apache’ and I still get this issue. What do I do?
Hi nice article….For people who are facing difficulties accessing the webserver with Centos 6.* Try disabling SELINUX also stop iptables service and then check if firewall is blocking the requests or not. Once the solution is found you can change the rules as per requirements.
Regarding error with server already in use…edith the hosts file and point hostname of your machine to ip address. just like 127.0.0.1 localhost should be somthing like 192.168.1.1 myhost myhost.mydomain myhost4 myhost.mydomain4
I hope this will solve the problems.
Thanks
Sal
http://www.itpings.com
Hmm, this is a great tutorial, but what do you do when you cant to add new sites and subdomains?
hey please let me know that can Nginx is able to reduce load on server
I follow step by step and it is working great.
THANKS!
A very useful article. Thanks
Hi Sawiyati, how are you doing?
I get Plesk+Centos. In my ‘service’ panel I see nginx activated, I mean green check.
Even I have installed PHP-FPM services, I cannot activate the service in the Plesk panel. I can see it PHP-FPM 5.5.38, but the play button it´s gray, and the message say ‘the service it´s not configured’
This is the reason, I belive my server takes a lot ‘load average’. I see a lot ‘php-cgi’ commands but not ´php-fpm’
Thanks a lot
felix