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

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/MariaDB

  1. Install MariaDB. MariaDB is the community-driven drop-in replacement for MySQL, and is functionally identical to MySQL:
    sudo yum install mariadb mariadb-server
  2. Activate MySQL:
    sudo systemctl start mariadb.service
  3. Set MySQL to start on server boot:
    sudo systemctl enable mariadb.service
  4. Configure your MySQL installation:
    sudo /usr/bin/mysql_secure_installation
    The prompt will request the answer to a number of questions.
    Question Suggested answer Reasoning
    Enter current password for root: Press Enter for none. You have not set a password yet, so hit enter for none.
    Set Root Password? y A password is needed to secure MySQL. Enter a strong password and write it down.
    Remove anonymous users? y The anonymous user allows anyone to login without requiring an account created by an admin. A hacker attempting to compromise your database server may 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 Why wait?
  5. Restart MySQL:
    sudo systemctl restart mariadb.service

Install and start NGINX

  1. Install NGINX:
    sudo yum install nginx
  2. Start the NGINX service:
    sudo systemctl start nginx.service
  3. Set NGINX to start upon server boot:
    sudo systemctl enable nginx.service
  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 PHP

  1. Install PHP:
    sudo yum install php php-mysql php-fpm
  2. Open php.ini:
    sudo vim /etc/php.ini
  3. Find the cgi.fix_pathinfo directive, uncomment it by removing the # and set it to 0:
    cgi.fix_pathinfo=0
  4. Save and close the file:
    :wq!
  5. Open www.conf:
    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 = /run/php-fpm/php-fpm.sock
  7. Find the listen.owner and listen.group directives, uncomment them, and modify them as follows:
    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. Change permissions and ownership on php-fpm.sock:
    sudo chmod 666 /run/php-fpm/php-fpm.sock
    sudo chown nginx:nginx /run/php-fpm/php-fpm.sock
  12. Restart PHP again:
    sudo systemctl restart php-fpm

Configure NGINX

  1. Open /etc/nginx/nginx.conf:
    sudo vim /etc/nginx/nginx.conf
  2. Ensure the following line is in the server context:
    include /etc/nginx/default.d/*.conf;
  3. Save and close the file:
    :wq!
  4. Create a new default file in /etc/nginx/default.d/default.conf:
    sudo vim /etc/nginx/default.d/default.conf
  5. Paste the following code into this file:
    index index.php index.html index.htm;
    server_name your domain name or IP;
    # pass the PHP scripts to FastCGI server listening on the php-fpm socket
    location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    }
    This code adds the needed default settings for your domains, as well as telling NGINX how to properly use PHP with FastCGI.

    Note: As long as the fastcgi_pass in the nginx.conf file matches the listen directive in the PHP configuration file, NGINX should be able to pass requests on to PHP.

  6. Save and close the file:
    :wq!
  7. Restart NGINX:
    sudo systemctl restart nginx

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

Verify PHP is running

  1. Create an info.php file to ensure PHP is running:
    sudo vim /usr/share/nginx/html/info.php
  2. Paste in 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?
Thanks for your feedback. To speak with a customer service representative, please use the support phone number or chat option above.
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 đó. Tell us what was confusing or why the solution didn’t solve your problem.