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

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. Arch Linux)
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. Run the pacman update to ensure your server is up-to-date on all of its packages:
    sudo pacman -Syu
  2. Install MySQL:
    sudo pacman -S mysql
  3. When prompted, choose the mariadb provider. MariaDB is the community-driven drop-in replacement for MySQL, and is functionally identical to MySQL.
  4. Initialize the MySQL database and data directory:
    sudo mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
  5. Start MySQL:
    sudo systemctl start mysqld
  6. Configure your MySQL installation:
    sudo /usr/bin/mysql_secure_installation
    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?
  7. Restart MySQL to save the changes:
    sudo systemctl restart mysqld
  8. Make sure that MySQL will start upon server boot:
    sudo systemctl enable mysqld

Install and start NGINX

  1. Install NGINX:
    sudo pacman -S nginx
  2. Start the NGINX service:
    sudo systemctl start nginx
  3. Make sure that NGINX will start upon server boot:
    sudo systemctl enable nginx
  4. Verify that NGINX is installed by going to:
    http://your server's IP address

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

A Welcome to nginx! page displays.

Install PHP

  1. Install PHP:
    sudo pacman -S php-fpm php
  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. Restart PHP:
    sudo systemctl restart php-fpm
    This creates the needed php-fpm.sock file
  6. Change permissions and ownership on php-fpm.sock:
    sudo chmod 666 /run/php-fpm/php-fpm.sock
    sudo chown http:http /run/php-fpm/php-fpm.sock
  7. Restart PHP again:
    sudo systemctl restart php-fpm

Configure NGINX

All of your LEMP stack components are now installed. You need only make NGINX aware that a FastCGI server exists to process PHP requests.

  1. Open /etc/nginx/nginx.conf for editing:
    sudo vim /etc/nginx/nginx.conf
  2. Find the server context, which will look something like this (comments removed):

    server {
      listen  80;
      server_name  localhost;
      location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        }
      error_page   500 502 503 504  /50x.html;
      location = /50x.html {
        root   /usr/share/nginx/html;
        }
    }
  3. Make the following edits (which are substantial, but don't worry - there's a template to show you how everything should work below).
    • 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.html;
    • 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:/run/php-fpm/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
      }
    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;
      location / {
        root   /usr/share/nginx/html;
        index  index.php index.html index.htm;
        try_files $uri $uri/ /index.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:/run/php-fpm/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
        }
    }
  4. Save and close the file:
    :wq!
  5. Restart NGINX:
    sudo systemctl restart nginx

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 PHP is running

  1. Create a test file in your web root:
    sudo vim /usr/share/nginx/html/info.php
  2. Give this file 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.


Was This Article Helpful?
Thank You For Your Feedback
Glad we helped! Anything more we can do for you?
Sorry about that. How can we be more helpful?