How to Install NGINX on Ubuntu-20.04
Environment Device : Odroid-HC2 OS : Ubuntu-20.04 Host : test (192.168.101.100)
1. Install NGINX
1) Install NGINX
$ sudo apt install nginx -y
$ nginx -v
Connect http://192.168.101.210
2) Install PHP
$ sudo apt install php php-fpm php-mysql -y
$ sudo apt remove apache2 --purge -y
$ sudo rm -rf /etc/apache2
$ sudo netstat -an | grep php
3) Setup NGIX For PHP
$ sudo mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default.orig
$ sudo rm /etc/nginx/sites-enabled/default
check php socket
$ sudo netstat -an | grep php
$ sudo nano /etc/nginx/sites-available/default
server{
listen 80;
server_name test.local;
root /var/www/html;
index index.html index.htm index.php index.nginx-debian.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
$ cd /etc/nginx/sites-enabled/
$ ls -l
$ sudo -i
# cd /etc/nginx/sites-enabled/
# ln -s /etc/nginx/sites-available/default
# ls -l
# exit
$ sudo nano /var/www/html/phpinfo.php
<?php
phpinfo();
?>
Connect http://192.168.101.100/phpinfo.php
2. Install HTTPS service
1) Generate SSL Certification
Generate private key
$ sudo openssl genrsa -des3 -out server.key 2048
Generate CSR(Certification sinning Request)
$ sudo openssl req -new -days 365 -key server.key -out server.csr
Clear password of Private key
$ sudo cp server.key server.key.orig
$ sudo openssl rsa -in server.key.orig -out server.key
Create Certification
$ sudo openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
Verify Certification
$ ls -l server.*
$ sudo cat server.key | head -3
$ sudo cat server.crt | head -3
Copy Certification
$ sudo mkdir /etc/nginx/ssl
$ sudo cp ~/server.* /etc/nginx/ssl/
2) Setup NGINX For HTTPS
$ sudo nano /etc/nginx/sites-available/default
server{
listen 443 ssl;
server_name test.local;
root /var/www/html;
index index.html index.htm index.php index.nginx-debian.html;
ssl_certificate /etc/nginx/ssl/server.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Connect https://192.168.101.100
Connect https://192.168.101.100/phpinfo.php