10 ноября 2019

Install Nginx, MariaDB, PHP7 (LEMP) on Arch Linux Server in 2019 (reposted from https://www.linuxbabe.com/)

Main article — https://www.linuxbabe.com/linux-server/install-lemp-nginx-mariadb-php7-arch-linux-server by Xiao Guoan

I just saved for own use because i am very forgetful ))

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

● 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

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

● 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

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.

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 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.

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.