LEMP

How to Install LEMP Stack (Nginx, MySQL, PHP) on Arch Linux

If you’re looking to host your website or web application on your Arch Linux distribution, setting up a LEMP stack (Linux, Nginx, MySQL, PHP) is a solid choice for a high-performance web server environment. In this blog post, we’ll go through the steps required to install and configure each component of the LEMP stack on your Arch Linux system.

Prerequisites

Before we start, make sure you have:

  • An Arch Linux system running.
  • Sudo privileges to install packages.
  • Basic familiarity with the command line.

Step 1: Update Your System

Start by ensuring your package list is updated and your system is upgraded. Open your terminal and run:

sudo pacman -Syu

Step 2: Install Nginx

Nginx is a high-performance web server that we’ll use to serve our web content. To install Nginx, execute the following command:

sudo pacman -S nginx

Once installed, you can start Nginx and enable it to start at boot:

sudo systemctl start nginx
sudo systemctl enable nginx

To verify that Nginx is running, open your web browser and enter http://localhost. You should see the Nginx welcome page.

Step 3: Install MySQL (MariaDB)

Next, we need a database server. Instead of the traditional MySQL, we’ll install MariaDB, a drop-in replacement for MySQL. Install it by running:

sudo pacman -S mariadb

After installation, run the following commands to initialize the database and start the MariaDB service:

sudo mariadb-install-db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
sudo systemctl start mariadb
sudo systemctl enable mariadb

To secure your MariaDB installation, run:

sudo mysql_secure_installation

You’ll be prompted to set a root password and select various security options. Follow the prompts to secure your installation.

Step 4: Install PHP

Now, let’s install PHP along with some common extensions necessary for most web applications. Run:

sudo pacman -S php php-fpm php-mysql

After installing, we need to configure PHP-FPM (FastCGI Process Manager) to work with Nginx. Open the PHP-FPM configuration file:

sudo nano /etc/php/php-fpm.conf

Uncomment the listen directive and change it to:

listen = /run/php-fpm/php-fpm.sock

Next, we need to add a user to the PHP-FPM pool. Edit the file by running:

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

Change the following lines to ensure that PHP runs under the Nginx user:

user = http
group = http
listen.owner = http
listen.group = http

Save and exit the editor.

Start PHP-FPM and enable it to start on boot:

sudo systemctl start php-fpm
sudo systemctl enable php-fpm

Step 5: Configure Nginx to Use PHP

Now that we have Nginx and PHP installed, let’s configure Nginx to process PHP files. Open the default Nginx configuration file:

sudo nano /etc/nginx/nginx.conf

In the server block, add the following lines:

server {
    listen 80;
    server_name your_domain.com; # Replace with your domain or IP address

    root /usr/share/webapps; # Change this to your document root
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include FASTCGI_PARAMS;
        fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

Make sure to replace your_domain.com and /usr/share/webapps with your actual domain and document root path.

Step 6: Test Nginx Configuration

Before restarting Nginx, it’s a good idea to test the configuration for syntax errors:

sudo nginx -t

If everything is okay, reload Nginx to apply the changes:

sudo systemctl reload nginx

Step 7: Create a Test PHP File

To verify that PHP is working correctly with Nginx, create a test PHP file in your document root:

echo "<?php phpinfo(); ?>" | sudo tee /usr/share/webapps/info.php

Now navigate to http://your_domain.com/info.php in your web browser. You should see the PHP information page which confirms that your PHP installation is working with Nginx.

Conclusion

Congratulations! You’ve successfully installed and configured a LEMP stack (Nginx, MySQL, PHP) on Arch Linux. This stack is now ready to serve your web applications. Don’t forget to delete the info.php file after testing, as it can provide sensitive information about your server if it remains accessible.

Feel free to explore more configurations, install additional PHP extensions, and harden your setup further based on your needs.

Happy hosting! 🌱