LEMP server on Ubuntu 20.04

2021.08.27  |  139

Installing a web server on Linux Nginx Mysql Php on Ubuntu 20.04

Install MySQL

apt install mysql-server
mysql_secure_installation
We must answer the first question with "N", otherwise there will be a problem with passwords due to checking their complexity.

Install PHP

apt install software-properties-common
add-apt-repository ppa:ondrej/php
apt install php7.4-fpm php7.4-mysql php7.4-mbstring php7.4-xml php7.4-gd
Set "open_short_tag = On" в /etc/php/7.4/fpm/php.ini
open_short_tag = On
We repeat several times with the replacement of the version if it is necessary to install several versions in parallel.

Installing NGINX

apt install nginx
Create a temporary self-signed certificate
sudo mkdir /etc/nginx/ssl
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/ssl/site.com.key -out /etc/nginx/ssl/site.com.crt

Create config nano /etc/nginx/sites-available/site.com.conf

server {
    listen *:80;
    server_name site.com www.site.com;
    return 301 https://$server_name$request_uri;
}

server {
    server_name site.com www.site.com;
    listen *:443 ssl;

    ssl_certificate /etc/nginx/ssl/site.com/site.com.pem;
    ssl_certificate_key /etc/www/nginx/ssl/site.com/site.com.key;

    root /var/www/site.com/public;

    location ~ .php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        client_max_body_size 128M;
        fastcgi_ignore_client_abort off;
    }

    include snippets/phpmyadmin.conf;
}
Create a link:
ln -s /etc/nginx/sites-avalible/og-studio.ru.conf /etc/nginx/sites-enabled/

Installing PhpMyAdmin

apt install phpmyadmin
Создаем файл /etc/nginx/snippets/phpmyadmin.conf со следующим содержимым:
location /phpmyadmin {
    root /usr/share/;
    index index.php index.html index.htm;
    location ~ ^/phpmyadmin/(.+\.php)$ {
        try_files $uri =404;
        root /usr/share/;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        fastcgi_param  PHP_VALUE mbstring.func_overload=0;

        include /etc/nginx/fastcgi_params;
    }

    location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
        root /usr/share/;
    }
}
Create a web site directory and assign an owner
mkdir -p /var/www/site.com/public
chown www-data:www-data /var/www -R
Change home directory, shell and create a password for www-data
usermod -d /var/www www-data
usermod --shell /bin/bash www-data
passwd www-data
Restart
shutdown -r 0
To work with the site, we connect under the www-data user using the sFTP protocol