테스트 환경
ubuntu 18.04
nginx 1.22.0
설치
apt install nginx
# 설치가 안될 경우 sudo 를 붙이거나
# sudo apt install nginx
# sudo apt update 등을 해주자
# 그리고도 안되면 아래 nginx 사이트나 다른 사이트를 찾아보자!
https://nginx.org/en/linux_packages.html#Ubuntu
nginx: Linux packages
nginx: Linux packages Supported distributions and versions nginx packages are available for the following Linux distributions and versions: RHEL and derivatives Version Supported Platforms 7.4+ x86_64, aarch64/arm64 8.x x86_64, aarch64/arm64, s390x 9.x x86
nginx.org
실행
아래 명령어 중 한개를 실행 시켜준다.
# 설치 환경에 따라 또는 실행시키는 명령어에 따라서 아래 중 한개를 실행시키거나 다를 수 있다.
sudo nginx
sudo service nginx start
sudo systemctl stop nginx
localhost또는 서버ip 로 접속했을 때 실행된다면 잘 설치된 것이다.
혹시라도 접속이 불가능 하다면 방화벽이나 공유기(포트포워딩) 등을 확인해봐야한다.
nginx 실행관련된 옵션 (only nginx)
nginx -s signal
stop — fast shutdown 빠른 종료
quit — graceful shutdown 정상 종료
reload — reloading the configuration file 중단하지 않고 설정 파일만 다시 가져오기
reopen — reopening the log files
그냥 다시 실행하고 싶다면 nginx 를 다시 실행하면 된다.
service 사용
아래와 같이 쓸수도 있다 궁금하면 더 검색해보자.
sudo service nginx restart
sudo service nginx reload
sudo service nginx status
nginx 환경 파일 테스트
어디 블로그에서 nginx 종료하고 하라는데
나는 반영 전 문법 등의 문제가 없는지 확인 후 reload 하는데 사용한다.
nginx -t
간단 설정
버전 및 설정에 따라 다를 수 있다.
nginx/1.22.0 기준
서버 블록 파일 만들기
확장자가 conf인 파일을 생성해준다.
또는 default.conf를 수정하자.
이유는 /etc/nginx/nginx.conf 의 기본 설정값이 아래와 같이 있기 때문이다.
include /etc/nginx/conf.d/*.conf;
touch /etc/nginx/conf.d/test.conf
설정파일에 설정을 해준다.
아래 내용은 default.conf의 내용이다.
# test.conf
server {
listen 80; # 포트 설정
server_name localhost; # 나중에 도메인 연결시 여기에 적으면 된다.
# 로그설정
access_log /var/log/nginx/host.access.log main;
error_log /var/log/nginx/host.error.log main;
# 들어오는 패스 설정
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
# 들어오는 패스 설정
# location에 들어오는 패스를 적을 수 있다.
# location /test {
# # 안쪽의 자세한 설명은 검색해보자.
# root /usr/share/nginx/html;
# index index.html index.htm;
# }
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
재시작
# 만든 conf파일의 문법 이상 등이 없는지 확인하자.
# 문제가 있다면 에러가 난다
nginx -t
# 설정만 다시 가져오기
nginx -s reload
재시작 하기전 default.conf를 사용했다면 문제가 없겠지만
새로 만들었다면
default.conf와 80번 포트 충돌이 일어난다.
이럴 경우 default.conf의 이름을 default.conf.tt 등 .conf로 끝나지 않도록 바꾸거나
default.conf를 삭제하면 된다.
또는 /etc/nginx/nginx.conf 에 include /etc/nginx/conf.d/*.conf; 이게 아닌 아까만든 conf 파일을 넣으면된다.
include /etc/nginx/conf.d/test.conf;
# /etc/nginx/nginx.conf
user nginx;
...
# include /etc/nginx/conf.d/*.conf;
include /etc/nginx/conf.d/test.conf;
}
도메인 연결하기
https://24hours-beginner.tistory.com/402
[NginX] 도메인(domain) 연결하기 (feat. Godaddy)
nginx를 통해서 도메인을 연결할 수 있다. 1. DNS서버에 A 레코드 추가하기 핵심은 DNS(Domain Name System)에 접속하여 나의 도메인에 서버 주소를 매핑(A 레코드 추가)하는 것이다. A 레코드 (A Record) 와 CNA
24hours-beginner.tistory.com
참고
https://nginx.org/en/docs/beginners_guide.html
Beginner’s Guide
Beginner’s Guide This guide gives a basic introduction to nginx and describes some simple tasks that can be done with it. It is supposed that nginx is already installed on the reader’s machine. If it is not, see the Installing nginx page. This guide de
nginx.org