Home > English > Tutorials > How to install LEMP ( NGINX, PHP-FPM, MARIADB) on  centos 6

How to install LEMP ( NGINX, PHP-FPM, MARIADB) on  centos 6

 

I. Preparation 

Install epel and remi repositories . Get the reference link here Click here

Turn off selinux , edit the file selinux

# nano /etc/sysconfig/selinux 

Edit SELINUX=disabled

# reboot to restart the server

Open the gateways 80 and 3306 iptables 

# nano /etc/sysconfig/iptables

And add 2 lines

-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

Restart iptables

# service iptables restart

II. Proceeding

Because nginx is not in yum repository default, you need to creat it

Visit the folder yum.repos.d. Creat the file nginx.repo

# nano /etc/yum.repos.d/nginx.repo

In the file nginx.repo creat this content 

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

Then install NGINX and PHP together with needed packages

# yum install nginx php-fpm php-mysql php-common php-mbstring php-mcrypt php-gd -y

# yum install php-fpm php-cli php-mysqlnd php-gd php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-soap -y

Some package names you may want to install

  • php-pecl-memcached – Memcached Server
  • php-pecl-memcache – extension chạy Memcached dành cho PHP
  • php-opcache – Zend Opcache
  • apcu – APC Opcode Cache

Start the nginx service

# service nginx start

Let nginx start together with the system 

# chkconfig nginx on

Start the php-fpm service

# service php-fpm start

Let php-fpm service start together with the system 

# chkconfig php-fpm on

You can test NGINX and PHP-FPM versions by commanding 

# nginx -v

# php-fpm -v

Then you visit your IP address (http://ip-vps) If this image appears you've succeeded

 

 nginx configuration

Change the configuration file of Nginx into default.conf

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

Search for this partta

# location ~ \.php$ {
#     root           html;
#     fastcgi_pass   127.0.0.1:9000;
#     fastcgi_index  index.php;
#     fastcgi_param  SCRIPT_FILENAME /scripts$fastcgi_script_name;
#     include        fastcgi_params;
# }

Remove the sign # and change the content as follows to allow nginx to work with php 
    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;
    }

PHP-FPM configuration

Defaulted PHP-FPM will have username and groupname being apache to run. You edit the file www.conf 

# nano /etc/php-fpm.d/www.conf

Search for the word apache
 

listen.owner = apache
listen.group = apache

user = apache
 ; RPM: Keep a group allowed to write in log dir.
 group = apache


Replace apache with nginx


listen.owner = nginx
listen.group = nginx
 user = nginx
; RPM: Keep a group allowed to write in log dir.
group =
nginx

Restart php-fpm

# service php-fpm restart

 Test Nginx and PHP-FPM configuration

To test whether both VirtualHost and PHP-FPM work correctly, creat a file called info.php and saved in the folder html :

# nano /usr/share/nginx/html/info.php

With this content

<?php
phpinfo();
?>

Then visit http://ip-vps/info.php , if the page PHP is as follows, you've succeeded

For security reason you should delete the file info.php after tesing it successfully to prevent access from other people 

# rm -f /usr/share/nginx/html/info.php

Add MariaDB repo

To install MariaDB, you need to add the following repository to your system by running these commands

# wget -O /etc/yum.repos.d/MariaDB.repo http://mariadb.if-not-true-then-false.com/centos/$(rpm -E %centos)/$(uname -i)/10

Then delete yum cache 

# yum clean all

update the system so that yum will update MariaDB package

# yum update -y

If you don't take 2 steps above, it will annouce error as follows

Transaction Check Error:

file /usr/share/mysql/ukrainian/errmsg.sys from install of MariaDB-server-10.0.25-1.el6.x86_64 conflicts with file from package mysql-libs-5.5.50-1.el6.remi.x86_64

Error Summary

Install or MariaDB

# yum install -y MariaDB MariaDB-server

Start MariaDB

# service mysql start

Let MariaDB start together with the system 

# chkconfig mysql on

Set a password for MariaDB root account:

# mysql_secure_installation

There will be these sentences

Sentence 1 : Enter current password for root (enter for none) : press enter

Sentence 2 : Set root password? [Y/n] select Y and enter your password 

Sentence 3 : Remove anonymous users? [Y/n] select Y

Sentence 4 : Disallow root login remotely? [Y/n] select Y 

Sentence 5 : Remove test database and access to it? [Y/n] select Y

Sentence 6 : Reload privilege tables now? [Y/n] select Y

To login mariaDB

# mysql -u root -p

Some commands of mariaDB

Display full databases:

MariaDB [(none)]> SHOW DATABASES;

Creat a database:

MariaDB [(none)]> CREATE DATABASE database name;

Use a database:

MariaDB [(none)]> USE database name;

Delete a database:

MariaDB [(none)]> DROP DATABASE database name;