How to Install Nginx with PHP and MySQL (LEMP Stack) on CentOS 7.2 - Page 2

5 Installing PHP

We can make PHP 5 work  with Nginx through PHP-FPM (FastCGI Process Manager). PHP-FPM is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites. We can install php-fpm together with php-cli and some PHP5 modules like php-mysql which you need if you want to use MySQL from your PHP scripts as follows:

yum -y install php-fpm php-cli php-mysql php-gd php-ldap php-odbc php-pdo php-pecl-memcache php-pear php-mbstring php-xml php-xmlrpc php-mbstring php-snmp php-soap

APC is a free and open PHP opcode cacher for caching and optimizing PHP intermediate code. It's similar to other PHP opcode cachers, such as eAccelerator and Xcache. It is strongly recommended to have one of these installed to speed up your PHP page.

I will install APC from PHP pecl repository. PECL requires the Centos Development tools to be installed to compile the APC package.

yum -y install php-devel
yum -y groupinstall 'Development Tools'

and install APC:

pecl install apc

[[email protected] ~]# pecl install apc
downloading APC-3.1.13.tgz ...
Starting to download APC-3.1.13.tgz (171,591 bytes)
.................done: 171,591 bytes
55 source files, building
running: phpize
Configuring for:
PHP Api Version: 20100412
Zend Module Api No: 20100525
Zend Extension Api No: 220100525
Enable internal debugging in APC [no] : <-- ENTER
Enable per request file info about files used from the APC cache [no] : <-- ENTER
Enable spin locks (EXPERIMENTAL) [no] : <-- ENTER
Enable memory protection (EXPERIMENTAL) [no] : <-- ENTER
Enable pthread mutexes (default) [no] : <-- ENTER
Enable pthread read/write locks (EXPERIMENTAL) [yes] : <-- ENTER
building in /var/tmp/pear-build-rootVrjsuq/APC-3.1.13
......

Then open /etc/php.ini and set cgi.fix_pathinfo=0:

nano /etc/php.ini

[...]
; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI.  PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is.  For more information on PATH_INFO, see the cgi specs.  Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec.  A setting
; of zero causes PHP to behave as before.  Default is 1.  You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://www.php.net/manual/en/ini.core.php#ini.cgi.fix-pathinfo
cgi.fix_pathinfo=0
[...]

(Please read http://wiki.nginx.org/Pitfalls to find out why you should do this.)

and add the line:

[...]
extension=apc.so

at the end of the /etc/php.ini file.

In addition to that, in order to avoid timezone errors like

[28-June-2016 14:21:01] PHP Warning: phpinfo(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Europe/Berlin' for 'CEST/2.0/DST' instead in /usr/share/nginx/html/info.php on line 2

... in /var/log/php-fpm/www-error.log when you call a PHP script in your browser, you should set date.timezone in /etc/php.ini:

[...]
[Date]
; Defines the default timezone used by the date functions
; http://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone
date.timezone = "Europe/Berlin"
[...]

You can find out the correct timezone for your system by running:

cat /etc/sysconfig/clock

[[email protected] nginx]# cat /etc/sysconfig/clock
ZONE="Europe/Berlin"
[[email protected] nginx]#

Next, create the system startup links for php-fpm and start it:

systemctl enable php-fpm.service
systemctl start php-fpm.service

PHP-FPM is a daemon process (with the init script /etc/init.d/php-fpm) that runs a FastCGI server on port 9000.

 

6 Configuring Nginx

The nginx configuration is in /etc/nginx/nginx.conf which we open now:

nano /etc/nginx/nginx.conf

First (this is optional) you can increase the number of worker processes and set the keepalive_timeout to a reasonable value:

[...]
worker_processes  4;
[...]
    keepalive_timeout  2;
[...]

The virtual hosts are defined in server {} containers in the /etc/nginx/conf.d directory. Let's modify the default vhost (in /etc/nginx/conf.d/default.conf) as follows:

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

[...]
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm index.php;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # 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;
    }
	
	# deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny  all;
    }
}

server_name _; makes this a default catchall vhost (of course, you can as well specify a hostname here like www.example.com).

In the location / part, I've added index.php to the index line. root /usr/share/nginx/html; means that the document root is the directory /usr/share/nginx/html.

The important part for PHP is the location ~ \.php$ {} stanza. Uncomment it to enable it. Change the root line to the web site's document root (e.g. root /usr/share/nginx/html;). Please note that I've added the line try_files $uri =404; to prevent zero-day exploits (see http://wiki.nginx.org/Pitfalls#Passing_Uncontrolled_Requests_to_PHP and http://forum.nginx.org/read.php?2,88845,page=3). Please make sure that you change the fastcgi_param line to fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; because otherwise, the PHP interpreter won't find the PHP script that you call in your browser ($document_root translates to /usr/share/nginx/html because that's what we have set as our document root).

PHP-FPM is listening on port 9000 on 127.0.0.1 by default, therefore we tell Nginx to connect to 127.0.0.1:9000 with the line fastcgi_pass 127.0.0.1:9000;. It is also possible to make PHP-FPM use a Unix socket - I will describe this in chapter 7.

Now save the file and reload Nginx:

systemctl restart nginx.service

Now create the following PHP file in the document root /usr/share/nginx/html...

nano /usr/share/nginx/html/info.php

<?php
phpinfo();
?>

Now we call that file in a browser (e.g. http://192.168.1.100/info.php):

The PHP info from our nginx server.

As you see, PHP 5 is working, and it's working through FPM/FastCGI, as shown in the Server API line. If you scroll further down, you will see all modules that are already enabled in PHP5, including the MySQL module:

MySQL driver has been activated in PHP.

 

7 Making PHP-FPM use a Unix Socket

By default, PHP-FPM is listening on port 9000 on 127.0.0.1. It is also possible to make PHP-FPM use a Unix socket which avoids the TCP overhead. To do this, open /etc/php-fpm.d/www.conf...

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

... and make the listen line look as follows:

[...]
;listen = 127.0.0.1:9000
listen = /var/run/php-fpm/php5-fpm.sock
[...]

Then reload PHP-FPM:

systemctl restart php-fpm.service

Next, go through your Nginx configuration and all your vhosts and change the line fastcgi_pass 127.0.0.1:9000; to fastcgi_pass unix:/tmp/php5-fpm.sock;, e.g. like this:

vi /etc/nginx/conf.d/default.conf

[...]
    location ~ \.php$ {
        root           /usr/share/nginx/html;
        try_files $uri =404;
        fastcgi_pass   unix:/var/run/php-fpm/php5-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
[...]

Finally, reload Nginx:

systemctl restart nginx.service

 

5 Download this CentOS 7.2 server as virtual machine

This setup is available as virtual machine download in ova/ovf format (compatible with VMWare and Virtualbox) for howtoforge subscribers.

Login details for the VM

  • The root password is: howtoforge
  • The password of the "administrator" user is: howtoforge

Please change both passwords on the first login.

  • The IP address of the VM is 192.168.1.100

Share this page:

0 Comment(s)

Add comment

Comments

From: howtoforge at: Jun 28, 2016