Ubuntu srever edition is such a stable system that many people use it as an operating system for servers. These servers deploy a lot of different network services. However, we can also have a light web server using LEMP. LEMP is another software stack quite popular with web developers. So, in this post, you will learn how to install LEMP on Ubuntu 20.04.
Differences between LAMP and LEMP
Recently we have presented a post where we taught how to install LAMP on Ubuntu 20.04 and now we are with LEMP.
If you are a beginner who is getting started with this, you may be confused and wondering what the difference is between the two. The only big difference is that LAMP includes the Apache web server and LEMP includes Nginx.
The difference between the two is a bit more profound but both are Unix web servers that serve up websites. Both overwhelmingly dominate the market but Nginx is faster and this makes it the preferred choice for many sysadmins.
Install LEMP on Ubuntu 20.04
Install Nginx on Ubuntu 20.04
As I mentioned before, Nginx is an open-source web server that competes directly against Apache. However, it sacrifices a bit the ease of use, but it is faster and more efficient in the consumption of computer resources.
Like Apache it is included in the official repositories of the distribution, so to install it you just need to open a terminal and run it:
:~$ sudo apt install nginx Reading package lists… Done Building dependency tree Reading state information… Done The following additional packages will be installed: fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0 libjpeg-turbo8 libjpeg8 libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream libtiff5 libwebp6 libxpm4 nginx-common nginx-core Suggested packages: libgd-tools fcgiwrap nginx-doc ssl-cert The following NEW packages will be installed: fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0 libjpeg-turbo8 libjpeg8 libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream libtiff5 libwebp6 libxpm4 nginx nginx-common nginx-core 0 upgraded, 17 newly installed, 0 to remove and 0 not upgraded. Need to get 2431 kB of archives. After this operation, 7891 kB of additional disk space will be used. Do you want to continue? [Y/n]
Like any other service on the system, Ubuntu will automatically start it and enable it to be started during system load.
Check the status of the service to see if it is working properly:
:~$ sudo systemctl status nginx ● 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 Mon 2020-04-27 14:32:43 UTC; 3min 7s ago Docs: man:nginx(8) Main PID: 1409 (nginx) Tasks: 2 (limit: 506) Memory: 4.5M CGroup: /system.slice/nginx.service ├─1409 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; └─1410 nginx: worker process Apr 27 14:32:43 osradar systemd[1]: Starting A high performance web server and a reverse proxy server… Apr 27 14:32:43 osradar systemd[1]: Started A high performance web server and a reverse proxy server.
Everything’s fine so far. Open a web browser and go to http://Server-IP
or http://your-domain
or http://localhost
and you should see something like this:
So, now we can continue.
Install PHP on Ubuntu 20.04
Both Apache and Nginx are large web servers, but by themselves, they cannot process PHP files. So you have to install the PHP language and do some configuration later.
First, install the PHP language and some modules with the following command:
:~$ sudo apt install php php-cli php-xml php-mbstring php-mysql php7.4-fpm Reading package lists… Done Building dependency tree Reading state information… Done The following additional packages will be installed: libonig5 php-common php7.4 php7.4-cli php7.4-common php7.4-json php7.4-mbstring php7.4-mysql php7.4-opcache php7.4-readline php7.4-xml Suggested packages: php-pear The following NEW packages will be installed: libonig5 php php-cli php-common php-mbstring php-mysql php-xml php7.4 php7.4-cli php7.4-common php7.4-fpm php7.4-json php7.4-mbstring php7.4-mysql php7.4-opcache php7.4-readline php7.4-xml 0 upgraded, 17 newly installed, 0 to remove and 0 not upgraded. Need to get 4852 kB of archives. After this operation, 20.8 MB of additional disk space will be used. Do you want to continue? [Y/n]
Then you have to make some changes to the Nginx configuration to make it work with PHP.
So, open the default Virtuahost configuration file for Nginx. This file affects all the files in the Nginx root directory, which is /var/www/html/
:~$ sudo nano /etc/nginx/sites-available/default
Find the line where the files are that Nginx takes as an index and adds index.php to that line. It will look like this:
index.html index.htm index.nginx-debian.html index.php;
Below, edit the configuration for PHP files. It should look like this:
location ~ .php$ { include snippets/fastcgi-php.conf; # # With php-fpm (or other unix sockets): fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # With php-cgi (or other tcp sockets): # fastcgi_pass 127.0.0.1:9000; }
Save the changes and close the file.
After that, a change must be made in the PHP configuration file to indicate that it will work with Nginx. I am specifically referring to FPM.
:~$ sudo nano /etc/php/7.4/fpm/php.ini
And leave this line like this:
cgi.fix_pathinfo=0
Save the changes and close the file.
To apply the changes, restart the Nginx services as of PHP-fpm which is the processor that Nginx will use.
:~$ sudo systemctl reload nginx :~$ sudo systemctl reload php7.4-fpm
Now we have to test that everything is fine by creating a new PHP file with some code.
:~$ sudo nano /var/www/html/test.php <?php phpinfo(); ?>
Again, save the changes and close the file.
Now, open it using the web browser.
Install MariaDB
MariaDB is a database manager which is also a fork with MySQL. They are highly compatible although they also have marked differences.
With MariaDB, you will have a program capable of storing data in a fast, secure, and reliable way.
As with Nginx and PHP, MariaDB can be installed from the distribution’s official repositories. The best thing is that today, there is version 10.3 which is one of the most recent ones.
Well, to install MariaDB run the following command:
:~$ sudo apt install mariadb-server Reading package lists… Done Building dependency tree Reading state information… Done The following additional packages will be installed: galera-3 libcgi-fast-perl libcgi-pm-perl libconfig-inifiles-perl libdbd-mysql-perl libdbi-perl libencode-locale-perl libfcgi-perl libhtml-parser-perl libhtml-tagset-perl libhtml-template-perl libhttp-date-perl libhttp-message-perl libio-html-perl liblwp-mediatypes-perl libmysqlclient21 libsnappy1v5 libterm-readkey-perl libtimedate-perl liburi-perl mariadb-client-10.3 mariadb-client-core-10.3 mariadb-common mariadb-server-10.3 mariadb-server-core-10.3 mysql-common socat Suggested packages: libclone-perl libmldbm-perl libnet-daemon-perl libsql-statement-perl libdata-dump-perl libipc-sharedcache-perl libwww-perl mailx mariadb-test tinyca The following NEW packages will be installed: galera-3 libcgi-fast-perl libcgi-pm-perl libconfig-inifiles-perl libdbd-mysql-perl libdbi-perl libencode-locale-perl libfcgi-perl libhtml-parser-perl libhtml-tagset-perl libhtml-template-perl libhttp-date-perl libhttp-message-perl libio-html-perl liblwp-mediatypes-perl libmysqlclient21 libsnappy1v5 libterm-readkey-perl libtimedate-perl liburi-perl mariadb-client-10.3 mariadb-client-core-10.3 mariadb-common mariadb-server mariadb-server-10.3 mariadb-server-core-10.3 mysql-common socat 0 upgraded, 28 newly installed, 0 to remove and 0 not upgraded. Need to get 21.1 MB of archives. After this operation, 173 MB of additional disk space will be used. Do you want to continue? [Y/n]
Then, a password must be set for the root user. This can be done with the help of the mysql_secure_installation script
:~$ sudo mysql_secure_installation
As soon as you log in you will be asked for the root password, but since it is not created you only have to press enter.
Then you will be asked the following questions:
Remove anonymous users? [Y/n] Disallow root login remotely? [Y/n] Remove test database and access to it? [Y/n] Reload privilege tables now? [Y/n]
Answer yes to all of them to ensure the installation and finish the tutorial.
Now yes, LEMP is correctly installed in your server with Ubuntu 20.04.
So, share this post and join our Telegram Channel.