Build a LEMP stack (Linux, NGINX, MySQL, PHP) - CentOS 6

Difficulty: 3
Time: 20 minutes

To build a dynamic web application, you need what has been coined a “stack” - which is developer lingo for an integrated set of software that has all of the components your application needs.

Most stacks have the same types of component, and differ mainly in which pieces of software they use for those components. For example, we’ll look at LEMP (which this article covers):

Component LAMP Software
Operating system Linux (e.g. CentOS)
Web server NGINX (pronounced engine-x)
Database MySQL
Programming language PHP

If you're astute, maybe you noticed that LEMP is basically an acronym for the software the stack uses (LNMP is difficult to pronounce in English, so they used “E” for NGINX because of its pronunciation).

In this guide, we'll walk you through installing all of these components (except for Linux, which is already installed as your OS when you create the server).

Install MySQL

  1. Install the Extra Packages for Enterprise Linux Repository (or EPEL for short):
    sudo yum install epel-release
  2. Run a yum update to sync your packages with the new EPEL repository:
    sudo yum update
  3. Install MySQL:
    sudo yum install mysql-server
  4. Activate MySQL:
    sudo service mysqld start
  5. Configure your MySQL installation:
    sudo /usr/bin/mysql_secure_installation
  6. At the prompt "Enter current password for root (enter for none)", press Enter.
  7. The prompt will request the answer to a number of questions.
    Question Recommended Response Notes
    Do you want to change or set the root password? y Think of a secure password, and re-enter it when prompted.
    Remove anonymous users? y The anonymous user allows anyone to log in without requiring an account created by an administrator. A hacker attempting to compromise your database server will surely try logging in as this user.
    Disallow root login remotely? y It is more secure to create a non-root user with admin permissions. The MySQL root user should only be used when connected directly to the database server.
    Remove test database and access to it? y The test database is another possible source of compromise, and is rarely needed. If you have a reason to keep a test database, answer "no."
    Reload privilege tables now? y This confirms your settings on the server, so say yes.
  8. Make it so that MySQL will start automatically on server reboot:
    sudo chkconfig mysqld on
  9. Restart MySQL to save the changes:
    sudo service mysqld restart

Install NGINX

  1. Install NGINX:
    sudo yum install nginx
  2. Start the NGINX service:
    sudo service nginx start
  3. Make it so that NGINX will start automatically on server reboot:
    sudo chkconfig nginx on
  4. Verify that NGINX is installed by going to:
    http://your server's IP address

    Note: GoDaddy Cloud Server customers can use Find your server's IP.

A Welcome to nginx! page displays.

Install and configure PHP

  1. Install PHP:
    sudo yum install php php-fpm php-mysql
  2. Open php.ini:
    sudo vim /etc/php.ini
  3. Find the cgi.fix_pathinfo directive, and set it as follows:
    cgi.fix_pathinfo=0
  4. Save and close the file:
    :wq!
  5. Open the www.conf file for editing:
    sudo vim /etc/php-fpm.d/www.conf
  6. Find the listen directive (it should be the first), and verify that it is set to listen for PHP traffic using a Unix socket (instead of port 9000):
    listen = /var/run/php-fpm/php-fpm.sock
  7. Find the Set permissions for unix socket... section, uncomment the listen.owner and listen.group sections, and edit them so that the user and group are set to nginx instead of apache:
    listen.owner = nginx
    listen.group = nginx
  8. Find the Unix user/group of processes section, and change the user and group from apache to nginx:
    user = nginx
    group = nginx
  9. Save and close the file:
    :wq!
  10. Restart PHP:
    sudo systemctl restart php-fpm
    This creates the needed php-fpm.sock file
  11. Modify the permissions and ownership for the php-fpm.sock file as follows:
    sudo chmod 666 /run/php-fpm/php-fpm.sock
    sudo chown nginx:nginx /run/php-fpm/php-fpm.sock
  12. Restart PHP again:
    sudo service php-fpm restart

Configure NGINX

All of your LEMP stack components are now installed. You need only to make some changes to the default NGINX so that it properly serves PHP with FastCGI.

  1. You may want to make a copy of the default.conf file before editing it:
    sudo cp  /etc/nginx/conf.d/default.conf  /etc/nginx/conf.d/default.safety
  2. Open the default.conf file for editing:
    sudo vim /etc/nginx/conf.d/default.conf
  3. Find the server context, which will look something like this (comments removed):

    server {
      listen  80 default_server;
      server_name  _;
      include /etc/nginx/default.d/*.conf;
      location / {
        root   /usr/share/nginx/html;
        index  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;
      ocation = /50x.html {
        root   /usr/share/nginx/html;
        }
    }
  4. Make the following edits (which are substantial, but don't worry - there's a template to show you how everything should work below).
    • Remove default_server so that the listen statement looks like:
      listen 80;
    • Change the server_name statement to use your server's IP address or the domain you want to use:
      server_name your domain name or IP;
    • Edit the index statement to include index.php:
      index index.php index.html index.htm;
    • Add a try_files statement to the location / {} block:
      try_files $uri $uri/ /index.html;
    • Add a new location ~ \.php$ {} block to tell NGINX how to properly use PHP with FastCGI:
      location ~ \.php$ {
                    root           /usr/share/nginx/html;
                    fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
                    fastcgi_index  index.php;
                    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                    include        fastcgi_params;
                  }
  5. Your new default server block should look like this, with comments removed (changes are highlighted in blue):
    server {
      listen 80;
      server_name your domain name or IP;
      include /etc/nginx/default.d/*.conf;
      location / {
        root /usr/share/nginx/html;
        index index.php index.html index.htm;
        try_files $uri $uri/ /index.html;
        }
      error_page 404 /404.html;
      error_page 500 502 503 504 /50x.html;
      location = /50x.html {
        root /usr/share/nginx/html;
        }
      location ~ \.php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
        }
    }
  6. Save and close the file:
    :wq!
  7. Restart PHP:
    sudo service php-fpm restart
  8. Restart NGINX:
    sudo service nginx restart

Note: If NGINX fails to restart, check your edits. Make sure that all of your braces ({}) are closed and that each directive ends with a semi-colon (;).

Verify that PHP is running

  1. Create a php test file, info.php:
    sudo vim /usr/share/nginx/html/info.php
  2. Insert the following contents:
    <?php
    phpinfo();
    ?>
  3. Go to:
    http://your server's IP address/info.php

A page full of statistics with a PHP logo in the upper right-hand corner will be displayed.

Next steps

Now that you have a LEMP stack, we recommend setting up NGINX server blocks for your websites.


Bài này có hữu ích không?
Cảm ơn về phản hồi của bạn. Để nói chuyện với một đại diện của dịch vụ khách hàng, vui lòng sử dụng số điện thoại hỗ trợ hoặc tùy chọn trò chuyện ở trên.
Rất vui vì chúng tôi đã giúp được bạn! Chúng tôi có thể làm gì thêm cho bạn?
Rất tiếc về điều đó. Vui lòng cho chúng tôi biết điều gì làm cho bạn bối rối và vì sao giải pháp không khắc phục được vấn đề này.