Prerequisites
- VPS with Ubuntu 22.04 installed. Need a VPS? Try Web Host Wizards!
- PC/Mac Terminal
Getting Started
- Open your terminal
- Type 'ssh root@yourserverip' replacing 'yourserverip' with your servers IP address.
ssh root@yourserverip
When you first login to a new server you should first update & upgrade to the latest Ubuntu version and packages. You can do so using the code below.
sudo apt update && apt upgrade
A message will then popup asking 'Do you want to continue? [Y/n]'
Type Y and press enter.
We can now move on to installing the LEMP stack.
Type Y and press enter.
We can now move on to installing the LEMP stack.
Installing LEMP Stack on Ubuntu Process
1. Installing NGINX
We can use the below code to install Nginx.
sudo apt install nginx
When the installation is complete we can then enable Nginx to run on server boot, this way it will start automatically if the server is ever restarted or shutdown.
sudo systemctl enable nginx
Now we startup Nginx
sudo systemctl start nginx
We can now check the status of Nginx to make sure its running.
sudo systemctl status nginx
Lastly we need to allow permissons for non root users.
sudo chown www-data:www-data /usr/share/nginx/html -R
With Nginx now up and running, you can type your IP (http://youripaddress) into the browser address bar which will bring up the Nginx welcome screen. This confirms Nginx is now configured and running on the server.
2. Installing MariaDB
First lets install MariaDB with the following code
sudo apt install mariadb-server mariadb-client
Now we can start MariaDb.
sudo systemctl start mariadb
Input the following so MariaDB starts automatically on server boot.
sudo systemctl enable mariadb
It is recommended to run the following security script for MariaDB
sudo mysql_secure_installation
The script will execute and you will need to follow the prompts
'Enter current password for root (enter for none)'
Press Enter.
Press Enter.
'Switch to Unix_socket authentication Y/n'
Press 'n' on the keyboard and then Enter.
Press 'n' on the keyboard and then Enter.
'Change the root password?'
Press 'n' on the keyboard and then Enter.
Press 'n' on the keyboard and then Enter.
Now press the enter key for every prompt until you see 'All done! If you've completed all of the above steps, your MariaDB installation should now be secure.'
Lastly we can add the root user to the database
sudo mariadb -u root
And exit
exit;
3. Installing PHP 8.1
First we check for system updates
sudo apt update && apt upgrade
Now we can install PHP 8.1
sudo sudo apt install php8.1 php8.1-fpm php8.1-mysql php-common php8.1-cli php8.1-common php8.1-opcache php8.1-readline php8.1-mbstring php8.1-xml php8.1-gd php8.1-curl
Start PHP
sudo systemctl start php8.1-fpm
Configure PHP to start automatically on reboot/server startup
sudo systemctl enable php8.1-fpm
4. Configuring Nginx
Remove default Nginx server block
sudo rm /etc/nginx/sites-enabled/default
Create a new .conf file
sudo nano /etc/nginx/conf.d/default.conf
Add the following code into the newly created file. Press ctrl + x to save and exit.
sudo server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html/;
index index.php index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
include snippets/fastcgi-php.conf;
}
# A long browser cache lifetime can speed up repeat visits to your page
location ~* \.(jpg|jpeg|gif|png|webp|svg|woff|woff2|ttf|css|js|ico|xml)$ {
access_log off;
log_not_found off;
expires 360d;
}
# disable access to hidden files
location ~ /\.ht {
access_log off;
log_not_found off;
deny all;
}
}
Test the new Nginx configuration
sudo nginx -t
Reload Nginx
sudo systemctl reload nginx
Reboot the server to save our new configuration
sudo reboot
Once the server has rebooted, you will need to log in using SSH via the terminal once again.
Congratulations the LEMP stack on Ubuntu is now installed & configured.
Congratulations the LEMP stack on Ubuntu is now installed & configured.