In the world of web development and server management, setting up a reliable infrastructure is essential for hosting dynamic websites and applications efficiently. One popular configuration for achieving this is the LEMP stack, which comprises Linux, Nginx, MySQL (or MariaDB), and PHP. In this guide, we’ll walk you through the step-by-step process of installing and configuring a LEMP stack on an Ubuntu 20.04 server.
Step 1: Update and Upgrade Ubuntu
Before we begin, it’s crucial to ensure that your Ubuntu 20.04 server is up to date. Open a terminal and run the following commands:
sudo apt update
sudo apt upgrade
These commands will update the package lists and upgrade any outdated packages to their latest versions, ensuring system stability and security.
Step 2: Install Nginx
Nginx is a powerful web server that will serve as the backbone of our LEMP stack. Install Nginx using the following command:
sudo apt install nginx
Once the installation is complete, start the Nginx service and enable it to start on boot:
sudo systemctl start nginx
sudo systemctl enable nginx
You can verify that Nginx is running by accessing your server’s IP address in a web browser. You should see the default Nginx landing page.
Step 3: Install MySQL (or MariaDB)
MySQL or MariaDB will serve as the database management system for storing and managing data. Choose either MySQL or MariaDB for this step. To install MySQL, use the following command:
sudo apt install mysql-server
During the installation process, you will be prompted to set a root password for MySQL. Make sure to choose a strong password and keep it secure.
Alternatively, if you prefer to use MariaDB, you can install it with the following command:
sudo apt install mariadb-server
Like MySQL, you will be prompted to set a root password during the installation process.
Step 4: Install PHP
PHP is a server-side scripting language used for creating dynamic web pages. Install PHP along with some commonly used extensions:
sudo apt install php-fpm php-mysql
Once PHP is installed, you’ll need to configure Nginx to use PHP to process PHP files. Open the default Nginx configuration file:
sudo nano /etc/nginx/sites-available/default
Find the following block:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
Uncomment the fastcgi_pass
line and update the PHP version if necessary. Save the file and exit the editor.
Restart Nginx for the changes to take effect:
sudo systemctl restart nginx
Step 5: Test PHP Processing
Create a test PHP file to ensure that PHP is correctly configured:
sudo nano /var/www/html/info.php
Add the following line to the file:
<?php phpinfo(); ?>
Save the file and exit the editor. Now, access this file in a web browser by navigating to http://your_server_ip/info.php
. You should see the PHP info page displaying information about your PHP installation.
Conclusion
You’ve now successfully configured a LEMP stack on your Ubuntu 20.04 server. This setup provides a solid foundation for hosting dynamic websites and applications. Remember to prioritize security by following best practices and keeping your server software up to date to protect against potential vulnerabilities.