2 min read

LEMP server - Ubuntu 20.04 LTE

LEMP server - Ubuntu 20.04 LTE

A very simple tutorial how to create your first web server with Linux, Nginx, MySQL and PHP.\

  • I use as a text editor 'vim', you can use what everu you wat.\
  • As the default firewall for you server I will be using here a program calleg 'ufw', again you can use what ever you like more.

Get redy, stedy, GO...


Step 1 – Installing the Nginx Web Server

sudo apt update && sudo apt install ufw vim -y && sudo apt upgrade -y
sudo apt install nginx
sudo apt install curl


Let's get ufw to work properly on our machine:

sudo ufw allow 'Nginx Full'
sudo ufw allow 22


Check ufw:

sudo ufw status


You should see this:

Output
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
Nginx HTTP                 ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
Nginx HTTP (v6)            ALLOW       Anywhere (v6)


Go to http://server_domain_or_IP to check is your Nginx server is working.


Step 2 — Installing MySQL
Time to install MySQL or Mariadb, depending on what aplication you will be using, just make sure that the aplication that you will be running on the web server support MySQL 8 and higher, if not use mariadb:

sudo apt-get install mysql-server mysql-client libmysqlclient-dev

OR

sudo apt install mariadb-server


Set the security of MySQL, follow instaruction on screen:

sudo mysql_secure_installation


Now set up what ever databases you need or users, again depending on what ever aplication you will need.
To log into mysql type:

sudo mysql -u root -p



Step 3 – Installing PHP
Before you start installing this, you will need to check what is the best PHP version for you. One more time, depending on what type of aplication you will be using will require from you a difrent version of PHP to have installed. We will install here php 7.4, but you install what ever wersion you need:

sudo apt install php-fpm php-mysql

Optional component's:

sudo apt install php-xml php7.4-gd php7.4-xml php7.4-zip php7.4-imagick php7.4-dom php7.4-curl 



Step 4 — Configuring Nginx to Use the PHP Processor
Will will use the default location ('/var/www/html') for web on Linux here, but just bear in mind, that you can create what ever folder you want where ever you want on the machine to host you files from it. Let's create a config file in nginx to get you rdommain running on your machine:

sudo vim /etc/nginx/sites-available/your_domain_name.conf

Replace the 'your_domain_name' with the name of your domain that you want to use.
This will create a new config file in the nginx location, paste this into it:

server {
    listen 80;
    server_name your_domain_name www.your_domain_name;
    root /var/www/html;

    index index.html index.htm index.php;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
     }

    location ~ /\.ht {
        deny all;
    }

}


Activate your nginx configuration:

sudo ln -s /etc/nginx/sites-available/your_domain_name.conf /etc/nginx/sites-enabled/


Test your configuration:

sudo nginx -t


If all is OKAY, then:

sudo systemctl reload nginx


The alst element if to enable rewrite on your domain:
Edit '/etc/nginx/site-avaliable/your_domain_name.conf'
Change this:

    location / {
        try_files $uri $uri/ =404;
    }

to

    location / {
        try_files $uri $uri/ /index.php?$args;
    }



Tha's it! Your web serevr runs. Create some magic now:)

Enjoy!