How to Install LEMP Stack (Nginx, MariaDB, PHP7.2) on Ubuntu 18.04 LTS
This tutorial is going to show you how to install Nginx, MariaDB and PHP7.2 (LEMP) on Ubuntu 18.04. A software stack is a set of software tools bundled together. LEMP stands for Linux, Nginx (Engine-X), MariaDB/MySQL and PHP, all of which are open source and free to use. It is the most common software stack that powers dynamic websites and web applications. Linux is the operating system; Nginx is the web server; MariaDB/MySQL is the database server and PHP is the server-side scripting language responsible for generating dynamic web pages.
Prerequisite
To follow this tutorial, you need to have an Ubuntu 18.04 OS running on your local computer or on a remote server. If you are looking for a VPS (Virtual Private Server), then you can click this special link to get $50 free credit on DigitalOcean. (For new users only). If you are already a DigitalOcean user, then you can click this special link to get $50 free credit on Vultr (for new users only). Without further ado, let’s get started.
Step 1: Update Software Packages
Before we install the LEMP stack, it’s a good practice to update repository and software packages by running the following commands on your Ubuntu 18.04 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 and caching server. 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: enabled) Active: active (running) since Thu 2018-05-17 02:20:05 UTC; 2min 56s ago Docs: man:nginx(8) Main PID: 19851 (nginx) Tasks: 2 (limit: 2059) CGroup: /system.slice/nginx.service ├─19851 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; └─19853 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.14.0 (Ubuntu)
Now type in the public IP address of your Ubuntu 18.04 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 18.04 computer, then type 127.0.0.1
or localhost
in the browser address bar.
Note that some cloud server providers may block all incoming traffic by default, so if your browser can’t connect to Nginx web server, you may need to set firewall rules in your control panel.
Finally, we need to make www-data
(Nginx user) as the owner of web 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 18.04.
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 Thu 2018-05-17 02:39:57 UTC; 49s ago Main PID: 21595 (mysqld) Status: "Taking your SQL requests now..." Tasks: 27 (limit: 2059) CGroup: /system.slice/mariadb.service └─21595 /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 it is the default answer. )
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.29.
mariadb Ver 15.1 Distrib 10.1.29-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2
Step 4: Install PHP7.2
PHP7.2 is included in Ubuntu 18.04 repository and has a minor performance imrovement over PHP7.1. Enter the following command to install php7.2 and some common extensions.
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
PHP extensions are commonly needed for content management systems (CMS) like WordPress. For example, if your installation lacks php7.2-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.2-fpm.
sudo systemctl start php7.2-fpm
Enable auto-start at boot time.
sudo systemctl enable php7.2-fpm
Check status:
systemctl status php7.2-fpm
Sample output:
● php7.2-fpm.service - The PHP 7.2 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php7.2-fpm.service; enabled; vendor prese Active: active (running) since Thu 2018-05-17 03:04:46 UTC; 2min 43s ago Docs: man:php-fpm7.2(8) Main PID: 31922 (php-fpm7.2) Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/se Tasks: 3 (limit: 2059) CGroup: /system.slice/php7.2-fpm.service ├─31922 php-fpm: master process (/etc/php/7.2/fpm/php-fpm.conf) ├─31939 php-fpm: pool www └─31940 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. The following snippet will make Nginx listen on IPv4 port 80 and IPv6 port 80 with a catch-all server name.
server { listen 80; listen [::]:80; server_name _; root /usr/share/nginx/html/; index index.php index.html index.htm index.nginx-debian.html; location / { try_files $uri $uri/ /index.php; } location ~ \.php$ { fastcgi_pass unix:/run/php/php7.2-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; include snippets/fastcgi-php.conf; } # A long browser cache lifetime can speed up repeat visits to your page location ~* \.(jpg|jpeg|gif|png|webp|svg|woff|woff2|ttf|css|js|ico|xml)$ { access_log off; log_not_found off; expires 360d; } # disable access to hidden files location ~ /\.ht { access_log off; log_not_found off; 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.
Congrats! You have successfully installed Nginx, MariaDB and PHP7.2 on Ubuntu 18.04. For your server’s security, you should delete info.php
file now to prevent hacker seeing it.
sudo rm /usr/share/nginx/html/info.php
Next Step
You may want to install phpMyAdmin, which is a web-based admin panel for MySQL/MariaDB database server.
You can also install WordPress on top of the LEMP stack to create your own website or blog.
That’s it! I hope this tutorial helped you install LEMP stack on Ubuntu 18.04 LTS. As always, if you found this post useful, then subscribe to our free newsletter to get more tips and tricks. Take care.
good
Best ever.
You are welcome.
Thank you so much for this tutorial!
Clear, concise and best of all, worked without problem
Testing the validity of the nginx configuration file gives me an error. Can you help?
[email protected]:~# nginx -t
nginx: [emerg] invalid number of arguments in “fastcgi_param” directive in /etc/nginx/conf.d/default.conf:14
nginx: configuration file /etc/nginx/nginx.conf test failed
line 14 is:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
line 14 looks good. Perhaps the error is in previous line, like a missing semicolon.
Hi,
I’ve looked at a lot of different tutorials online and yours are by far the most clear and concise. I like that you explain what everything does in simple terms each step of the way.
Maybe this is obvious, but where you write ‘www-data’ above, that can be any name, right?
Nginx is running as the
www-data
user, which is defined in/etc/nginx/nginx.conf
file.output:
So you can’t use other name.
ok, thanks!
For step 3, I’m getting:
E: Unable to locate package mariadb-server
E: Unable to locate package mariadb-client
Any ideas?
mariadb-server and mariadb-client are in the default Ubuntu repository (universe). Make sure it’s enabled by executing the following command.
Then
No luck. I think it might be this: https://mariadb.com/kb/en/library/unable-to-install-mariadb-101-on-ubuntu-1804-after-apt-remove-purge-mysql/
I did have mysql installed previously, and today I started over with my whole server (reinstalled Ubuntu 18.04 from usb stick) because I was stuck and decided to follow your steps (also for Nextcloud).
Maybe I should reformat the drive, as I am only at the very beginning?
You can add the official MairaDB repository https://mariadb.com/kb/en/library/mariadb-package-repository-setup-and-usage/
Then install mariadb-server and mariadb-client.
If this doesn’t work, reinstall your Ubuntu 18.04 system.
Thank you – I’ll try that.
Thank you – I’ll try that.
Edit: that worked!
It’s weird – everything installed but when I type:
sudo mariadb -u root
it says:
sudo: mariadb: command not found.
Same with:
mariadb –version
It doesn’t seem to recognise mariadb as a command, even though mariadb is active and running.
Use mysql command.
That brings up Access denied for user ‘root’@;’localhost’ (using password: NO)
Then use the following command.
Provide the MariaDB root password.
Thank you, Xiao. That seems to work. For the ‘version’ command, I also have to type mysql –version instead of mariadb for it to work.
Thank you for your time!
Hi Xiao
Thank you for your excellent howtos … I have used a few over a long time now. I am having a problem with the php part. Specifically I am getting the message
So a little bit of hunting around and I have found this …
typing
returns the following …
So modules php7.2-fpm and php7.2-mbstring are apparently missing, so how do I get them?
php7.2-fpm and php7.2-mbstring are in the default Ubuntu 18.04 repository. https://packages.ubuntu.com/bionic/php7.2-fpm
There might be a problem with the repository mirror you are using.
Thank you Xiao
I am using a clean install of Ubuntu 18.01 which I have spun up on a VMware VM using an official sourced ISO to install.
here is my sources.list file in total …
I tried using us.archive. etc but that didn’t work, I tried adding a packages.ubuntu. etc but that didn’t work either. I seem to be missing something …
Can you please advise what I need to have in my sources.list file?
Thank you again.
hi, help me. after installation nextcloud on my ubuntu server 18.04. when i running on address bar with ip address. i found not success nextcloud it
Your sources.list file only contains the
main
component, you need to add theuniverse
component.mysite.com/lnfo.php 404 Not Found access.log
Hello, is there any tutorial on how to enable brotli compression on latest nginx server ?
Thanks a lot for your tutorial. Everything worked fine by just cutting and pasting the commands.
Hi, thanks for the tutorial, everthing seems to work except the nginx to nextcloud connection. I’m running a Ryzen3 local server on lan – no outside connection yet, I’m connected to 192.168.0.100 and get
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
I’ve edited /etc/nginx/conf.d/nextcloud.conf
and tried a few options
#listen 80;
listen 80 default_server;
listen [::]:80 default_server;
#server_name nextcloud.192.168.0.100;
#server_name 192.168.0.100/nextcloud;
server_name _;
But I can’t get to the nextcloud installer page – any ideas ?
Do you recommend to create separate spools owned by different users to separate the server blocks from each other?
I’m confused.
Every other guide I’ve seen for Ubuntu create new server block under
/etc/nginx/sites-available/
while this guide says
/etc/nginx/conf.d/
What is the difference?
/etc/nginx/sites-available/ is for storing the actual virtual host/server block file.
/etc/nginx/sites-enabled/ contains symbolic links to the /etc/nginx/available/ directory.
In /etc/nginx/nginx.conf file, you can find the following two lines.
which means virtual host/server block files in /etc/nginx/conf.d/ and /etc/nginx/sites-enabled/ directories will be loaded by Nginx.
If you create virtual host/server block file in /etc/nginx/sites-available/ directory, then you must create a symbolic link in /etc/nginx/sites-enabled/ directory. (This is a Debian/Ubuntu tradition.)
I personally prefer to use /etc/nginx/conf.d/ directory, because it’s easy to type and I don’t need to create symbolic links. The upstream Nginx package uses this directory by default.
I have come across a problem when i put a different server name and server, as ll it does is giving me a 502 bad gateway when i go to web.prueba.local. This is the edit i did to the server block :
I have a folder in /usr/share/prueba and it only has a index.php file
Apparently the problem comes from fastcgi_pass unix:/run/php/php7.2-fpm.sock; changed it to fastcgi_pass unix:/run/php7.2-fpm.sock; and is working now.
Im using mint 19, by the way.
Step 6: Test PHP
There are not instructions to open Ubuntu server port 80 to TCP traffic before testing server-ip-address/info.php in a browser.
Regards, Jeff
Opening ports, configuring firewalls and setting protection tools, managing DNS and setting up domains are not part of the scope of this guide… or any install LEMP/LAMP guide.
THANKS! 5/5 guide. 11/10 actually.
Had to use this guide because my server still runs this old as* ubuntu.
The guide is flawless. I used it 3 times with no errors in 2 different servers.
Now I’m going to check de nextcloud on 2018 guide.
To install PHP 7.2 on Ubuntu 22:
Add the Ondrej PPA to your system, which contains all versions of PHP packages for Ubuntu systems.: `LC_ALL=C.UTF-8 sudo add-apt-repository ppa:ondrej/php`
run `sudo apt update`
You should be able to install any PHP version