ก๊วนซอฟท์แวร์ </softganz> SoftGang (Gang Software)

Web &amp; Software Developer Gang.

nginx - ถึงเวลาซะที

by Little Bear @22 ธ.ค. 66 14:19 ( IP : 171...124 ) | Tags : nginx , PHP

Step 1 – Installing the Nginx Web Server

sudo apt update
sudo apt upgrade

sudo apt install nginx

sudo nginx -t sudo systemctl restart nginx sudo systemctl reload nginx sudo service nginx restart sudo service nginx reload

Step 2 – Installing MySQL to Manage Site Data

sudo apt install mysql-server
sudo mysqlsecureinstallation
sudo mysql
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysqlnativepassword BY 'password';
mysql> FLUSH PRIVILEGES;

ถ้าต้องการถอด php ออก ด้วย

sudo apt-get purge php7.*
sudo apt-get autoclean
sudo apt-get autoremove

Step 3 – Installing PHP and Configuring Nginx to Use the PHP Processor => รายละเอียด

sudo apt-get purge php8.*
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update && sudo apt-get install ca-certificates apt-transport-https software-properties-common
sudo apt-get install php8.3-fpm
php -v
php --modules
apt-get install -y php8.3-common php8.3-fpm php8.3-mysql php8.3-redis php8.3-mongodb php8.3-zip php8.3-gd php8.3-mbstring php8.3-cli php8.3-curl php8.3-xml php8.3-bcmath
Enabling PHP8.3 on Nginx server
sudo nano /etc/nginx/sites-available/default

location ~ \.php$ {
        include snippets/fastcgi-php.conf
        # With php-fpm (or other unix sockets):
        fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
        # With php-cgi (or other tcp sockets):
        <a class="hashtag" href="/tags/fastcgi">#fastcgi</a>_pass 127.0.0.1:9000;
}

index index.php index.html

Restart nginx

sudo systemctl restart nginx.service

Old version

sudo add-apt-repository universe
sudo apt install php-fpm php-mysql
sudo apt install php8.1-mbstring
sudo apt install php8.1-gd

php --version which php whereis php

sudo nano /etc/nginx/sites-available/your_domain

/etc/nginx/sites-available/yourdomain

server {
        listen 80;
        root /var/www/html;
        index index.php index.html index.htm index.nginx-debian.html;
        servername your_domain;

    location / {
           try_files $uri $uri/ /index.php?$uri&amp;$args;
           # try_files $uri $uri/ =404;
    }

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

    location ~ /\.ht {
            deny all;
    }

}

Step 4 - How To Secure Nginx with Let's Encrypt on Ubuntu 22.04

sudo apt remove certbot
sudo snap install core; sudo snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
sudo certbot

ติดตั้ง SSL Certificate Filde วิธีการติดตั้ง SSL Certificate บน Nginx Server

Step 5 – ขั้นตอนสุดทัาย - Check List

1. กำหนดค่า php เช่น Timezone, postmaxsize, uploadmaxfilesize 2. Create new user

Ubuntu create user account commands
sudo adduser name
ติดตั้ง ssl แบบ manual
server {
    ...
    listen 443 ssl; # managed by manual
    ssl_certificate    /etc/ssl/certs/filename.crt;
    ssl_certificate_key    /etc/ssl/certs/filename.key;
    # ssl_certificate_chain /etc/ssl/filename.chain
}

config PHP

Change timezone to Asia/Bangkok
sudo nano /etc/php/8.3/fpm/php.ini
Then change
date.timezone = Asia/Bangkok
Then restart php
sudo service php8.3-fpm restart
เปลี่ยนขนาดอัพโหลดไฟล์
It sets the maximum allowed size of the client request body, specified in the “Content-Length” request header field. Here’s an example of increasing the limit to 50MB in /etc/nginx/nginx.conf file. Set in http block which affects all server blocks (virtual hosts).
http {
    ...
    client_max_body_size 50M;
}
Set in server block, which affects a particular site/app
server {
    ...
    client_max_body_size 50M;
}
Set in location block, which affects a particular directory (uploads) under a site/app.
location /uploads {
    ...
    client_max_body_size 50M;
}<br />
Save the file and restart Nginx web server to apply the recent changes using following command.
systemctl restart nginx

ถอนการติดตั้ง

หากมีการติดตั้ง apache2 ไว้ก่อน สามารถถอนการติดตั้งด้วย

apt-get purge apache2
apt-get autoremove

กรณีการอัพเกรดจาก Apache มีขั้นตอนของการตรวจสอบ และ ถอดโปรแกรม

uname -a
Linux alumni 5.4.0-167-generic <a class="hashtag" href="/tags/184">#184</a>-Ubuntu SMP Tue Oct 31 09:21:49 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.1 LTS
Release:    20.04

apachectl -v
Server version: Apache/2.4.41 (Ubuntu)
Server built:   2023-10-26T13:54:09

php -v
PHP 7.4.3-4ubuntu2.20 (cli) (built: Feb 21 2024 13:54:34) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3-4ubuntu2.20, Copyright (c), by Zend Technologies

mysql --version
mysql  Ver 15.1 Distrib 10.3.39-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

<a class="hashtag" href="/tags/apt">#apt</a> update
<a class="hashtag" href="/tags/apt">#apt</a> list --upgradable<br />
<br />
<br />
add-apt-repository ppa:ondrej/apache2
apt update
apt upgrade

do-release-upgrade

ssh recovery port 1022