Ubuntu-20.04에 NGINX 설치하기



  Environment
    Device : Odroid-HC2
    OS : Ubuntu-20.04
    Host : test (192.168.101.100)




1. NGINX 설치


1) NGINX 설치
$ sudo apt install nginx -y
$ nginx -v

Connect http://192.168.101.210

2) 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) PHP를 위한 NGINX 설정 변경
$ sudo mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default.orig
$ sudo rm /etc/nginx/sites-enabled/default

PHP 소켓 확인

$ 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. HTTPS 서비스 추가


1) SSL인증서 추가
개인키 생성
$ 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
개인키 암호 삭제
$ sudo cp server.key server.key.orig
$ sudo openssl rsa -in server.key.orig -out server.key
인증서 생성
$ sudo openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
인증서 확인
$ ls -l server.*
$ sudo cat server.key | head -3
$ sudo cat server.crt | head -3
인증서 NGIX 디렉터리로 복사
$ sudo mkdir /etc/nginx/ssl
$ sudo cp ~/server.* /etc/nginx/ssl/
2) HTTPS를 위한 NGINX 설정
$ sudo nano /etc/nginx/sites-available/default
Insert Configuration
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