Install Nginx, MariaDB, PHP7 (LEMP) on Arch Linux Server in 2019

In a previous tutorial, I explained how to install Arch Linux on KVM VPS. Now this tutorial will show you how to Install Nginx, MariaDB, PHP7, aka LEMP stack, on Arch Linux server. You can also follow this guide to install LEMP stack on your Arch Linux home computer.

Step 1: Update Arch Linux Server

Before installing any packages on Arch Linux, it’s always a good idea to refresh repository and perform an update first. Enter the following command in terminal to upgrade software.

sudo pacman -Syu

Step 2: Install Nginx Web Server

The Arch Linux repository contains two versions of Nginx Web server: nginx and nginx-mainline. nginx is the more stable and older version and nginx-mainline is the latest version. That doesn’t necessarily mean nginx-mainline is unstable. In fact, nginx.org recommends using nginx mainline if you don’t have a strong reason to use the older version.

To install the older version:

sudo pacman -S nginx

This tutorial installs the mainline version.

sudo pacman -S nginx-mainline

By default, the above two commands will also install geoip and geoip-database package. Once it’s installed, check Nginx status.

systemctl status nginx

Output:

● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
     Active: active (running) since Tue 2016-04-12 13:10:57 CST; 1 day 7h ago
   Main PID: 2010 (nginx)
      Tasks: 2 (limit: 512)
     CGroup: /system.slice/nginx.service
             ├─2010 nginx: master process /usr/bin/nginx -g pid /run/nginx.pid; daemon on; master_process on
             └─2011 nginx: worker process

If it’s not running, start it with systemctl:

sudo systemctl start nginx

Enable Nginx to auto-start when Arch Linux server is booted up.

sudo systemctl enable nginx

Check Nginx version.

nginx -v

output:

nginx version: nginx/1.17.1

Enter the IP address of your Arch Linux server in the browser address bar, if you see the following text, that means Nginx is running correctly.

Install (LEMP) Nginx, MariaDB, PHP7 on Arch Linux Server

You can check your server’s public IP with the following command.

curl http://icanhazip.com

If you are installing Nginx on your Arch Linux home computer, just type 127.0.0.1 or localhost in the address bar.

Step 3: Install MariaDB Database Server

MariaDB is Arch Linux’s default implementation of MySQL since 2013. Oracle MySQL was dropped to AUR. So you see, Arch Linux really wants you to migrate from MySQL to MariaDB.

MariaDB is provided with the mariadb package.

sudo pacman -S mariadb

You need to initialize the MariaDB data directory prior to starting the service. This can be done with mariadb-install-db command.

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

Now start MariaDB.

sudo systemctl start mariadb

Check if it’s running:

systemctl status mariadb

Output:

● mysqld.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2016-04-13 21:09:45 CST; 5s ago
  Process: 8373 ExecStartPost=/usr/bin/mysqld-post (code=exited, status=0/SUCCESS)
 Main PID: 8372 (mysqld)
    Tasks: 26 (limit: 512)
   CGroup: /system.slice/mysqld.service
           └─8372 /usr/bin/mysqld --pid-file=/run/mysqld/mysqld.pid

Enable MariaDB to auto-start when Arch Linux is booted up.

sudo systemctl enable mariadb

Run the post-installation security script.

sudo mysql_secure_installation

You will be asked to enter your MariaDB root password. Since you have not a MariaDB root password yet, press enter.

arch linux mysql_secure_installation

When asked if you wan to switch to unix_socket authentication, you can press Enter. (Note that Y is capitalized, which means it is the default answer.)  When asked if you want to change root password, answer ‘n’, because unix_socket authentication doesn’t require the MariaDB root user to have a password. Then you can press Enter to answer all remaining questions, which will remove test database, anonymous user and disable root remote login.

To log into MariaDB console, simply run the following command.

sudo mysql -u root

To exit, run

exit

The default main configuration file is located at /etc/my.cnf.

Step 4: Install PHP7

The latest version of PHP in Arch Linux repository is 7.3.6. To install it, run this command:

sudo pacman -S php php-fpm

After it’s installed, we need to tell Nginx to run PHP using php-fpm. To achieve that, edit /etc/nginx/nginx.conf file.

sudo nano /etc/nginx/nginx.conf

Find the location ~ \.php$ section (line 65) and modify it to the following.

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

Save and close the file. Then test Nginx configurations.

sudo nginx -t

If the test is successful, reload Nginx.

sudo systemctl reload nginx

Then start and enable php-fpm service with the following commands:

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

Step 5: Test PHP Processing

Create a test.php file in the web root .

sudo nano /usr/share/nginx/html/test.php

Add these lines to the file.

<?php
  phpinfo();
?>

Save and close the file. Now in your browser address, type http://your-server-ip/test.php. You should see all your PHP info. If you are installing LEMP on your Arch Linux desktop or laptop, type 127.0.0.1/test.php or localhost/test.php.

install LEMP stack on arch linux

The test.php file is for testing purpose only. For security reasons you can now remove it.

sudo rm /usr/share/nginx/html/test.php

Enable PHP Extensions

Edit /etc/php/php.ini config file.

sudo nano /etc/php/php.ini

Find the following two lines (line 922 and 926), remove semicolons to enable these two extensions.

;extension=mysqli
;extension=pdo_mysql

You can also run the following 2 commands to enable mysqli and pdo_mysql extensions on Arch Linux, so you don’t have to open the file and find the 2 lines.

sudo sed -i 's/;extension=mysqli/extension=mysqli/g' /etc/php/php.ini
sudo sed -i 's/;extension=pdo_mysql/extension=pdo_mysql/g' /etc/php/php.ini

Reload php-fpm service for the changes to take effect.

sudo systemctl reload php-fpm

mysqli.so is used to connect PHP with MariaDB/MySQL database. pdo_mysql.so is necessary to display Drupal sites. You may also need to enable imagemagick extension for PHP in order to crop and rotate images in CMS platforms such as WordPress.

Wrapping Up

Congrats! You have successfully installed Nginx, MariaDB and PHP7 on Arch Linux server! As always, if you found this post useful, then subscribe to our free newsletter to get more tips and tricks. Take care 🙂

Rate this tutorial
[Total: 25 Average: 4.7]

7 Responses to “Install Nginx, MariaDB, PHP7 (LEMP) on Arch Linux Server in 2019

  • Hi,

    You are the best! 🙂

    Thanks a lot and best regards!

  • Yu-Wei Lu
    1 day ago

    Thanks! This article help me a lot. 🙂

  • Good day to you,

    …this … *IS* the way that a tutorial walkthrough is written when it is well done.

    THANK YOU for having refreshed my faith in online help and also – of course- for your informative, keeping-me-in-the-loop assistance while I install LEMP.

    I’ll never use a script (installer or other) that I can’t understand, and I’d urge any others reading this to also adopt that “best-practice”

    Regards – I wish you much helpdesk good Karma

    Matty

  • Hi,
    Thank you very much for this step-by-step guide, going through Arch Linux Wiki was really a pain for me.

    I face problems with PHP scripts not interpreted and replaced by “File not found.” message only.

    What did help is to add the full path to the HTML root directory in ‘/etc/nginx/nginx.conf’ file for the “fastcgi_param SCRIPT_FILENAME …..” line in the “pass the PHP scripts to FastCGI” section.

    Following is what’s working for me:

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
    root /usr/share/nginx/html;
    fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/$fastcgi_script_name;
    include fastcgi_params;
    }

    Nginx version: 1.17.8

    I still wonder what to write instead of the absolute full path to the “HTML root directory”… ?
    Would be very convenient to use a variable in the entire configuration file… 😉

    I hope that my comment may help someone else too ! 🙂
    Cheers !

    “HP” 🙂

    • Helped me… 4 years later lol apparently this is still an issue if installing base nginx instead of nginx-mainline

  • I have this error after:

    systemctl start php-fpm

    failed to start php-fpm.service: Unit php-fpm.service has a bad unit file setting.
    See system logs and ‘systemctl status php-fpm.service’ for details.

    logs:
    php-fpm.service
    Loaded: bad-setting (Reason: Unit php-fpm.service has a bad unit file se>
    Active: inactive (dead)

  • fotastisch
    4 years ago

    Hi!

    This already helped me a lot, thank you very much! (I used this excellent guide setting up a LEMP server once before.)

    Trying this again, I’m running into a problem though: Instead of showing the phpinfo, calling IP/test.php only downloads the test.php file. It works in the console, but does not show the information when called in a browser. Did something change (running php7.4)?

Leave a Comment

  • Comments with links are moderated by admin before published.
  • Your email address will not be published.
  • Use <pre> ... </pre> HTML tag to quote the output from your terminal/console.
  • Please use the community (https://community.linuxbabe.com) for questions unrelated to this article.
  • I don't have time to answer every question. Making a donation would incentivize me to spend more time answering questions.

The maximum upload file size: 2 MB. You can upload: image. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded. Drop file here