Monday, August 3, 2020

How To install LEMP Stack With Server Blocks ( Virtual Host ) On Ubuntu 20.04

Video Tutorial -

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

LEMP software stack is a group of open-source software that is installed together to enable a server to host websites and apps. It is an acronym for Linux, ENginx server, MySQL, and PHP.


Nginx Server Blocks allows you to run more than one website on a single machine. With Server Blocks, you can specify the site document root (the directory which contains the website files), create a separate security policy for each site, use different SSL certificates for each site, and much more.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Testing Environment: Ubuntu 20.04 LTS          Hostname - www.yourdomain.com  - ip Address - 192.168.1.10

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

apt update ; apt install -y build-essential net-tools curl git software-properties-common neofetch


apt update ; apt install nginx mysql-server mysql-client php-fpm php-cli php-curl php-mysql php-curl php-gd php-mbstring php-pear php-cgi php-imap php-intl php-snmp php-tidy php-zip php-mbstring php-json php-pgsql php-opcache php-mysql php-ldap php-bz2 php-xml phpmyadmin -y


mysql_secure_installation

systemctl enable nginx mysql php7.4-fpm ; systemctl start nginx mysql php7.4-fpm


timedatectl

sed -i "s/;date.timezone.*/date.timezone = Asia\/\Kolkata/" /etc/php/*/fpm/php.ini

sed -i "s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/" /etc/php/*/fpm/php.ini


nano /etc/nginx/sites-enabled/default

server {

  listen 80;

  listen [::]:80;

  server_name www.yourdomain.com;

  root /var/www/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/php7.4-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;

  }

}


nginx -t

ln -s  /usr/share/phpmyadmin /var/www/html/phpmyadmin

chmod 775 -R /usr/share/phpmyadmin/ ; chown www-data:www-data -R /usr/share/phpmyadmin/


rm -rf /var/www/html/index.nginx-debian.html

echo '<?php phpinfo(); ?>' > /var/www/html/info.php ; echo "This is Test Page on www.yourdomain.com" > /var/www/html/index.html


mysql -u root -p

create user Test@'localhost' identified by 'StrongPassword';

grant all privileges on *.* to Test@'localhost';

flush privileges;

exit


echo "192.168.1.10 www.yourdomain.com www" >> /etc/hosts

systemctl reload nginx  php7.4-fpm


http://www.yourdomain.com/ | http://www.yourdomain.com/info.php | http://www.yourdomain.com/phpmyadmin

______________________________________________________________________________________________________________

Set Up Nginx Server Blocks (Virtual Hosts) -

______________________________________________________________________________________________________________

nano /etc/nginx/nginx.conf

server_names_hash_bucket_size 64;


mkdir -p /var/www/serverblock.com/html ; chmod -R 755 /var/www ; chown -R $USER:$USER /var/www/serverblock.com/html


echo '<?php phpinfo(); ?>' > /var/www/serverblock.com/html/info.php ; echo "This is Test Page on www.serverblock.com" > /var/www/serverblock.com/html/index.html


nano /etc/nginx/sites-available/serverblock.com

server {

  listen 80;

  listen [::]:80;

  server_name www.serverblock.com;

  root /var/www/serverblock.com/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/php7.4-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;

  }

}

nginx -t

ln -s /etc/nginx/sites-available/serverblock.com /etc/nginx/sites-enabled/

echo "192.168.1.10 www.serverblock.com www" >> /etc/hosts

systemctl reload nginx php7.4-fpm

http://www.serverblock.com/ | http://www.serverblock.com/info.php


Optional Step -  Download Free Website Templates & Test -

https://freewebsitetemplates.com/templates/page-2

rm -rf /var/www/serverblock.com/html/index.html info.php

cp -r /home/ubuntu/Downloads/ecologicalwebsitetemplate/* /var/www/serverblock.com/html/

systemctl reload nginx php7.4-fpm

http://www.serverblock.com/

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


No comments:

Post a Comment