Installing Nginx with PHP support

Comments Off on Installing Nginx with PHP support

This article will teach you “Installing Nginx with PHP support” in  CentOS 7/Redhat7  webserver. Nginx has grown in popularity since its release due to its light-weight resource utilization and its ability to scale easily on minimal hardware. Nginx excels at serving static content quickly and is designed to pass dynamic requests off to other software that is better suited for those purposes.

PHPFPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites.

The combination of Nginx  with PHP-FPM improves performance dramatically who love to program with PHP.

Step 1 – Prerequisites

We will be using the yum package manager to install all required software. Some of the required packages are not in the stock CentOS 7 repositories, so before we begin we may need to install the Extended Packages for Enterprise Linux (EPEL) repository. Install the EPEL using the following if it has nor been already installed:

# yum install epel-release

Step 2 – Install Nginx, PHP-FPM

Now that we have the EPEL repository installed, we can proceed to install all software that will be required. We can do this with the single command below:

# yum -y install nginx php-fpm  php-mysqlnd php-pear php-pdo php-mysql php-gd php-mbstring  php-xml  php-devel php-pear

Step 3 – Configure PHP-FPM
By default, PHP-FPM will be configured to run as the Apache user. Since we are using Nginx, we will need to modify the file /etc/php-fpm.d/www.conf and change this to the “nginx” user.  You can open the file in vi editor and change the value user = apache with user = nginx and group = apache with group = nginx.

This can be done quickly with a sed replacement as follows:

# sed -i ‘s/user = apache/user = nginx/’ /etc/php-fpm.d/www.conf
# sed -i ‘s/group = apache/group = nginx/’ /etc/php-fpm.d/www.conf

Now we can enable PHP-FPM to start on boot and then start the service:

# systemctl enable php-fpm
# systemctl start php-fpm

You can check that php-fpm is running issuing the following command

#systemctl status php-fpm
php-fpm.service - The PHP FastCGI Process Manager
Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled)
Active: active (running) since Tue 2015-10-27 14:03:35 BDT; 5s ago
Main PID: 4425 (php-fpm)
Status: "Ready to handle connections"
CGroup: /system.slice/php-fpm.service
php-fpm: master process (/etc/php-fpm.conf)
php-fpm: pool www
php-fpm: pool www
php-fpm: pool www
php-fpm: pool www
php-fpm: pool www

Oct 27 14:03:35 backupserver systemd[1]: Started The PHP FastCGI Process Manager.

Step 4 – Configure Nginx

Finally, we need to make a few adjustments to the Nginx configuration. The configuration lives inside /etc/nginx/ with the main server configuration being /etc/nginx/nginx.conf and server blocks (vhosts) residing in /etc/nginx/conf.d/. We will also be implementing a sites-available/sites-enabled configuration to make configuration cleaner.

Before we begin, lets go ahead and enable Nginx to start on boot and start it:

# systemctl enable nginx
# systemctl start nginx

You can check that nginx is running by issuing the command

#systemctl status nginx
nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled)
Active: active (running) since Tue 2015-10-27 14:10:34 BDT; 2s ago
Process: 4534 ExecStop=/bin/kill -s QUIT $MAINPID (code=exited, status=0/SUCCESS)
Process: 4543 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 4541 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Main PID: 4545 (nginx)
CGroup: /system.slice/nginx.service
 nginx: master process /usr/sbin/nginx
 nginx: worker process

Oct 27 14:10:34 backupserver nginx[4541]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Oct 27 14:10:34 backupserver nginx[4541]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Oct 27 14:10:34 backupserver systemd[1]: Started The nginx HTTP and reverse proxy server.

We will also need to allow Nginx through the firewall:

# firewall-cmd –permanent –zone=public –add-service=http
# firewall-cmd –reload

If you open a browser to your server’s name or IP, you should see a nice Nginx welcome page. Now we can proceed with configuring Nginx!

Troubleshooting Note: If everything above was setup correctly but Nginx returns a 403, you may need to disable SELinux. To do this, modify “/etc/sysconfig/selinux” and change “SELINUX=enforcing” to “SELINUX=disabled.” Once this change has been completed, you will need to reboot to apply it.

Now that Nginx is working, the first thing we need to do is create the sites-available/sites-enabled directories:

# mkdir /etc/nginx/sites-available
# mkdir /etc/nginx/sites-enabled

Now we need to modify the /etc/nginx/nginx.conf file to make use of these. In the main “http {” block, locate this conf include line

include /etc/nginx/conf.d/*.conf;

Directly below it, add this line:

include /etc/nginx/sites-enabled/*.conf;

Save and close the /etc/nginx/nginx.conf file. Now we need to create our first server block. Assuming that we will use www.reportportal.com as the domain, create and proceed to edit the file /etc/nginx/sites-available/reportportal.conf and add the following to it:

server {
listen 80; ## listen for ipv4; this line is default and implied
root /usr/share/nginx/reportportal.com;
index index.php index.html index.htm;

# Make site accessible from http://localhost/
server_name reportportal.com www.reportportal.com;

location / {
try_files $uri $uri/ /index.html;
}

location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}

error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/reportportal.com;
}

# pass the PHP scripts to PHP-FPM server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache’s document root
# concurs with nginx’s one
#
location ~ /\.ht {
deny all;
}
}

Make sure you change the “root” and “server_name” lines to match the actual document root and server name you wish to use. As instance if you want to make document root /home/user1/reportportal.com   then root and server_name would be root /home/user1/reportportal.com;

Once the file in sites-available has been configured, we will create a symlink to enable it:

# ln -s /etc/nginx/sites-available/reportportal.com.conf /etc/nginx/sites-enabled/reportportal.com.conf

Step 5 – Test Nginx

We should now create a quick PHPInfo script to make sure our Nginx installation is working as expected. To do this using our simplehelix.rocks example, we create a file in the document root called info.php (/usr/share/nginx/reportportal.com/info.php) and add the following contents:
<?php
phpinfo();
?> Now "Installing Nginx with PHP support" is completed and if we navigate to http://reportportal.com/info.php in a browser, we should see PHP Info output. Success! For security purposes, you should delete the info.php file once you are done with testing.

For details and advanced configuration, please  take a look at the official Nginx documentation to learn more about what can be accomplished with Nginx.