Ubuntu 24.04에 Nginx 설치하고 방화벽 여는 방법

Ubuntu 24.04 서버에 기본 저장소의 Nginx 1.24를 설치하고, ufw 방화벽에서 80번과 443번 포트를 열어 다른 PC에서 접속하는 방법을 정리합니다. 서비스 관리 명령, 설정 파일 위치, 자주 만나…

Ubuntu 24.04Nginx 1.24x86_64약 22분

검증된 문서입니다

클라우드 서버를 생성하여 이 문서의 명령을 처음부터 끝까지 그대로 실행하고 확인한 문서입니다.

운영체제
Ubuntu 24.04.5 LTS · x86_64
설치 버전
Nginx 1.24.0 (1.24.0-2ubuntu7.19)
테스트 서버
DigitalOcean · 1 vCPU / 2 GB RAM / 50 GB SSD (s-1vcpu-2gb, sgp1)
마지막 확인
2026-10-27

이 문서에는 제휴 링크가 포함되어 있습니다. 링크를 통해 가입하면 코드샌드가 일정 수수료를 받을 수 있으며, 문서 내용과 평가에는 영향을 주지 않습니다.

Ubuntu 24.04 서버에 기본 저장소의 Nginx 1.24를 설치하고, ufw 방화벽에서 80번과 443번 포트를 열어 다른 PC에서 접속하는 방법을 정리합니다. 서비스 관리 명령, 설정 파일 위치, 자주 만나는 오류와 삭제하는 방법까지 다룹니다.

1. 준비물

  • Ubuntu 24.04 LTS 서버 1대
  • sudo 권한이 있는 일반 사용자 계정으로 SSH 접속
  • 외부에서 접속을 확인할 PC (방화벽 확인 단계에서 사용)

서버가 없다면 DigitalOcean의 Droplet(1 vCPU · 2GB)을 생성해 보세요. 이 문서의 모든 과정은 Droplet(1 vCPU · 2GB 메모리 · 50GB SSD · Ubuntu 24.04 · 싱가포르)으로 서버를 생성하고, sudo 권한이 있는 일반 사용자 계정으로 확인했습니다. 외부 접속은 다른 PC(예시 IP 203.0.113.10)에서 서버(예시 IP 203.0.113.20)로 확인했습니다.

2. 설치할 버전 확인

먼저 패키지 목록을 갱신합니다. 출력의 앞부분(Hit·Get 줄)은 생략했습니다.

sudo apt update
72 packages can be upgraded. Run 'apt list --upgradable' to see them.

Candidate가 설치될 버전입니다. Ubuntu 24.04 기본 저장소에는 Nginx 1.24가 들어 있습니다. Ubuntu 26.04를 쓴다면 Ubuntu 26.04에 Nginx 설치하고 방화벽 여는 방법을 보세요. 26.04에는 Nginx 1.28이 들어 있습니다.

apt-cache policy nginx
nginx:
  Installed: (none)
  Candidate: 1.24.0-2ubuntu7.19
  Version table:
     1.24.0-2ubuntu7.19 500
        500 http://sgp1.clouds.archive.ubuntu.com/ubuntu noble-updates/main amd64 Packages
        500 http://security.ubuntu.com/ubuntu noble-security/main amd64 Packages
     1.24.0-2ubuntu7 500
        500 http://sgp1.clouds.archive.ubuntu.com/ubuntu noble/main amd64 Packages

3. Nginx 설치

-y는 설치할지 묻는 질문에 자동으로 「예」라고 답한다는 뜻입니다. nginx와 설정 파일 패키지인 nginx-common이 함께 설치됩니다. 출력의 가운데 부분은 생략했습니다.

sudo apt install -y nginx
The following additional packages will be installed:
  nginx-common
Suggested packages:
  fcgiwrap nginx-doc ssl-cert
The following NEW packages will be installed:
  nginx nginx-common
0 upgraded, 2 newly installed, 0 to remove and 72 not upgraded.
Need to get 570 kB of archives.
After this operation, 1602 kB of additional disk space will be used.
…
Setting up nginx (1.24.0-2ubuntu7.19) ...
 * Upgrading binary nginx
   ...done.
Processing triggers for man-db (2.12.0-4build2) ...
Processing triggers for ufw (0.36.2-6) ...

설치된 버전을 확인합니다.

nginx -v
nginx version: nginx/1.24.0 (Ubuntu)

4. 서비스 상태 확인

Ubuntu의 Nginx 패키지는 설치가 끝나면 바로 실행됩니다. Active: active (running)이면 정상입니다.

systemctl status nginx --no-pager | head -3
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: enabled)
     Active: active (running) since Tue 2026-10-27 08:24:35 UTC; 13s ago

enabled는 서버를 재부팅해도 Nginx가 자동으로 실행된다는 뜻입니다.

systemctl is-enabled nginx
enabled

Nginx가 80 포트에서 접속을 기다리는지 확인합니다. 0.0.0.0:80은 IPv4, [::]:80은 IPv6의 모든 주소에서 접속을 받는다는 뜻입니다.

sudo ss -tlnp | grep nginx
LISTEN 0      511          0.0.0.0:80        0.0.0.0:*    users:(("nginx",pid=3262,fd=5),("nginx",pid=3259,fd=5))
LISTEN 0      511             [::]:80           [::]:*    users:(("nginx",pid=3262,fd=6),("nginx",pid=3259,fd=6))

서버 안에서 기본 페이지를 요청해 봅니다. -I는 본문 없이 응답 헤더만 받는다는 뜻입니다. HTTP/1.1 200 OK가 표시되면 Nginx가 정상으로 응답하고 있습니다.

curl -I http://localhost
HTTP/1.1 200 OK
Server: nginx/1.24.0 (Ubuntu)
Date: Tue, 27 Oct 2026 08:24:50 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 27 Oct 2026 08:24:34 GMT
Connection: keep-alive
ETag: "6ae05fc2-267"
Accept-Ranges: bytes

5. 방화벽(ufw)에서 80, 443 포트 열기

DigitalOcean에서 새로 만든 Ubuntu 서버는 ufw 방화벽이 꺼져 있습니다. 그래서 지금은 다른 PC에서도 바로 접속됩니다. 아래는 PC(203.0.113.10)의 터미널에서 서버로 요청한 결과입니다.

curl -I http://203.0.113.20
HTTP/1.1 200 OK
Server: nginx/1.24.0 (Ubuntu)
sudo ufw status
Status: inactive

방화벽을 켜기 전에 열어 둘 포트를 정합니다. Nginx를 설치하면 ufw에 Nginx용 프로필이 등록됩니다.

sudo ufw app list
Available applications:
  Nginx Full
  Nginx HTTP
  Nginx HTTPS
  OpenSSH

Nginx HTTP는 80 포트, Nginx HTTPS는 443 포트만 엽니다. 나중에 HTTPS 인증서를 붙일 것이라면 두 포트를 함께 여는 Nginx Full을 쓰면 됩니다.

sudo ufw app info "Nginx Full"
Profile: Nginx Full
Title: Web Server (Nginx, HTTP + HTTPS)
Description: Small, but very powerful and efficient web server

Ports:
  80,443/tcp

방화벽을 켜기 전에 SSH(22 포트)를 먼저 허용합니다. SSH를 허용하지 않고 방화벽을 켜면 지금 쓰는 SSH 접속이 끊긴 뒤 다시 접속할 수 없습니다.

sudo ufw allow OpenSSH
Rules updated
Rules updated (v6)

방화벽을 켭니다. --force는 「SSH 접속이 끊길 수 있다」는 확인 질문을 건너뛴다는 뜻입니다.

sudo ufw --force enable
Firewall is active and enabled on system startup

이제 SSH만 열려 있기 때문에 PC에서 웹 페이지를 요청하면 응답 없이 시간이 초과됩니다. --max-time 10은 10초까지만 기다린다는 뜻입니다.

curl -I --max-time 10 http://203.0.113.20
curl: (28) Connection timed out after 10005 milliseconds

80번과 443번 포트를 엽니다.

sudo ufw allow "Nginx Full"
Rule added
Rule added (v6)
sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
Nginx Full                 ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
Nginx Full (v6)            ALLOW       Anywhere (v6)

PC에서 다시 요청하면 응답이 옵니다.

curl -I --max-time 10 http://203.0.113.20
HTTP/1.1 200 OK
Server: nginx/1.24.0 (Ubuntu)
Date: Tue, 27 Oct 2026 08:29:14 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 27 Oct 2026 08:24:34 GMT
Connection: keep-alive
ETag: "6ae05fc2-267"
Accept-Ranges: bytes

페이지 제목만 뽑아 보면 Nginx 기본 페이지인 것을 확인할 수 있습니다. 브라우저에서 http://203.0.113.20(서버 IP)을 열어도 같은 페이지가 표시됩니다.

curl -s http://203.0.113.20 | grep -o "<title>.*</title>"
<title>Welcome to nginx!</title>

6. 설정 파일 위치

Nginx 설정은 /etc/nginx 폴더에 있습니다.

ls /etc/nginx/
conf.d		koi-win		   nginx.conf	    sites-enabled
fastcgi.conf	mime.types	   proxy_params     snippets
fastcgi_params	modules-available  scgi_params	    uwsgi_params
koi-utf		modules-enabled    sites-available  win-utf

주로 고치는 파일은 다음과 같습니다.

  • /etc/nginx/nginx.conf: 전체 설정. 맨 아래에서 conf.d와 sites-enabled 폴더의 파일을 불러옵니다.
  • /etc/nginx/sites-available/: 사이트(server 블록)마다 설정 파일을 하나씩 만들어 두는 곳입니다.
  • /etc/nginx/sites-enabled/: 실제로 쓸 사이트만 sites-available의 파일을 링크로 걸어 둡니다. 링크를 지우면 파일은 남겨 둔 채 사이트만 끌 수 있습니다.
grep -n include /etc/nginx/nginx.conf
5:include /etc/nginx/modules-enabled/*.conf;
26:	include /etc/nginx/mime.types;
59:	include /etc/nginx/conf.d/*.conf;
60:	include /etc/nginx/sites-enabled/*;

설치 직후에는 default 사이트 하나가 켜져 있습니다. sites-enabled/default는 sites-available/default를 가리키는 링크입니다.

ls -l /etc/nginx/sites-available/ /etc/nginx/sites-enabled/
/etc/nginx/sites-available/:
total 4
-rw-r--r-- 1 root root 2412 Nov 30  2023 default

/etc/nginx/sites-enabled/:
total 0
lrwxrwxrwx 1 root root 34 Oct 27 08:24 default -> /etc/nginx/sites-available/default

default 파일에서 주석(#)과 빈 줄을 빼고 보면 내용은 몇 줄뿐입니다. root는 웹 페이지 파일이 있는 폴더, index는 폴더 주소로 요청했을 때 보여 줄 파일 이름을 앞에서부터 찾는 순서입니다.

grep -v -E "^\s*#|^\s*$" /etc/nginx/sites-available/default
server {
	listen 80 default_server;
	listen [::]:80 default_server;
	root /var/www/html;
	index index.html index.htm index.nginx-debian.html;
	server_name _;
	location / {
		try_files $uri $uri/ =404;
	}
}

지금 보이는 기본 페이지는 /var/www/html 폴더의 index.nginx-debian.html입니다.

ls /var/www/html/
index.nginx-debian.html

접속 기록과 오류 기록은 /var/log/nginx 폴더에 쌓입니다. 문제가 생기면 error.log를 먼저 확인합니다.

ls -l /var/log/nginx/
total 8
-rw-r----- 1 www-data adm 435 Oct 27 08:28 access.log
-rw-r----- 1 www-data adm  76 Oct 27 08:24 error.log

7. 페이지 바꾸고 설정 적용하기

/var/www/html에 index.html을 만들면 index 순서에서 index.nginx-debian.html보다 앞에 있기 때문에 이 파일이 표시됩니다. tee는 받은 내용을 파일에 쓰고 화면에도 보여 주는 명령으로, 관리자 권한이 필요한 폴더에 파일을 쓸 때 sudo와 함께 씁니다.

echo "<h1>Hello Nginx</h1>" | sudo tee /var/www/html/index.html
<h1>Hello Nginx</h1>
curl http://localhost
<h1>Hello Nginx</h1>

페이지 파일은 바꾸는 즉시 반영되지만, 설정 파일을 고쳤을 때는 Nginx에 다시 읽게 해야 합니다. 그 전에 nginx -t로 설정에 문법 오류가 없는지 먼저 확인합니다.

sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

설정을 다시 읽을 때는 reload를 씁니다. 접속 중인 요청을 끊지 않고 새 설정으로 바꿉니다. 출력은 없습니다.

sudo systemctl reload nginx

그 밖의 관리 명령은 다음과 같습니다. 모두 출력이 없고, systemctl is-active nginx로 결과를 확인할 수 있습니다.

  • sudo systemctl restart nginx: Nginx를 껐다가 다시 켭니다. 접속 중인 요청이 끊깁니다.
  • sudo systemctl stop nginx: Nginx를 끕니다. 재부팅하면 다시 켜집니다.
  • sudo systemctl start nginx: 꺼진 Nginx를 켭니다.
sudo systemctl stop nginx
systemctl is-active nginx
inactive
sudo systemctl start nginx
systemctl is-active nginx
active

8. 문제 해결

curl: (28) Connection timed out after 10005 milliseconds

서버 안에서 curl -I http://localhost는 되는데 다른 PC에서만 시간이 초과된다면 방화벽이 80 포트를 막고 있다는 뜻입니다. sudo ufw status에 Nginx Full(또는 80/tcp)이 있는지 확인하고, 없으면 sudo ufw allow "Nginx Full"을 실행하세요. ufw에 규칙이 있는데도 같다면 클라우드 업체의 방화벽(DigitalOcean Cloud Firewall 등)을 확인하세요.

nginx: [emerg] invalid number of arguments in "root" directive

설정 파일에 문법 오류가 있다는 뜻입니다. 아래는 root /var/www/html; 줄 끝의 세미콜론(;)을 지운 뒤 확인한 결과입니다. 메시지 끝의 default:44는 오류가 있는 파일과 줄 번호입니다.

sudo nginx -t
2026/10/27 08:29:38 [emerg] 5184#5184: invalid number of arguments in "root" directive in /etc/nginx/sites-enabled/default:44
nginx: configuration file /etc/nginx/nginx.conf test failed

이 상태에서 reload를 하면 실패하지만, Nginx는 바뀌기 전의 설정으로 계속 실행됩니다. 그래서 사이트는 그대로 열리는데 고친 설정만 반영되지 않습니다.

sudo systemctl reload nginx
Job for nginx.service failed.
See "systemctl status nginx.service" and "journalctl -xeu nginx.service" for details.
systemctl is-active nginx
active

표시된 줄을 고친 뒤 sudo nginx -t가 test is successful로 끝나는지 확인하고 다시 reload하세요.

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

다른 프로그램이 이미 80 포트를 쓰고 있어서 Nginx가 시작하지 못했다는 뜻입니다. 아래는 다른 프로그램(파이썬 웹 서버)이 80 포트를 쓰는 상태에서 Nginx를 시작한 결과입니다.

sudo systemctl start nginx
Job for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xeu nginx.service" for details.

원인은 journalctl로 확인합니다. -n 12는 마지막 12줄만 보여 준다는 뜻이고, 앞의 3줄은 생략했습니다. Nginx는 몇 번 다시 시도한 뒤 still could not bind()로 멈춥니다.

sudo journalctl -u nginx --no-pager -n 12
…
Oct 27 08:29:47 opendoc-nginx-10271716 nginx[5452]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Oct 27 08:29:48 opendoc-nginx-10271716 nginx[5452]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Oct 27 08:29:48 opendoc-nginx-10271716 nginx[5452]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Oct 27 08:29:49 opendoc-nginx-10271716 nginx[5452]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Oct 27 08:29:49 opendoc-nginx-10271716 nginx[5452]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Oct 27 08:29:50 opendoc-nginx-10271716 nginx[5452]: nginx: [emerg] still could not bind()
Oct 27 08:29:50 opendoc-nginx-10271716 systemd[1]: nginx.service: Control process exited, code=exited, status=1/FAILURE
Oct 27 08:29:50 opendoc-nginx-10271716 systemd[1]: nginx.service: Failed with result 'exit-code'.
Oct 27 08:29:50 opendoc-nginx-10271716 systemd[1]: Failed to start nginx.service - A high performance web server and a reverse proxy server.

80 포트를 쓰고 있는 프로그램은 ss로 찾습니다. users: 뒤에 프로그램 이름과 프로세스 번호(pid)가 표시됩니다.

sudo ss -tlnp | grep ":80 "
LISTEN 0      5            0.0.0.0:80        0.0.0.0:*    users:(("python3",pid=5394,fd=3))

그 프로그램이 필요 없다면 끄고 Nginx를 시작합니다. 서비스로 실행 중인 프로그램이면 sudo systemctl stop 서비스이름으로 끄고, 아니면 sudo kill에 위에서 확인한 pid를 적습니다. 두 프로그램을 함께 써야 한다면 둘 중 하나의 포트를 바꿔야 합니다.

sudo kill 5394
sudo systemctl start nginx
systemctl is-active nginx
active
curl: (7) Failed to connect to localhost port 80 after 0 ms: Couldn't connect to server

서버에서 80 포트로 접속을 받는 프로그램이 없다는 뜻입니다. Nginx가 꺼져 있거나 설치되지 않은 상태입니다. systemctl status nginx --no-pager로 상태를 확인하고, failed로 표시되면 sudo journalctl -u nginx --no-pager -n 20에서 원인을 찾으세요.

9. 삭제하기

purge는 패키지와 함께 /etc/nginx의 설정 파일까지 지웁니다. 출력의 앞부분은 생략했습니다.

sudo apt purge -y nginx nginx-common
The following packages will be REMOVED:
  nginx* nginx-common*
0 upgraded, 0 newly installed, 2 to remove and 72 not upgraded.
After this operation, 1602 kB disk space will be freed.
…
Removing nginx (1.24.0-2ubuntu7.19) ...
Removing nginx-common (1.24.0-2ubuntu7.19) ...
Processing triggers for man-db (2.12.0-4build2) ...
Purging configuration files for nginx-common (1.24.0-2ubuntu7.19) ...
Processing triggers for ufw (0.36.2-6) ...
Rules updated for profile 'OpenSSH'
Skipped reloading firewall

함께 설치되었다가 더 이상 쓰지 않는 패키지가 있으면 지웁니다. 이 서버에서는 지울 것이 없었습니다.

sudo apt autoremove -y
0 upgraded, 0 newly installed, 0 to remove and 72 not upgraded.

방화벽에서 열었던 80번과 443번 포트를 닫습니다. SSH 규칙은 남겨 둡니다.

sudo ufw delete allow "Nginx Full"
Rule deleted
Rule deleted (v6)
sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)

남은 패키지를 확인합니다. grep -c는 찾은 줄의 수를 센다는 뜻으로, 0이면 Nginx 패키지가 모두 지워진 것입니다.

dpkg -l | grep -c nginx
0

/etc/nginx는 지워졌지만, 웹 페이지 폴더 /var/www/html은 남아 있습니다.

ls /etc/nginx /var/www/html
/var/www/html:
index.html
index.nginx-debian.html
ls: cannot access '/etc/nginx': No such file or directory

남은 페이지 파일이 필요 없다면 지웁니다. rm -rf는 폴더와 안의 파일을 확인 없이 모두 지우므로 경로를 다시 확인하고 실행하세요.

sudo rm -rf /var/www/html

Nginx가 지워졌으므로 80 포트로 접속할 수 없습니다.

curl http://localhost
curl: (7) Failed to connect to localhost port 80 after 0 ms: Couldn't connect to server

직접 해 보기

서버가 없다면 아래 업체에서 몇 분이면 Ubuntu 서버를 만들 수 있습니다. 이 문서는 DigitalOcean 에서 검증했습니다.