Welcome to “The Ultimate Guide: Installing LEMP Stack on Ubuntu 22.04”. In this comprehensive guide, we will walk you through setting up a powerful web development environment using Ubuntu 22.04 as the base operating system and the LEMP stack (Linux, Nginx, MySQL, PHP) to serve your web applications.
LEMP is a popular software stack used for web development. It stands for:
- Linux: The operating system on which the other components are installed. It provides the foundation for the stack.
- Nginx: The web server in the LEMP stack. Nginx is known for its high performance, low memory usage, and efficient handling of concurrent connections. It’s often used to serve static content or as a reverse proxy.
- MySQL: The database management system used in the LEMP stack. MySQL is a popular open-source relational database that stores and retrieves data for your web applications.
- PHP: The programming language used for server-side scripting. PHP processes code on the server to generate dynamic content, which is then sent to the web browser
Prerequisites For Installing LEMP Stack on Ubuntu
Before we dive into Installing LEMP Stack on Ubuntu, let’s ensure you have everything you need:
- A machine running Ubuntu 22.04: Make sure you have a clean installation of Ubuntu 22.04 on your system.
- Superuser Privileges: You’ll need administrative privileges to install software and make system-level changes.
- Stable Internet Connection: This guide assumes you have a reliable internet connection to download necessary packages.
Step 1: Linux – Set up Ubuntu 22.04
We are not going to explain here, how to install Ubuntu 22.04, Here we are focusing on Installing LEMP Stack on Ubuntu and Assuming you have already installed Ubuntu 22.04.
Once you have Ubuntu 22.04 up and running, we can proceed to the next step for initial server setup(If you are in the local environment then skip this) go to this link and follow the steps.
Step 2: Installing Nginx
Nginx is a high-performance web server known for its speed and efficiency. To install it, open a terminal window and run the following commands:
sudi apt update
sudo apt install nginx
After the installation is complete, start the Nginx service and enable it to start on boot:
sudo systemctl start nginx
sudo systemctl enable nginx
The below steps are specific to the Installing LEMP Stack on Ubuntu (Server Like AWS) and do not apply to local setups, if you are local then skip the below steps.
Securing your Ubuntu 20.04 server with a firewall is crucial, just like with any server. Ubuntu 20.04 conveniently includes the preinstalled Uncomplicated Firewall (UFW) by default.
To enable traffic to Nginx, simply add rules to UFW allowing HTTP on port 80 and, if using SSL/TLS encryption, also permit HTTPS on port 443(Don’t use it in AWS).
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
To confirm the modification, you can inspect the status:
sudo ufw status
#OUTPUT
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
80/tcp ALLOW Anywhere
443/tcp ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
80/tcp (v6) ALLOW Anywhere (v6)
443/tcp (v6) ALLOW Anywhere (v6)
To verify that Nginx is running, open a web browser and enter your server’s IP address(local, type http://localhost) in the address bar. You should see the default Nginx welcome page.
Congratulations! You’ve successfully installed Nginx on Ubuntu 22.04. In the next step, for Installing LEMP Stack on Ubuntu we’ll tackle the installation of MySQL, the database management system.
Step 3: Installing MySQL
MySQL is a widely used open-source relational database management system. Follow these steps to install MySQL on your Ubuntu 22.04 system:
Install the MySQL server by running the following command:
sudo apt install mysql-server
When prompted, confirm installation by pressing Y
, and then ENTER
.
After completing the installation, it’s advisable to execute a built-in security script provided by MySQL. This script will eliminate potentially insecure default configurations and enhance the security of your database system. Initiate the interactive script by using the following command:
NOTE: Deciding to enable this feature depends on your discretion. When enabled, MySQL will reject passwords that do not meet the specified criteria. While it’s considered safe to leave validation disabled, it’s imperative to always employ robust and distinctive passwords for your database credentials
sudo mysql_secure_installation
#OUTPUT
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:
Skipping password set for root as authentication with auth_socket is used by default.
If you would like to use password authentication instead, this can be done with the "ALTER_USER" command.
See https://dev.mysql.com/doc/refman/8.0/en/alter-user.html#alter-user-password-management for more information.
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 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? (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 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.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y
Success.
All done!
To start the MySQL service and to enable the MySQL service to start automatically at boot we need to run the below command.
sudo systemctl start mysql
sudo systemctl enable mysql
Once you’ve completed the process, verify if you can successfully access the MySQL console:
kahnu@ubuntu-lemp:~$ sudo mysql
#OUTPUT
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.35-0ubuntu0.22.04.1 (Ubuntu)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
To exit from the MySQL console, need to run the below command :
mysql> exit
With MySQL successfully installed and secured, you now have a powerful database management system at your disposal. In the next step, we’ll install PHP, which will allow you to run dynamic web applications.
Step 4: Installing PHP
For Installing LEMP Stack on Ubuntu, we need PHP which is a popular server-side scripting language used for web development. Follow these steps to install PHP on your Ubuntu 22.04 system:
Run the following command to install PHP and common extensions:
sudo apt install php8.1-fpm php-mysql php-common php-cli
It will install various components associated with PHP version 8.1.
- php8.1-fpm: This installs the FastCGI Process Manager (FPM) for PHP version 8.1. FPM is a widely-used method for handling PHP requests in high-performance scenarios, often used in conjunction with web servers like Nginx or Apache
- php-mysql: This extension enables PHP to interact with MySQL databases, allowing you to perform database operations from PHP scripts.
- php-common: This package provides shared files and directories used by different PHP modules. It contains common files that are essential for the functioning of PHP.
- php-cli: This installs the PHP command-line interface (CLI). It allows you to run PHP scripts directly from the command line, which can be useful for tasks like scripting, automation, and running PHP scripts without the need for a web server.
Check the PHP Version in your terminal
kahnu@ubuntu-lemp:~$ php -v
#OUTPUT
PHP 8.1.2-1ubuntu2.14 (cli) (built: Aug 18 2023 11:41:11) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
with Zend OPcache v8.1.2-1ubuntu2.14, Copyright (c), by Zend Technologies
Congratulations! You now have PHP installed and running on your system. In the next step, we’ll configure Nginx to work with PHP.
Installing LEMP Stack on Ubuntu involves not just installing Nginx, MySQL, and PHP, but also configuring Nginx to effectively handle PHP files.
Configuring Nginx for PHP
To enable Nginx to process PHP files, you’ll need to configure it to work with PHP-FPM (FastCGI Process Manager). Follow these steps:
Open the Nginx default configuration file in a text editor. We’ll use nano in this example:
sudo nano /etc/nginx/sites-available/default
Locate the location ~ .php$ block and uncomment it by removing the # symbol at the beginning of each line.
Update the fastcgi_pass
directive to point to the PHP-FPM socket file.
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock; #Replace ypur php version
}
Validate the nginx configuration
sudo nginx -t
If there are no errors, you should see a message indicating that the configuration is okay.
kahnu@ubuntu-lemp:~$ sudo nginx -t
#OUTPUT
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Reload Nginx to apply the changes:
sudo systemctl reload nginx
With Nginx configured to work with PHP, you’re now ready to test the LEMP stack
To confirm the successful installation of PHP, generate a test PHP file. Utilize a text editor to create a file called info.php
in the default web directory:
sudo nano /var/www/html/info.php
Add the following content to the file:
<?php
phpinfo();
?>
Use ctrl + x, then Y, then Enter to save and close the file.
Open a web browser and navigate to http://your_server_ip/info.php
. You should see a page displaying PHP information.
Finally, we have set up our LEMP stack in our Ubuntu server.
As you can see, Installing LEMP Stack on Ubuntu 22.04 is an easy and straightforward process.
For more info about the LEMP stack you can check it – Here