Tutorial

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

Published on June 13, 2012
How To Install Linux, nginx, MySQL, PHP (LEMP) stack on CentOS 6
Not using CentOS 6?Choose a different version or distribution.
CentOS 6

Status: Deprecated

This article covers a version of CentOS that is no longer supported. If you are currently operating a server running CentOS 6, we highly recommend upgrading or migrating to a supported version of CentOS.

Reason: CentOS 6 reached end of life (EOL) on November 30th, 2020 and no longer receives security patches or updates. For this reason, this guide is no longer maintained.

See Instead:
This guide might still be useful as a reference, but may not work on other CentOS releases. If available, we strongly recommend using a guide written for the version of CentOS you are using.

The following DigitalOcean tutorial may be of interest, as it outlines installing a LEMP stack on a CentOS 7 server:


About Lemp

LEMP stack is a group of open source software to get web servers up and running. The acronym stands for Linux, nginx (pronounced Engine x), MySQL, and PHP. Since the server is already running CentOS, the linux part is taken care of. Here is how to install the rest.

Step One—Install the Required Repositories

We will be installing all of the required software with Yum. However, because nginx is not available straight from CentOS, we'll need to install the epel repository.

sudo yum install epel-release

Step Two—Install MySQL

The next step is to begin installing the server software on the virtual private server, starting with MySQL and dependancies.

 sudo yum install mysql-server

Once the download is complete, restart MySQL:

sudo /etc/init.d/mysqld restart

You can do some configuration of MySQL with this command:

sudo /usr/bin/mysql_secure_installation

The prompt will ask you for your current root password.

Since you just installed MySQL, you most likely won’t have one, so leave it blank by pressing enter.

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

Then the prompt will ask you if you want to set a root password. Go ahead and choose Y and follow the instructions.

CentOS automates the process of setting up MySQL, asking you a series of yes or no questions.

It’s easiest just to say Yes to all the options. At the end, MySQL will reload and implement the changes.

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!

Step Three—Install nginx

As with MySQL, we will install nginx on our virtual private server using yum:

sudo yum install nginx

nginx does not start on its own. To get nginx running, type:

sudo /etc/init.d/nginx start

You can confirm that nginx has installed on your virtual private server by directing your browser to your IP address.

You can run the following command to reveal your server’s IP address.

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

Step Four—Install PHP

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:

sudo yum install php-fpm php-mysql

Step Five—Configure php

We need to make one small change in the php configuration. Open up php.ini:

 sudo 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.

Step Six—Configure nginx

Open up the default nginx config file:

sudo vi /etc/nginx/nginx.conf

Raise the number of worker processes to 4 then save and exit that file.

Now we should configure the nginx virtual hosts.

In order to make the default nginx file more concise, the virtual host details are in a different location.

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

The configuration should include the changes below (the details of the changes are under the config information):

#
# 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:

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
[...]

Finish by restarting php-fpm.

sudo service php-fpm restart

Step Seven—RESULTS: Create a php info page

Although LEMP is installed, we can still take a look and see the components online by creating a quick php info page

To set this up, first create a new file:

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

Add in the following line:

<?php
phpinfo();
?>

Then Save and Exit.

Restart nginx so that all of the changes take effect:

sudo service nginx restart

Finish up by visiting your php info page (make sure you replace the example ip address with your correct one): http://12.34.56.789/info.php

It should look similar to this.

Step Eight—Set Up Autostart

You are almost done. The last step is to set all of the newly installed programs to automatically begin when the VPS boots.

sudo chkconfig --levels 235 mysqld on
sudo chkconfig --levels 235 nginx on
sudo chkconfig --levels 235 php-fpm on
By Etel Sverdlov

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
10 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

problem solved by: sudo yum --enablerepo=remi install phpmyadmin

I’ve a problems with No input file specified.

Here in my: nginx.conf http://pastebin.com/8hfBZ7p5 default.conf http://pastebin.com/Kn4amsuz

Followed all of above steps, but when I try to open http://12.34.56.789/info.php, getting “No input file specified.” always.

Any clues?

Thanks for this tutorial, it has to help e a lot with all my server configurations. However, you forgot to mention that for those who uses scripts that require sessions you need to make the sessions folder and give permissions to Nginx user.

mkdir /var/lib/php/session
chown -R nginx:nginx /var/lib/php/session

To those of you still struggling with the “no input file specified” or the info.php file downloading (if you followed the tutorial like I did). This might work for you.

My /etc/nginx/conf.d/default.conf file looked like this:

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 /scripts$fastcgi_script_name; include fastcgi_params; }

Pay attention to the /scripts portion above. From a default nginx install, you need to change the above to read:

$document_root

My default.conf file now looks like this:

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; }

And that solved my problem :-)

213123123

Many thanks!

This comment has been deleted

    Followed the tutorial on a fresh centos 6.7. Can’t get it working. In the end, when i type my IP, i get a “This webpage is not available”. Following is my nginx.conf file …

    For more information on configuration, see:

    * Official English Documentation: http://nginx.org/en/docs/

    * Official Russian Documentation: http://nginx.org/ru/docs/

    user nginx; worker_processes 4;

    error_log /var/log/nginx/error.log; #error_log /var/log/nginx/error.log notice; #error_log /var/log/nginx/error.log info;

    pid /var/run/nginx.pid;

    events { worker_connections 1024; }

    http { include /etc/nginx/mime.types; default_type application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    
    access_log  /var/log/nginx/access.log  main;
    
    sendfile        on;
    #tcp_nopush     on;
    
    #keepalive_timeout  0;
    keepalive_timeout  65;
    
    #gzip  on;
    
    # Load config files from the /etc/nginx/conf.d directory
    # The default server is in conf.d/default.conf
    include /etc/nginx/conf.d/*.conf;
    

    }

    You say: The first thing we need to do is enable the REMI repository

    So why you dont say how to do this?

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Become a contributor for community

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    DigitalOcean Documentation

    Full documentation for every DigitalOcean product.

    Resources for startups and SMBs

    The Wave has everything you need to know about building a business, from raising funding to marketing your product.

    Get our newsletter

    Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

    New accounts only. By submitting your email you agree to our Privacy Policy

    The developer cloud

    Scale up as you grow — whether you're running one virtual machine or ten thousand.

    Get started for free

    Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

    *This promotional offer applies to new accounts only.