nginx는 요즘 떠오르는 차세대 웹서버 소프트웨어이다. 적은 자원으로 더 많은 일을 할 수 가 있다.
APCHE는 요청이 많을 경우 Process를 생성하여 처리하는 방식(preFork)이다. APACHE는 접속마다 프로세스 또는 Thread를 생성하는 구조이다. 동시 접속 요청이 10,000개라면 그 만큼 Thread 생성 비용이 들 것이고 대용량 요청을 처리할 수 있는 웹서버로서의 한계를 드러내게 된다.
반면 NGINX는 Event-driven 방식으로 동작한다.
한 개 또는 고정된 프로세스만 생성하고, 그 프로세스 내부에서 비동기방식으로 효율적으로 작업들을 처리한다. 따라서 동시 접속 요청이 많아도 프로세스 또는 쓰레드 생성 비용이 존재하지 않는다.
우분투 18.04에서 NGINX 설치 방법
nginx는 Ubuntu의 레파지토리에 default로 있기에 아래와 같이 명령어를 입력한다.
$ sudo apt update
$ sudo apt install nginx
그렇다면 아래와 같이 nginx가 설치된다.
root2@DESKTOP-024G8AA:~$ sudo apt update
Get:4 https://download.docker.com/linux/ubuntu bionic InRelease [64.4 kB]
Hit:1 http://mirror.kakao.com/ubuntu bionic InRelease
Get:2 http://mirror.kakao.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:3 http://mirror.kakao.com/ubuntu bionic-backports InRelease [74.6 kB]
Get:5 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages [9594 B]
Get:6 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Get:7 http://mirror.kakao.com/ubuntu bionic-updates/main amd64 Packages [768 kB]
Get:8 http://mirror.kakao.com/ubuntu bionic-updates/main Translation-en [275 kB]
Get:9 http://mirror.kakao.com/ubuntu bionic-updates/restricted amd64 Packages [18.7 kB]
Get:10 http://mirror.kakao.com/ubuntu bionic-updates/restricted Translation-en [5328 B]
Get:11 http://mirror.kakao.com/ubuntu bionic-updates/universe amd64 Packages [1022 kB]
Get:12 http://mirror.kakao.com/ubuntu bionic-updates/universe Translation-en [315 kB]
Get:13 http://mirror.kakao.com/ubuntu bionic-updates/multiverse amd64 Packages [8096 B]
Get:14 http://mirror.kakao.com/ubuntu bionic-updates/multiverse Translation-en [3972 B]
Get:15 http://mirror.kakao.com/ubuntu bionic-backports/main amd64 Packages [2512 B]
Get:16 http://mirror.kakao.com/ubuntu bionic-backports/main Translation-en [1644 B]
Get:17 http://mirror.kakao.com/ubuntu bionic-backports/universe amd64 Packages [4024 B]
Get:18 http://mirror.kakao.com/ubuntu bionic-backports/universe Translation-en [1856 B]
Get:19 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [545 kB]
Get:20 http://security.ubuntu.com/ubuntu bionic-security/main Translation-en [182 kB]
Get:21 http://security.ubuntu.com/ubuntu bionic-security/restricted amd64 Packages [9856 B]
Get:22 http://security.ubuntu.com/ubuntu bionic-security/restricted Translation-en [3480 B]
Get:23 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [617 kB]
Get:24 http://security.ubuntu.com/ubuntu bionic-security/universe Translation-en [206 kB]
Get:25 http://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 Packages [5476 B]
Get:26 http://security.ubuntu.com/ubuntu bionic-security/multiverse Translation-en [2500 B]
Fetched 4324 kB in 13s (328 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
291 packages can be upgraded. Run 'apt list --upgradable' to see them.
root2@DESKTOP-024G8AA:~$ sudo apt install nginx
[sudo] password for root2:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
libnginx-mod-http-geoip libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail
libnginx-mod-stream nginx-common nginx-core
Suggested packages:
fcgiwrap nginx-doc ssl-cert
The following NEW packages will be installed:
libnginx-mod-http-geoip libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail
libnginx-mod-stream nginx nginx-common nginx-core
0 upgraded, 8 newly installed, 0 to remove and 291 not upgraded.
1 not fully installed or removed.
Need to get 598 kB of archives.
After this operation, 2120 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://mirror.kakao.com/ubuntu bionic-updates/main amd64 nginx-common all 1.14.0-0ubuntu1.6 [37.3 kB]
Get:2 http://mirror.kakao.com/ubuntu bionic-updates/main amd64 libnginx-mod-http-geoip amd64 1.14.0-0ubuntu1.6 [11.2 kB]
Get:3 http://mirror.kakao.com/ubuntu bionic-updates/main amd64 libnginx-mod-http-image-filter amd64 1.14.0-0ubuntu1.6 [1
4.5 kB]
Get:4 http://mirror.kakao.com/ubuntu bionic-updates/main amd64 libnginx-mod-http-xslt-filter amd64 1.14.0-0ubuntu1.6 [12
.9 kB]
Get:5 http://mirror.kakao.com/ubuntu bionic-updates/main amd64 libnginx-mod-mail amd64 1.14.0-0ubuntu1.6 [41.7 kB]
Get:6 http://mirror.kakao.com/ubuntu bionic-updates/main amd64 libnginx-mod-stream amd64 1.14.0-0ubuntu1.6 [63.6 kB]
Get:7 http://mirror.kakao.com/ubuntu bionic-updates/main amd64 nginx-core amd64 1.14.0-0ubuntu1.6 [413 kB]
Get:8 http://mirror.kakao.com/ubuntu bionic-updates/main amd64 nginx all 1.14.0-0ubuntu1.6 [3596 B]
Fetched 598 kB in 1s (1183 kB/s)
Preconfiguring packages ...
Selecting previously unselected package nginx-common.
(Reading database ... 46509 files and directories currently installed.)
Preparing to unpack .../0-nginx-common_1.14.0-0ubuntu1.6_all.deb ...
Unpacking nginx-common (1.14.0-0ubuntu1.6) ...
Selecting previously unselected package libnginx-mod-http-geoip.
Preparing to unpack .../1-libnginx-mod-http-geoip_1.14.0-0ubuntu1.6_amd64.deb ...
Unpacking libnginx-mod-http-geoip (1.14.0-0ubuntu1.6) ...
Selecting previously unselected package libnginx-mod-http-image-filter.
Preparing to unpack .../2-libnginx-mod-http-image-filter_1.14.0-0ubuntu1.6_amd64.deb ...
Unpacking libnginx-mod-http-image-filter (1.14.0-0ubuntu1.6) ...
Selecting previously unselected package libnginx-mod-http-xslt-filter.
Preparing to unpack .../3-libnginx-mod-http-xslt-filter_1.14.0-0ubuntu1.6_amd64.deb ...
Unpacking libnginx-mod-http-xslt-filter (1.14.0-0ubuntu1.6) ...
Selecting previously unselected package libnginx-mod-mail.
Preparing to unpack .../4-libnginx-mod-mail_1.14.0-0ubuntu1.6_amd64.deb ...
Unpacking libnginx-mod-mail (1.14.0-0ubuntu1.6) ...
Selecting previously unselected package libnginx-mod-stream.
Preparing to unpack .../5-libnginx-mod-stream_1.14.0-0ubuntu1.6_amd64.deb ...
Unpacking libnginx-mod-stream (1.14.0-0ubuntu1.6) ...
Selecting previously unselected package nginx-core.
Preparing to unpack .../6-nginx-core_1.14.0-0ubuntu1.6_amd64.deb ...
Unpacking nginx-core (1.14.0-0ubuntu1.6) ...
Selecting previously unselected package nginx.
Preparing to unpack .../7-nginx_1.14.0-0ubuntu1.6_all.deb ...
Unpacking nginx (1.14.0-0ubuntu1.6) ...
Processing triggers for ufw (0.35-5) ...
Setting up docker.io (18.09.2-0ubuntu1~18.04.1) ...
invoke-rc.d: could not determine current runlevel
Processing triggers for ureadahead (0.100.0-20) ...
Setting up nginx-common (1.14.0-0ubuntu1.6) ...
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /lib/systemd/system/nginx.service.
Setting up libnginx-mod-http-image-filter (1.14.0-0ubuntu1.6) ...
Processing triggers for systemd (237-3ubuntu10.3) ...
Setting up libnginx-mod-mail (1.14.0-0ubuntu1.6) ...
Processing triggers for man-db (2.8.3-2) ...
Setting up libnginx-mod-http-xslt-filter (1.14.0-0ubuntu1.6) ...
Setting up libnginx-mod-http-geoip (1.14.0-0ubuntu1.6) ...
Setting up libnginx-mod-stream (1.14.0-0ubuntu1.6) ...
Setting up nginx-core (1.14.0-0ubuntu1.6) ...
Cannot open netlink socket: Protocol not supported
invoke-rc.d: could not determine current runlevel
Setting up nginx (1.14.0-0ubuntu1.6) ...
Processing triggers for ureadahead (0.100.0-20) ...
Processing triggers for ufw (0.35-5) ...
W: APT had planned for dpkg to do more than it reported back (40 vs 44).
Affected packages: docker.io:amd64
방화벽설치
NGINX를 테스트하기전에 ufw라는 방화벽을 설치한다.
root2@DESKTOP-024G8AA:~$ sudo ufw app list
Available applications:
Nginx Full
Nginx HTTP
Nginx HTTPS
OpenSSH
CREATE TABLE nodejs.users(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
age INT UNSIGNED NOT NULL,
married TINYINT NOT NULL,
comment TEXT NULL,
created_at DATETIME NOT NULL DEFAULT now(),
PRIMARY KEY(id),
UNIQUE INDEX name_UNIQUE (name ASC))
COMMENT = '사용자 정보'
DEFAULT CHARSET=utf8
ENGINE=InnoDB;
결과
mysql> CREATE TABLE nodejs.users(
-> id INT NOT NULL AUTO_INCREMENT,
-> name VARCHAR(20) NOT NULL,
-> age INT UNSIGNED NOT NULL,
-> married TINYINT NOT NULL,
-> comment TEXT NULL,
-> created_at DATETIME NOT NULL DEFAULT now(),
-> PRIMARY KEY(id),
-> UNIQUE INDEX name_UNIQUE (name ASC))
-> COMMENT = '사용자 정보'
-> DEFAULT CHARSET=utf8
-> ENGINE=InnoDB;
Query OK, 0 rows affected, 1 warning (0.07 sec)
table 정보 확인
mysql> DESC users;
+------------+------------------+------+-----+-------------------+-------------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+-------------------+-------------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | UNI | NULL | |
| age | int(10) unsigned | NO | | NULL | |
| married | tinyint(4) | NO | | NULL | |
| comment | text | YES | | NULL | |
| created_at | datetime | NO | | CURRENT_TIMESTAMP | DEFAULT_GENERATED |
+------------+------------------+------+-----+-------------------+-------------------+
6 rows in set (0.01 sec)
그 뒤, 사용자의 댓글을 저장하는 테이블 생성
아래 코드 입력
CREATE TABLE nodejs.comments (
id INT NOT NULL AUTO_INCREMENT,
commenter INT NOT NULL,
comment VARCHAR(100) NOT NULL,
created_at DATETIME NOT NULL DEFAULT now(),
PRIMARY KEY(id),
INDEX commenter_idx (commenter ASC),
CONSTRAINT commenter
FOREIGN KEY (commenter)
REFERENCES nodejs.users (id)
ON DELETE CASCADE
ON UPDATE CASCADE)
COMMENT = '댓글'
DEFAULT CHARSET=utf8
ENGINE=InnoDB;
결과
mysql> CREATE TABLE nodejs.comments (
-> id INT NOT NULL AUTO_INCREMENT,
-> commenter INT NOT NULL,
-> comment VARCHAR(100) NOT NULL,
-> created_at DATETIME NOT NULL DEFAULT now(),
-> PRIMARY KEY(id),
-> INDEX commenter_idx (commenter ASC),
-> CONSTRAINT commenter
-> FOREIGN KEY (commenter)
-> REFERENCES nodejs.users (id)
-> ON DELETE CASCADE
-> ON UPDATE CASCADE)
-> COMMENT = '댓글'
-> DEFAULT CHARSET=utf8
-> ENGINE=InnoDB;
Query OK, 0 rows affected, 1 warning (0.06 sec)
테이블명 확인
mysql> SHOW TABLES;
+------------------+
| Tables_in_nodejs |
+------------------+
| comments |
| users |
+------------------+
2 rows in set (0.00 sec)