How to Install LEMP Stack (Nginx, MariaDB, PHP7.1) on Ubuntu 17.10
This tutorial is going to show you how to install Nginx, MariaDB and PHP7.1 (LEMP) on Ubuntu 17.10. You can follow this tutorial on a VPS (Virtual Private Server) or on a local Ubuntu 17.10 computer. PHP7.1 is included in Ubuntu 17.10 and has a minor performance boost over PHP7.0.
Step 1: Update Software Packages
Before we install the LEMP stack, it’s a good idea to update repository and software packages. Run the following command on your Ubuntu 17.10 OS.
sudo apt update sudo apt upgrade
Step 2: Install Nginx Web Server
Nginx is a high performance web server and very popular these days. It also can be used as a reverse proxy. Enter this command to install Nginx Web server.
sudo apt install nginx
After it’s installed, we can enable Nginx to auto start when Ubuntu is booted by running the following command.
sudo systemctl enable nginx
Then start Nginx with this command:
sudo systemctl start nginx
Now check out its status.
systemctl status nginx
Output:
● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: en Active: active (running) since Tue 2017-12-12 06:17:50 UTC; 51s ago Docs: man:nginx(8) Main PID: 15672 (nginx) Tasks: 2 (limit: 4915) Memory: 6.3M CPU: 37ms CGroup: /system.slice/nginx.service ├─15672 nginx: master process /usr/sbin/nginx -g daemon on; master_pr └─15675 nginx: worker process
“enabled” indicates that auto start at boot time is enabled and we can see that Nginx is running. Notice that the above command will not immediately quit after running. You need to press “q” to make it quit.
Check Nginx version.
nginx -v
Output:
nginx version: nginx/1.12.1 (Ubuntu)
Now type in the public IP address of your Ubuntu 17.10 server in the browser address bar. You should see the “Welcome to Nginx” Web page, which means Nginx Web server is running properly. If you are installing LEMP on your local Ubuntu 17.10 computer, then type 127.0.0.1
or localhost
in the browser address bar.
Finally, we need to make www-data
(Nginx user) as the owner of web root directory. By default it’s owned by the root user.
sudo chown www-data:www-data /usr/share/nginx/html -R
Step 3: Install MariaDB Database Server
MariaDB is a drop-in replacement for MySQL. It is developed by former members of MySQL team who are concerned that Oracle might turn MySQL into a closed-source product. Enter the following command to install MariaDB on Ubuntu 17.10.
sudo apt install mariadb-server mariadb-client
After it’s installed, MariaDB server should be automatically stared. Use systemctl to check its status.
systemctl status mariadb
Output:
● mariadb.service - MariaDB database server Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset:enabled) Active: active (running) since Tue 2017-12-12 06:36:12 UTC; 6s ago Main PID: 17409 (mysqld) Status: "Taking your SQL requests now..." Tasks: 26 (limit: 4915) Memory: 59.9M CPU: 342ms CGroup: /system.slice/mariadb.service └─17409 /usr/sbin/mysqld
If it’s not running, start it with this command:
sudo systemctl start mariadb
To enable MariaDB to automatically start at boot time, run
sudo systemctl enable mariadb
Now run the post installation security script.
sudo mysql_secure_installation
When it asks you to enter MariaDB root password, press Enter key as the root password isn’t set yet. Then enter y to set the root password for MariaDB server.
Next, you can press Enter to answer all remaining questions, which will remove anonymous user, disable remote root login and remove test database. This step is a basic requirement for MariaDB database security. (Notice that Y is capitalized, which means pressing Enter is the same as typing in Y. )
By default, the MaraiDB package on Ubuntu uses unix_socket to authenticate user login, which basically means you can use username and password of the OS to log into MariaDB console. So you can run the following command to login without providing MariaDB root password.
sudo mariadb -u root
To exit, run
exit;
Check MariaDB server version information.
mariadb --version
As you can see, we have installed MariaDB 10.1.25.
mariadb Ver 15.1 Distrib 10.1.25-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2
Step 4: Install PHP7.1
PHP7.1 is included in Ubuntu 17.10 repository and has a minor performance boost over PHP7.0. Enter the following command to install PHP7.1 and some common extensions.
sudo apt install php7.1 php7.1-fpm php7.1-mysql php-common php7.1-cli php7.1-common php7.1-json php7.1-opcache php7.1-readline php7.1-mbstring php7.1-xml php7.1-gd php7.1-curl
PHP extensions are commonly needed for content management systems (CMS) like WordPress. For example, if your installation lacks php7.1-xml
, then some of your WordPress site pages may be blank and you can find an error in Nginx error log like:
PHP message: PHP Fatal error: Uncaught Error: Call to undefined function xml_parser_create()
Installing these PHP extensions ensures that your CMS runs smoothly. Now start php7.1-fpm.
sudo systemctl start php7.1-fpm
Enable auto-start at boot time.
sudo systemctl enable php7.1-fpm
Check status:
systemctl status php7.1-fpm
Sample output:
● php7.1-fpm.service - The PHP 7.1 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php7.1-fpm.service; enabled; vendor preset:enabled) Active: active (running) since Tue 2017-12-12 06:53:08 UTC; 4min 25s ago Docs: man:php-fpm7.1(8) Main PID: 27560 (php-fpm7.1) Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/se Tasks: 3 (limit: 4915) Memory: 28.0M CPU: 58ms CGroup: /system.slice/php7.1-fpm.service ├─27560 php-fpm: master process (/etc/php/7.1/fpm/php-fpm.conf) ├─27580 php-fpm: pool www └─27583 php-fpm: pool www
Step 5: Create a Nginx Server Block
A Nginx server block is like a virtual host in Apache. We will not use the default server block because it’s inadequate to run PHP code and if we modify it, it becomes a mess. So remove the default
symlink in sites-enabled
directory by running the following command. (It’s still available as /etc/nginx/sites-available/default
.)
sudo rm /etc/nginx/sites-enabled/default
Then create a brand new server block file under /etc/nginx/conf.d/ directory.
sudo nano /etc/nginx/conf.d/default.conf
Paste the following text into the file. Replace 12.34.56.78 with your actual server IP address.
server {
listen 80;
listen [::]:80;
server_name 12.34.56.78;
root /usr/share/nginx/html/;
index index.php index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ /index.php;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
include snippets/fastcgi-php.conf;
}
location ~ /\.ht {
deny all;
}
}
Save and close the file. Then test Nginx configurations.
sudo nginx -t
If the test is successful, reload Nginx.
sudo systemctl reload nginx
Step 6: Test PHP
To test PHP-FPM with Nginx Web server, we need to create a info.php
file in the document root directory.
sudo nano /usr/share/nginx/html/info.php
Paste the following PHP code into the file.
<?php phpinfo(); ?>
Save and close the file. Now in the browser address bar, enter server-ip-address/info.php
. Replace sever-ip-address
with your actual IP. If you follow this tutorial on your local computer, then type 127.0.0.1/info.php
or localhost/info.php
.
You should see your server’s PHP information. This means PHP scripts can run properly with Nginx web server.
How to Install PHP7.2
PHP7.2 is the latest stable version of PHP, released on November 30, 2017 and it has minor performance boost over PHP7.1. We can add the PPA from Ondrej Sury to install PHP7.2 on Ubuntu 17.10. That guy is also the maintainer of Certbot PPA.
sudo apt install software-properties-common sudo add-apt-repository ppa:ondrej/php sudo apt update
Then we can install PHP7.2 and common extensions by using the following command.
sudo apt install php7.2 php7.2-fpm php7.2-mysql php-common php7.2-cli php7.2-common php7.2-json php7.2-opcache php7.2-readline php7.2-mbstring php7.2-xml php7.2-gd php7.2-curl
Now start PHP7.2-FPM.
sudo systemctl start php7.2-fpm
Enable auto-start at system boot time.
sudo systemctl enable php7.2-fpm
Check its status:
systemctl status php7.2-fpm
Using PHP7.2-FPM with Nginx
To make Nginx use PHP7.2-FPM instead of PHP7.1-FPM, we need to edit the Nginx server block file.
sudo nano /etc/nginx/conf.d/default.conf
Find the following line.
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
Change php7.1-fpm
to php7.2-fpm
.
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
Save and close the file. Then reload Nginx for the change to take effect.
sudo systemctl reload nginx
If you refresh the server-ip-address/info.php
page, you will see Nginx is now using PHP7.2-FPM.
PHP7.1 can coexist with PHP7.2. If you don’t want to use PHP7.1, then you can remove it. Please beware that some web application may not be compatible with PHP7.2 like NextCloud 12. In my test, WordPress runs well with PHP7.2.
Congrats! You have successfully installed Nginx, MariaDB and PHP7 on Ubuntu 17.10. For your server’s security, you should delete info.php file now to prevent prying eyes.
sudo rm /usr/share/nginx/html/info.php
That’s it! I hope this tutorial helped you install LEMP stack on Ubuntu 17.10. As always, if you found this post useful, then subscribe to our free newsletter to get more tips and tricks.