Comparing apache and nginx, both have its strength and weakness. Though apache still has higher market proportion, but it is undeniable that nginx is catching up and has the trend of increasing in market proportion from year to year. Therefore, let’s get to know more about nginx and how to use it.
Php-fpm, an alternative PHP FastCGI service, which is used by nginx to pass php web page requests to php. Nginx itself is unable to interpret php language written web pages, so when requests are sent from client to nginx, nginx send those requests to php with the help of php-fpm. Then results are sent back to user with the help of php-fpm too through nginx from php after php is done interpreting.
So in this post, we will do
- Install epel, update the system.
- Install nginx repository, install and start nginx.
- Setup firewalld so nginx is accessible.
- Install, setup and start php-fpm
- Modify nginx to use php-fpm, restart service and check final output.
Install epel and update system
Install epel package.
yum install http://mirror01.idc.hinet.net/EPEL/7/x86_64/Packages/e/epel-release-7-11.noarch.rpm yum update -y
Install nginx repository, install and start nginx
First we check the version of nginx package provided by the official repository of CentOS and is
1.12.2-2.el7, which is not too old compared to the nginx official release version. I prefer to use the newest packages so I will start with adding nginx’s official repository and do the rest of things.
Add a new file inside
/etc/yum.repos.d/ directory, you can name the file anyway you like but .repo extension must be used. Copy and paste the lines below into the file you created, save and exit. Now check again the version of nginx package and will see that the newest version is
[nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=0 enabled=1
yum install nginx -y
Now let’s start nginx. For more information about systemctl, please refer:
systemctl start nginx systemctl status nginx
Allow http access to firewall or stop firewall
Stop firewall or allow http access to your server so we can test out nginx.
For more information about firewall-cmd, please refer:
# To allow http access firewall-cmd --add-service=http --permanent firewall-cmd --reload firewall-cmd --list-all # To stop firewall service systemctl stop firewalld
Now, browse to your server’s ip and you will see nginx running!
Php-fpm installation and configuration
If we want to run web pages written in php, we must have php package installed. Please install the version of php that you need.
yum install php
yum install php-fpm
Start php-fpm service and check status.
systemctl start php-fpm systemctl status php-fpm
Create a file named
index.php with the content below at
/usr/share/nginx/html/, which is the default www directory of nginx for php-fpm testing later on.
<?php phpinfo(); ?>
Modify nginx to work with php-fpm
As mentioned before, nginx will pass php web pages request to php through the help of php-fpm, so let’s modify the content of
- Add index.php at line 10
- Unmark line 30 ~ 36
- Change line 31 to root /usr/share/nginx/html;
- Change line 34 to $document_root$fastcgi_script_name;
- Save, exit and restart nginx.
systemctl restart nginx
And finally, if you see the php infomation page, that means you succeed and we are done for this post!