Link Search Menu Expand Document

LEMP Stack - Linux, Nginx, MySQL and PHP

Table of contents


  1. Using SSH key, read on How to generate SSH on Mac.
  2. Knowing and change user permission, read on User & Permission.
  3. For micro image size, read on SwapFile.

1. Simple update and upgrade

apt update & apt upgrade 

2. Nginx Web Server

apt install nginx 
systemctl start nginx 
systemctl status nginx 
  # checking the version `nginx -v`

3. UFW - Firewall

  • Add allow rules

    ufw enable
    ufw default deny incoming
    ufw default allow outgoing
    ufw allow 'Nginx Full'
    ufw allow OpenSSH
    ufw status
    ufw app list
    chown -R www-data:www-data /usr/share/nginx/html
  • Delete allow rules

    ufw delete allow 'Nginx Full'

4. MySQL

Read on MySQL for Beginners

apt install mysql-server

5. PHP7.4

More on PHP Config

apt install php7.4 php7.4-fpm php7.4-mysql php-common php7.4-cli php7.4-common php7.4-json php7.4-opcache php7.4-readline php7.4-mbstring php7.4-xml php7.4-gd php7.4-curl
systemctl start php7.4-fpm
systemctl status php7.4-fpm

6. Create an Nginx Server Block

  • Run rm /etc/nginx/sites-enabled/default
  • Create a brand new server block file under this /etc/nginx/conf.d/ directory. Server Block Sample Here

      nano /etc/nginx/conf.d/default.conf
      nginx -t
      systemctl reload nginx

7. Add a site

vim /etc/nginx/sites-available/
  • Add server content to the above See default server content
  • Run ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled
  • Then, nginx -t and systemctl reload nginx
  • Done

8. Test PHP

vim /var/www/html/info.php 
  • Paste <?php phpinfo(); ?> then save.
nginx -t
systemctl reload nginx
  # must reload before checking online, otherwise browser would start download
  • Check curl
  • Remove the info.php after it’s running successfully
rm /var/www/html/info.php

Troubleshoot tip

Log: /var/log/nginx/error.log
systemctl restart nginx

Setup auto restart

mkdir -p /etc/systemd/system/nginx.service.d/
  # -p means --parent
vim /etc/systemd/system/nginx.service.d/restart.conf
  # Add below 3 lines. 
  # --------------------#
  # [Service]           #
  # Restart=always      #
  # RestartSec=5s       #
  # --------------------#
  # This file `restart.conf` is to set Nginx auto restart 
systemctl daemon-reload
pkill nginx
systemctl status nginx