Install NGINX, PHP and MySQL – CentOS

Installing MySQL

# yum install mysql-server
# chkconfig mysqld on
# service mysqld start

Running secure installation script

# /usr/bin/mysql_secure_installation

This will give you the option to remove the test databases and anonymous user created by default, and also to create the root password. This is strongly recommended for production servers.


Installing NGINX

nginx.repo

To add NGINX yum repository, create a file named /etc/yum.repos.d/nginx.repo and paste the configurations below:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

After that we can install NGINX with yum like so:

yum install nginx
chkconfig nginx on
service nginx start

Type in your web server’s IP address or hostname into a browser, and you should see the message “Welcome to nginx!”.

The default nginx document root on CentOS is /usr/share/nginx/html.


Installing PHP

To make PHP work in NGINX we need to also install PHP-FPM (PHP FastCGI Process Manager).

yum install php php-fpm php-mysql php-common php-gd php-mbstring php-mcrypt php-devel php-xml

Edit the file “/etc/php.ini” and set “cgi.fix_pathinfo=0” like so:

; 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

Start PHP-FPM:

/etc/init.d/php-fpm start

Autostart PHP-FPM on boot:

chkconfig php-fpm on

Configuring NGINX

Edit the file /etc/nginx/nginx.conf and adjust the number of worker_processes to match your system CPU.

You can run following command to list the number of CPU on your system:

grep processor /proc/cpuinfo | wc -l

The default vhost is defined in the file /etc/nginx/conf.d/default.conf. You can edit that file or just copy it over to create another vhost. But I like to create my vhost like so:

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

Now edit the file “/etc/nginx/nginx.conf” and add the following code after the line “include /etc/nginx/conf.d/*.conf“.

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

Configure virtual host

Create a file called “/etc/nginx/sites-available/your.domain.com” with the following config. This is just a very basic config to get you up and running.

server {                                                                           
    listen 80;                                                                   
    server_name your.domain.com;                                             
    access_log /var/www/your.domain.com/logs/access.log;                     
    error_log /var/www/your.domain.com/logs/error.log;                       
    root /var/www/your.domain.com;                                           
 
    location / {                                                                   
        index index.php index.html;                                                
    }                                                                              
 
    location ~ \.php$ {                                                            
        include /etc/nginx/fastcgi_params;                                         
        fastcgi_pass  127.0.0.1:9000;                                              
        fastcgi_index index.php;                                                   
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;          
    }                                                                              
}
  • root /var/www/your.domain.com – is the $document_root

Link the virtual host to “/etc/nginx/sites-enabled“:

cd /etc/nginx/sites-enabled
ln -s /etc/nginx/sites-available/your.domain.com

Reload NGINX:

/etc/init.d/nginx reload

Now create a php file “/path/to/document/root/info.php” with this code:

<?php
    phpinfo();
?>

Point your browser to: http://your.server.ip/info.php

You should see the PHP information page and modules that are installed.