Install Nginx MariaDB PHP-FPM on CentOS 6

How to install Nginx web server, MariaDB and PHP-FPM

To avoid any conflict once NGINX service start please make sure Apache service already removed (if available). We can remove Apache service as follow
Shutdown Apache service (if running)

#service httpd stop

Remove Apache from boot cycle, so that it does not start up again on boot

#chkconfig httpd off

Remove Apache package

#yum remove httpd

UPDATE EPEL REPOSITORY

NGINX is available as a package for CentOS 6 from epel repository but first we should update epel repository, which we can install as follows:

#yum install epel-release

Install Nginx using yum

#yum install nginx

To get NGINX running type:

#/etc/init.d/nginx start

We can check that nginx has installed by open a browser and type http://ipaddressNGINX-success-installed
Configure the server to start NGINX on server boot

#chkconfig nginx on

Install MariaDB

Install MariaDB from a repository using yum, before install make sure CentOS packages is up-to-date

#yum -y update

Then add the MariaDB repository

#vi /etc/yum.repos.d/MariaDB.repo

Insert this custom MariaDB 10.1 Stable YUM repository for CentOS 6 (64 Bit).

# MariaDB 10.1 CentOS repository list - created 2016-07-03 19:01 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos6-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

*** For complete custom MariaDB repository with linux system version please visit https://downloads.mariadb.org/mariadb/repositories/

Install MariaDB with yum

#yum install MariaDB-server MariaDB-client -y

Start MariaDB and set to start on every boot.

#service mysql start
#chkconfig mysql on

Secure MariaDB, secure your MariaDB installation with the following checklist
Set (Change) root password
Remove anonymous users
Disallow root login remotely.
Remove test database and access to it.
Reload privilege tables.
Run the secure installation command.

#mysql_secure_installation
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y
New password: ENTER YOUR PASSWORD
Re-enter new password: REPEAT YOUR PASSWORD
Password updated successfully!
Reloading privilege tables..
... Success!
Remove anonymous users? [Y/n] y
... Success!
Disallow root login remotely? [Y/n] y
... Success!
Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reload privilege tables now? [Y/n] y
... Success!
Cleaning up...
Thanks for using MariaDB!

Restart MariaDB.

#service mysql restart

INSTALL PHP-FPM
The php-fpm package is located within the REMI repository, which, at this point, is disabled. The first thing we need to do is enable the REMI repository and install php and php-fpm:

#wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
#rpm -Uvh remi-release-6.rpm

Enable REMI repository by edit file remi.repo.
Find the line enabled =0 and change all of it to 1 to enable REMI repository.

#vi /etc/yum.repos.d/remi.repo
[...]
enabled=1
[...]

Then we can install php and php-fpm as follows:

#yum install php-fpm php-mysql

Set the php-fpm processes to run automatically when server boots

#service php-fpm start
#chkconfig php-fpm on

CONFIGURE PHP
We need to make one small change in the php.ini configuration.
Find the line, cgi.fix_pathinfo=1, and change the 1 to 0

#vi /etc/php.ini
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.

STEP SIX: CONFIGURE NGINX
Open up the default nginx config file and raise the number of worker processes to 4 then save and exit that file.

#vi /etc/nginx/nginx.conf
[...]
worker_processes  4;
[...]

Nginx already comes with a default configuration to use php fpm. Open the /etc/nginx/conf.d/default.conf file and look following lines.

#vi /etc/nginx/conf.d/default.conf
# 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;
    }
}

Here 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

Open up the php-fpm configuration, replace the apache in the user and group with nginx:

#vi /etc/php-fpm.d/www.conf
; 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

Finish by restarting php-fpm.

#service php-fpm restart

CHECK RESULTS:
Create a php info page and add in the following line:

#vi /usr/share/nginx/html/info.php
<?php
phpinfo();
?>

Then Save and Exit.
Restart nginx so that all of the changes take effect:

#service nginx restart

We can check that NGINX MySQL PHP has installed successfully by open a browser and type http://ipaddress/info.php

Set Up nginx, mysqld, php-fpm Autostart on reboot

#chkconfig --levels 235 mysql on
#chkconfig --levels 235 nginx on
#chkconfig --levels 235 php-fpm on

How to install Linux Httpd web server, MariaDB and PHP on CentOS 6

Leave a Reply

Your email address will not be published. Required fields are marked *