the LEMP stack, which stands for Linux, Nginx, MySQL and PHP. In this post, I will show you the steps to install a LEMP stack on a Linux server, and also secure it with Let’s Encrypt SSL.
Make sure you have the following:
- A Linux server (Ubuntu 18.04 or 20.04 recommended)
- SSH access to your server as a root user or a user with sudo privileges
- A domain name pointed to your server IP address
Table of Contents:
- Step 1: Update the System
- Step 2: Install Nginx
- Step 3: Install MySQL
- Step 4: Install PHP and Configure Nginx to use PHP
- Step 5: Install Let’s Encrypt SSL
- Step 6: Test the Setup
- Conclusion
Step 1: Update the System
update the system packages.:
sudo apt-get update
sudo apt-get upgrade
Step 2: Install Nginx
sudo apt-get install nginx
Start the Nginx service:
sudo systemctl start nginx
To enable Nginx to start at boot time, run:
sudo systemctl enable nginx
Step 3: Install MySQL
sudo apt-get install mysql-server
Advertisements
Start the MySQL:
sudo systemctl start mysql
To enable MySQL with system boot:
sudo systemctl enable mysql
You can use these commands to create a database(optional):
sudo mysql -u root -p
CREATE DATABASE mydatabase;
CREATE USER 'mydbuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON mydatabase.* TO 'mydbuser'@'localhost';
FLUSH PRIVILEGES;
Make sure to replace the DB name, DB username, and password.
- database name: mydatabase.
- database username mydbuser
- database password: password
you can exit MySQL using:
exit;
Step 4: Install PHP and Configure Nginx to use PHP
sudo apt-get install php-fpm php-mysql
By default, Nginx does not know how to handle PHP files. We need to configure Nginx to pass the PHP requests to PHP-FPM. To do this, create a new server block configuration file:
sudo nano /etc/nginx/sites-available/example.com
Replace example.com
with your domain name. Copy and paste the following configuration:
server {
listen 80;
listen [::]:80;
root /var/www/example.com;
index index.php index.html index.htm;
server_name example.com;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Save and close the file.
Note: replace fastcgi_pass unix:/run/php/php7.4-fpm.sock;
with your php version. to check php version run php -v
Enable the site by creating a symbolic link to the sites-enabled directory:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
Test the Nginx configuration:
sudo nginx -t
If the test is successful, restart Nginx:
sudo systemctl restart nginx
Step 5: Install Let’s Encrypt SSL
To install Let’s Encrypt SSL:
sudo apt-get install certbot python3-certbot-nginx
Generate the SSL certificate:
sudo certbot --nginx -d example.com -d www.example.com
Replace example.com
with your domain name. Follow the prompts to configure the SSL certificate. Once the installation is complete, Nginx will automatically be configured to use SSL.
Step 6: Test the Setup
Open a web browser and navigate to your domain name using HTTPS. You should see a lock icon in the address bar indicating that the website is secured with SSL.
In Step 5, we have mentioned the directory /var/www/example.com
as the root directory for the website. This is just an example directory and you can replace it with the actual directory where your website files are located.
If you haven’t yet created the directory for your website files, you can create it using the following command:
sudo mkdir -p /var/www/example.com
Replace example.com
with your actual domain name.
Advertisements
Once you have created the directory, you can copy your website files to this directory.
After copying the files, you can set the ownership and permissions for the files using:
sudo chown -R www-data:www-data /var/www/example.com
sudo chmod -R 755 /var/www/example.com
These commands set the ownership of the files to the www-data user and group, which is the user and group used by Nginx, and set the permissions to 755, which allows Nginx to access the files.
Conclusion
In this post, I have shown the steps to install a LEMP stack on a Linux server and secure it with Let’s Encrypt SSL. If you have any questions about this post please comment…