Networking HowTos
Networking HowTos

Installing NGINX, PHP, and MySQL on CentOS 6

January 23, 2013 CentOS, Linux

This “how to” guide will step you through the process of setting up Nginx, PHP, and MySQL on CentOS 6.

Nginx (pronounced Engine-X) is a fast & lightweight HTTP and HTTPS web server (it can also act as a reverse proxy, and perform load balancing).
Its small memory footprint requirements make it great for systems with small amounts of memory, such as low end cloud servers. Nginx is great for serving static files to users, and is cable of handling more than 10,000 simultaneous connections, but it lacks the embedded module support for PHP as Apache does.
Nginx can’t process PHP files directly, so Nginx essentially offloads processing of .php files to the PHP FPM package, which in turn passes the interpreted script information back to Nginx, to return back to the end user.
This guide requires the EPEL and REMI repositories to be configured.
Install the EPEL repostitory

# yum -y install

Install the REMI repository:
Select between installing the 64bit package or the 32bit package depending on what version of CentOS you installed.

# yum -y install


# yum -y install

Make sure your system is up to date:

# yum update

Install Nginx:
Install Nginx using the following command:

# yum -y install nginx

Set Nginx to start on boot:

# chkconfig --level 2345 nginx on

Start Nginx:

# service nginx start

Configure Nginx to Pass PHP files to PHP-FPM:
Edit the /etc/nginx/conf.d/default.conf file:
# vi /etc/nginx/conf.d/default.conf
Locate the following section in the site config file:

    # pass the PHP scripts to FastCGI server listening on
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;

Modify this configuration section to read as follows:

    # pass the PHP scripts to FastCGI server listening on
    location ~ \.php$ {
        root           html;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_index  index.php;
        try_files $uri =403;
        fastcgi_param  SCRIPT_FILENAME $request_filename;
        include        fastcgi_params;

Note: There are a few changes from the original commented lines, so make sure the config reads as above.
Save the file and exit the editor.
Reload the Nginx configuration:

# sudo service nginx reload

Allow remote connections to your server on port 80:
A default install of CentOS will block incoming connections on port 80, so the above must be run.

# iptables -I INPUT 1 -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT

Save the iptables firewall config so that it remembers the settings across system boots:

# service iptables save

Install PHP:
To use PHP with Nginx, you require the PHP FPM package. To install PHP-FPM use the following command:

# yum -y --enablerepo=remi install php-fpm php-mysql php-pear-Net-Curl php-gd php-intl \
php-pear php-pecl-imagick php-imap php-mcrypt php-pecl-memcached php-ps php-pspell \
php-recode php-snmp php-sqlite php-tidy php-xmlrpc php-common php-xml

Note: You can modify the above command to add/remove individual PHP packages if needed. The php-fpm package is however required when used with Nginx.
Set PHP-FPM to start on boot:

# chkconfig --level 2345 php-fpm on

Start PHP-FPM:

# service php-fpm start

Install MySQL:
Install MySQL from the remi repository:

# yum --enablerepo=remi install mysql mysql-server

Set MySQL to start on boot:

# chkconfig --level 2345 mysqld on

Start MySQL:

# service mysqld start

Run through the “mysql_secure_installation” script to lock down the MySQL installation.

# mysql_secure_installation

The first option will allow you to set up the root password. Select “y” to set the password.
Keep note of the password you set for the root mysql user, and keep this safe.
You will generally want to answer the rest of the prompts with “y” too.
To test to make sure you have set it all up correctly, create a .php file containing the following PHP code and save it in your website folder and call it test.php (/usr/share/nginx/html/test.php in my example.):


Load up your site in a browser, and add the file /test.php to the url, and make sure it comes up with the usual PHP information page, and not just the source code for the .php file you created.
You should now have a working Nginx, PHP, and MySQL stack. Read the Nginx documentation for more detailed configuration options.
The Nginx documentation can be found on the Nginx website at

You Might Also Like