Install nginx with PHP and MySQL in CentOS 6.4 / RHEL 6.4

nginx (pronounced as engine-x) is a lightweight, high performance http server, reverse proxy server, IMAP, SMTP and POP3 server. In this tutorial my test box hostname and ip address are “” and “”.

Install EPEL Repository

nginx will not be found in Official CentOS repository, so lets install EPEL repository first.

[[email protected] ~]# wget [[email protected] ~]# rpm -ivh epel-release-6-8.noarch.rpm

Install MySQL

[[email protected] ~]# yum install mysql mysql-server -y

Start the MySQL service and make to start automatically on every reboot.

[[email protected] ~]# /etc/init.d/mysqld start 
[[email protected] ~]# chkconfig mysqld on

Set MySQL root password.

[[email protected] ~]# /usr/bin/mysql_secure_installation 

In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):     ## Press Enter ## 
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n]     ## Press Enter ##
New password:                ## Enter new password ##
Re-enter new password:       ## Re-enter new password ##
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n]     ## Press Enter ##
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n]     ## Press Enter ## 
... Success!
By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n]     ## Press Enter ##
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n]     ## Press Enter ##
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!

Install nginx

[[email protected] ~]# yum install nginx -y

Start nginx and make it to start automatically on every reboot.

[[email protected] ~]# /etc/init.d/nginx start 
Starting nginx: [ OK ] 
[[email protected] ~]# chkconfig nginx on

Stop Apache or any other web servers if you have.

[[email protected] ~]# /etc/init.d/httpd stop 
[[email protected] ~]# chkconfig httpd off

Open the nginx port in your firewall/router if you want to access the web server from other systems.

[[email protected] ~]# vi /etc/sysconfig/iptables 
-A INPUT -p udp -m state --state NEW --dport 80 -j ACCEPT 
-A INPUT -p tcp -m state --state NEW --dport 80 -j ACCEPT

Restart iptables

[[email protected] ~]# /etc/init.d/iptables restart

Now point your web browser with “”. The test page of nginx will open.

Test Page for the Nginx HTTP Server on EPEL - Mozilla Firefox_001

Install PHP

[[email protected] ~]# yum install php php-common php-fpm -y

Start php-fpm service.

[[email protected] ~]# /etc/init.d/php-fpm start 
[[email protected] ~]# chkconfig php-fpm on

Test PHP

Create a sample “testphp.php” file in nginx document root folder and append the lines as shown below.

[[email protected] ~]# vi /usr/share/nginx/html/testphp.php 

Now open the testphp.php file in browser using It will display the details about php package.

phpinfo() - Mozilla Firefox_015

Configure nginx

Open nginx config file and set the worker_processes (i.e No of CPU’s in your system). To see the no of cpu, use the command “lscpu”. In my case its “1”. So i set this as ‘1’.

[[email protected] ~]# vi /etc/nginx/nginx.conf 
worker_processes 1;

Save and close the file and Edit the “/etc/nginx/conf.d/default.conf” as shown below.

[[email protected] ~]# vi /etc/nginx/conf.d/default.conf
server { 

Save and close the file. Restart nginx service now.

[[email protected] ~]# /etc/init.d/nginx restart

Thats it your nginx web server is ready to use.