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

by
Share this Article: Facebook22Google+0Twitter20LinkedIn3Reddit0StumbleUpon1

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 “server.unixmen.com” and “192.168.1.200″.

Install EPEL Repository

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

[root@server ~]# wget http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm [root@server ~]# rpm -ivh epel-release-6-8.noarch.rpm

Install MySQL

[root@server ~]# yum install mysql mysql-server -y

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

[root@server ~]# /etc/init.d/mysqld start 
[root@server ~]# chkconfig mysqld on

Set MySQL root password.

[root@server ~]# /usr/bin/mysql_secure_installation 

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
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

[root@server ~]# yum install nginx -y

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

[root@server ~]# /etc/init.d/nginx start 
Starting nginx: [ OK ] 
[root@server ~]# chkconfig nginx on

Stop Apache or any other web servers if you have.

[root@server ~]# /etc/init.d/httpd stop 
[root@server ~]# chkconfig httpd off

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

[root@server ~]# 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

[root@server ~]# /etc/init.d/iptables restart

Now point your web browser with “http://192.168.1.200″. The test page of nginx will open.

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

Install PHP

[root@server ~]# yum install php php-common php-fpm -y

Start php-fpm service.

[root@server ~]# /etc/init.d/php-fpm start 
[root@server ~]# chkconfig php-fpm on

Test PHP

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

[root@server ~]# vi /usr/share/nginx/html/testphp.php 
<?php 
phpinfo(); 
?>

Now open the testphp.php file in browser using http://192.168.1.200/testphp.php. 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′.

[root@server ~]# 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.

[root@server ~]# vi /etc/nginx/conf.d/default.conf
server { 
     listen 192.168.1.200:80; 
     server_name server.unixmen.com

Save and close the file. Restart nginx service now.

[root@server ~]# /etc/init.d/nginx restart

Thats it your nginx web server is ready to use.

For questions please refer to our Q/A forum at : http://ask.unixmen.com/

Share this Article: Facebook0Google+0Twitter20LinkedIn3Reddit0StumbleUpon1