How to Install Linux, nginx, MySQL, PHP (LEMP) stack on CentOS 6

Introduction

 

LEMP is an open source software configuration used in web server and it stands for Linux, Nginx, MySQL, and PHP. This tutorial describes how to install LEMP stack on CentOS 6. The only difference between LAMP and LEMP stack is the web server in which the stack runs.

Since the server is already set up with CentOS, the Linux part of the stack is taken care of. This tutorial will show you how to install nginx, mysql and php-fpm, configure as well as verify them.
 

Installation of Required Repositories

 

Since nginx and php-fpm are not available from CentOS directly, we will have to download two repositories to our virtual private server. We will be using Yum for installation.

sudo rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

 

Installation of MySQL

 

Let’s start with MySQL installation process. It is used for storing, organizing and retrieving data.

Open your terminal and enter the following command.

sudo yum install mysql mysql-server

After the download, restart the MySQL:

sudo /etc/init.d/mysqld restart

Now, you can start running the secure installation processes. This will let you make some configuration of MySQL:

sudo /usr/bin/mysql_secure_installation

While the secure install scrip is running, the system will ask for your MySQL root password. You will be able to set the MySQL root password here.

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

You will be asked whether you want to set the root password. You can choose Y and follow the instructions.

You will be given a series of questions which you can say ‘yes’ and MySQL will be reloaded with all the changes. The screen should look similar to this:

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 MySQLinstallation should now be secure. 

Thanks for using MySQL!

 

Installation and Configuration of Nginx on VPS

 

Once the installation of MySQL is completed, you can now move on to installing nginx on your VPS.

sudo yum install nginx

Since nginx does not start on its own, you need to get it running:

sudo /etc/init.d/nginx start

Now, you can verify the nginx installation by directing your web browser to your IP address. Get your VPS IP address using:

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

Open the default nginx config file:

sudo vi /etc/nginx/nginx.conf

 

You need to make the following changes:

  1. Change the root to /usr/share/nginx/html
  2. Add index.php to index line.
  3. Replace local host (server_name) to your domain name or IP.
  4. Change the root to access document root – /var/www/wordpress
  5. Uncomment the lines in “location ~ \.php$ {“ section
  6. Change fastcgi_param line so as to help PHP interpreter find php script that was stored in document root.

Once you are done with the changes, the file should look 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;
    }
}

 

Installing and Configuring PHP

 

Let’s go ahead and install php-fpm package located in REMI repository. Run the following command to enable REMI repository and install php and php-fpm:

sudo yum --enablerepo=remi install php-fpm php-mysql

Now, open the php configuration file and update it with one change.

sudo vi /etc/php.ini

Search for the line ‘cgi.fix_pathinfo=1’ and replace 1 to 0.

cgi.fix_pathinfo=0

This is to ensure that the php interpreter processes only the exact file path. After making the changes, save and exit the file.

Now, open up the php5-fpm configuration file:

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

Search for the following lines of code and update it with nginx instead of apache:

[...]
; 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 httpduser = nginx
; RPM: Keep a group allowed to write in log dir.group = nginx
[...]

Once you are done with the changes, restart the php-fpm:

sudo service php-fpm restart

 

Creating a PHP Info Page

 

Now that the LEMP stack is completely installed on your system, you may have to verify it by creating a simple php info page.

For that, you need to create a new blank file and paste the following code.

sudo vi /usr/share/nginx/html/info.php
<?
phpphpinfo();
?>

Save and exit the file.

Restart nginx using:

sudo service nginx restart

Visit your php info page by going to your web address, http://youripaddress/info.php.

You will be able to view the nginx and php-fpm configuration details too by visiting it.

LEMP stack is now successfully installed on your VPS.

If you want the newly installed programs to automatically start when VPS boots, set up the auto-startup using the following commands:

sudo chkconfig --levels 235 mysqld on
sudo chkconfig --levels 235 nginx on
sudo chkconfig --levels 235 php-fpm on

support2 has written 111 articles

Leave a Reply