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


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

name=nginx repo

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.                               

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/” with the following config. This is just a very basic config to get you up and running.

server {                                                                           
    listen 80;                                                                   
    access_log /var/www/;                     
    error_log /var/www/;                       
    root /var/www/;                                           
    location / {                                                                   
        index index.php index.html;                                                
    location ~ \.php$ {                                                            
        include /etc/nginx/fastcgi_params;                                         
        fastcgi_index index.php;                                                   
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;          
  • root /var/www/ – is the $document_root

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

cd /etc/nginx/sites-enabled
ln -s /etc/nginx/sites-available/

Reload NGINX:

/etc/init.d/nginx reload

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


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

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