How to setup Nginx, My SQL, PHP enabled on Centos 6.7

How to setup Nginx, My SQL, PHP enabled on Centos 6.7

1. Update the yum repository as the Nginx is not directly available from Centos

# yum install epel-release

2. Install MySQL with dependencies

# yum install mysql-server

3. Once the MySQL installation is completed start the MySQL services.

# service mysqld start

or

# sudo /etc/init.d/mysqld restart

4. We can secure MySQL by running this command, we can alse setup root password for the MySQL

# /usr/bin/mysql_secure_installation

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Since we have installed the MySQL just now we dont have the password for root, Just press Enter key

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] y                                           
 ... 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] y
... 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] y
 - 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] y
 ... 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!

5. Make the MySQL start by it self on time of booting

# chkconfig mysqld on


6. Install the Nginx and dependencies using this command

# yum install nginx

7. Once the nginx is installed it wont start itself start the Nginx server

# service nginx start

8. Making Nginx start by it self at the time of boot

# chkconfig nginx on

9. We can check the working status of nginx by pointing the browser to our own server IP

# ifconfig eth0 | grep inet | awk '{ print $2 }'

Open any browser and check https://your-ip-address

10. Installing PHP, the php-fpm package is localted in REMi repositoty.

# yum install php-fpm php-mysql

11. Configure the PHP

# vi /etc/php/ini

find the line cgi.fix_pathinfo=1 and change the 1 to 0

cgi.fix_pathinfo=0

If this number is kept as a 1, the php interpreter will do its best to process the file that is as near to the requested file as possible. This is a possible security risk. If this number is set to 0, conversely, the interpreter will only process the exact file path—a much safer alternative. Save and Exit.


12. configure the nginx

# vi /etc/nginx/ngin.conf

Raise the number fo worker process to 4 the save, exit the file.

now we have to configure the nginx virtual hosts, in order to make the default nginx file more concise, the virtual hosts details are in the different location

# vi /etc/nginx/conf.d/default.conf

The sample config files looks like this

#
# The default server
#
server {
    listen       80;
    server_name example.com;

  
    location / {
        root   /usr/share/nginx/html;
        index index.php  index.html index.htm;
    }

    error_page  404              /404.html;
    location = /404.html {
        root   /usr/share/nginx/html;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}




These are the details of the changes:

Add index.php within the index line.
Change the server_name to your domain name or IP address (replace the example.com in the configuration)
Change the root to /usr/share/nginx/html;
Uncomment the section beginning with "location ~ \.php$ {",
Change the root to access the actual document root, /usr/share/nginx/html;
Change the fastcgi_param line to help the PHP interpreter find the PHP script that we stored in the document root home.
Save and Exit


13. Open the /etc/php-fpm.d/www/conf

# sudo vi /etc/php-fpm.d/www.conf

Replace the apache in the user and group with nginx

[...]
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;               will be used.
; RPM: apache Choosed to be able to access some dir as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx
[...]

14. Finish by restarting php-fpm.

# sudo service php-fpm restart

# chkconfig php-fpm on

Checking the Nginx and PHP  configuration

15. To check the configuration create file /usr/share/nginx/html/info.php

# sudo vi /usr/share/nginx/html/info.php

Add in the following lines in the info.php file:



<?php
phpinfo();
?>

Then Save and Exit.

Restart nginx so that all of the changes take effect:

# sudo service nginx restart





Keywords, LAMP, Nginx, SQL, PHP, PHP-mysql, Webserver, Nginx Webserver



Comments

Popular posts from this blog

Observium: Configuring Microsoft Windows 2008 Server SNMP Agent

AWS: Introduction to AWS (Amazon Web Services)

System/Server Health Status using PowerShell