LEMP is a collection of four open-source components used together to host websites and web applications on the Internet. LEMP stands for L for Linux operating system, N for Nginx web server, M for MySQL or MariaDB database, and P for PHP language. It is generally used to host highly-scaled applications on the Internet. The LEMP stack is well supported by the community and used by developers worldwide.
This article will show you how to install the LEMP server on Ubuntu 22.04.
Requirements
- A server running Ubuntu 22.04.
- At least 1 GB RAM and 5 GB hard disk.
- A root password is set up on the server.
Installing Nginx on Ubuntu 22.04
The web server package Nginx is included by default in the Ubuntu 22.04 standard repository. You can easily install it with the following command:
apt-get install nginx -y
Once the Nginx web server package is installed, start the Nginx service and activate it with the following command so that it starts on system reboot:
systemctl start nginx systemctl enable nginx
To check the status of Nginx, run the following command:
systemctl status nginx
You will receive the following 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 Fri 2022-04-29 07:47:01 UTC; 17s ago Docs: man:nginx(8) Process: 11919 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Process: 11920 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Main PID: 12009 (nginx) Tasks: 3 (limit: 4630) Memory: 3.3M CPU: 55ms CGroup: /system.slice/nginx.service ??12009 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;" ??12011 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ??12012 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" Apr 29 07:47:01 ubuntu systemd[1]: Starting A high performance web server and a reverse proxy server... Apr 29 07:47:01 ubuntu systemd[1]: Started A high performance web server and a reverse proxy server.
To check the Nginx version, execute the following command:
nginx -V
You will get the following output:
nginx version: nginx/1.18.0 (Ubuntu) built with OpenSSL 3.0.2 15 Mar 2022 TLS SNI support enabled configure arguments: --with-cc-opt='-g -O2 -ffile-prefix-map=/build/nginx-9P0wNJ/nginx-1.18.0=. -flto=auto -ffat-lto-objects -flto=auto -ffat-lto-objects -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -flto=auto -ffat-lto-objects -flto=auto -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-compat --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --add-dynamic-module=/build/nginx-9P0wNJ/nginx-1.18.0/debian/modules/http-geoip2 --with-http_addition_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_sub_module
You can also check the Nginx installation via the web browser at the URL http://your-server-ip . You should see the Nginx test page on the following screen:
Installing PHP and PHP-FPM on Ubuntu 22.04
PHP is a processing language used for serving PHP pages. By default, the latest PHP version is included in the standard Ubuntu 22.04 repository. You can install it along with other extensions using the following command:
apt-get install php8.1 php8.1-fpm php8.1-mysql php8.1-gd php8.1-curl php8.1-xml -y
After installing PHP, start the PHP-FPM service with the following command:
systemctl start php8.1-fpm
To check the status of PHP-FPM, run the following command:
systemctl status php8.1-fpm
You will get the following output:
? php8.1-fpm.service - The PHP 8.1 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php8.1-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2022-04-29 07:48:26 UTC; 2min 8s ago Docs: man:php-fpm8.1(8) Process: 13491 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/8.1/fpm/pool.d/www.conf 81 (code=e> Main PID: 13488 (php-fpm8.1) Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec" Tasks: 3 (limit: 4630) Memory: 9.2M CPU: 116ms CGroup: /system.slice/php8.1-fpm.service ??13488 "php-fpm: master process (/etc/php/8.1/fpm/php-fpm.conf)" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""> ??13489 "php-fpm: pool www" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "> ??13490 "php-fpm: pool www" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "> Apr 29 07:48:26 ubuntu systemd[1]: Starting The PHP 8.1 FastCGI Process Manager... Apr 29 07:48:26 ubuntu systemd[1]: Started The PHP 8.1 FastCGI Process Manager.
Install MySQL Server on Ubuntu 22.04
MySQL is a database management system that can be used as a database backend for any web application. By default, the latest version of MySQL is included in the default repository of Ubuntu 22.04. You can install it with the following command:
apt-get install mysql-server -y
Once MySQL is installed, start and activate the MySQL service with the following command:
systemctl start mysql systemctl enable mysql
You can also check the MySQL status with the following command:
systemctl status mysql
You should see the following output:
? mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2022-04-29 07:43:21 UTC; 5min ago Main PID: 11476 (mysqld) Status: "Server is operational" Tasks: 38 (limit: 4630) Memory: 358.7M CPU: 2.266s CGroup: /system.slice/mysql.service ??11476 /usr/sbin/mysqld Apr 29 07:43:20 ubuntu systemd[1]: Starting MySQL Community Server... Apr 29 07:43:21 ubuntu systemd[1]: Started MySQL Community Server.
You can also check the MySQL version with the following command:
mysql --version
You will get the following output:
mysql Ver 8.0.28-0ubuntu4 for Linux on x86_64 ((Ubuntu))
By default, MySQL is not secured and the root password is not set. So you need to secure it by running the following script:
mysql_secure_installation
Set a new MySQL password as shown below:
Securing the MySQL server deployment. Connecting to MySQL using a blank password. VALIDATE PASSWORD COMPONENT can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD component? Press y|Y for Yes, any other key for No: Please set the password for root here. New password: Re-enter new password: By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment.
Remove the Anonymous user (see below):
Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y Success. Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network.
Disallow root login remotely (see below):
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y Success. By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment.
Remove the test database (see below):
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y - Dropping test database... Success. - Removing privileges on test database... Success. Reloading the privilege tables will ensure that all changes made so far will take effect immediately.
Reset the permissions as shown below:
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y Success. All done!
Check the PHP installation
Nginx uses PHP-FPM to process the PHP pages. Therefore, you need to edit the default configuration file of Nginx for the virtual host and define PHP-FPM sock to serve the PHP page:
Edit the default Nginx configuration file with the following command:
nano /etc/nginx/sites-enabled/default
Remove all lines and add the following lines:
server { listen 80; root /var/www/html; index info.php; server_name _; location / { try_files $uri $uri/ =404; } location ~ .php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.1-fpm.sock; } }
Save and close the file and check Nginx for syntax errors with the following command:
nginx -t
You will get the following output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Next, restart the Nginx service to apply the changes:
systemctl restart nginx
Now create a sample PHP information page in the Nginx web root directory:
nano /var/www/html/info.php
Paste the following code:
Save and close the file, then open your web browser and access the PHP page via the URL http://your-server-ip . You should see the PHP information page on the following screen:
Conclusion
Congratulations! You have successfully installed the LEMP server on Ubuntu 22.04. Now you can use the LEMP server in the development environment to create, test and deploy the web application. If you have any further questions, please feel free to contact me.