Introduction:
The LEMP stack, which stands for Linux, Nginx, MySQL (or MariaDB), and PHP, is a popular web server configuration for hosting dynamic websites and web applications. In this guide, we’ll walk you through the steps to install and configure a LEMP stack on an Ubuntu 22.04 server.
Prerequisites:
Before you begin, ensure that you have the following:
- An Ubuntu 22.04 server with root or sudo access.
- A secure and up-to-date server environment.
- Basic knowledge of the Linux command line.
Step 1: Update the System:
Start by updating the package list and upgrading installed packages to their latest versions:
sudo apt update
sudo apt upgrade
Step 2: Install Nginx:
Nginx is a high-performance web server that will serve as the foundation of our LEMP stack. Install it with the following command:
sudo apt install nginx
Step 3: Configure Nginx:
Start Nginx and enable it to start at boot:
sudo systemctl start nginx
sudo systemctl enable nginx
You can test the Nginx configuration with:
sudo nginx -t
Step 4: Install MySQL (or MariaDB):
Choose either MySQL or MariaDB as your database management system. Install MySQL with:
sudo apt install mysql-server
During the installation, you’ll be prompted to set a root password. Make it secure.
Step 5: Secure MySQL (Optional):
Run the MySQL security script to enhance security:
sudo mysql_secure_installation
Follow the on-screen instructions, and consider enabling the “VALIDATE PASSWORD COMPONENT” for better password security.
Step 6: Install PHP:
Install PHP and required extensions:
sudo apt install php-fpm php-mysql
Step 7: Configure PHP:
Edit the PHP configuration file:
sudo nano /etc/php/7.4/fpm/php.ini
Make sure the following settings are configured:
cgi.fix_pathinfo=0
memory_limit=256M
upload_max_filesize=64M
post_max_size=64M
Save and close the file. Then, restart the PHP-FPM service:
sudo systemctl restart php7.4-fpm
Step 8: Create an Nginx Server Block:
Create an Nginx server block (virtual host) for your website. You can create a configuration file in the /etc/nginx/sites-available/
directory and create a symbolic link in the /etc/nginx/sites-enabled/
directory.
Here’s a basic example:
sudo nano /etc/nginx/sites-available/your_domain
server {
listen 80;
server_name your_domain.com www.your_domain.com;
root /var/www/your_domain/public_html;
index index.php index.html;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location / {
try_files $uri $uri/ =404;
}
}
Activate the server block:
sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
Step 9: Test Configuration and Reload Nginx:
Test your Nginx configuration:
sudo nginx -t
If the test is successful, reload Nginx to apply the changes:
sudo systemctl reload nginx
Step 10: Create Web Content:
Create your website’s root directory and add your web content:
sudo mkdir -p /var/www/your_domain/public_html
sudo chown -R www-data:www-data /var/www/your_domain
Step 11: Test Your LEMP Stack:
Create a simple PHP file in your website’s root directory to test your LEMP stack:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/your_domain/public_html/info.php
You can then access this file in your web browser to confirm that PHP is working correctly.
Conclusion:
You have successfully installed and configured a LEMP stack (Linux, Nginx, MySQL/MariaDB, and PHP) on your Ubuntu 22.04 server. You can now deploy and host your web applications or websites with this powerful web server stack. Make sure to secure your server, keep your software up to date, and regularly back up your data for a robust web hosting environment.