운영체제/Linux

[Linux] nginx 설치

louky 2019. 6. 25. 17:17
반응형

Linux에서 Nginx설치하는 방법이다. 

 

환경

  • OS :  Centos7.6

사전 작업 

1. yum repository 추가 

[root@cluster01 yum.repos.d]# rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
[root@cluster01 yum.repos.d]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

** epel-release의 경우 "yum install epel-release" 를 통해서도 설치가 가능하다. 

 

 

2. nginx repository 생성 

[root@cluster01 yum.repos.d]# cat << EOF > /etc/yum.repos.d/nginx.repo
[nginx]
name=Nginx Repository \$basearch - Archive
baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/
enabled=1
gpgcheck=1
gpgkey=https://nginx.org/keys/nginx_signing.key
EOF

repository가 정상적으로 추가 되었는지 확인 

[root@cluster01 yum.repos.d]# yum clean all ; yum info nginx
Loaded plugins: fastestmirror
Cleaning repos: base epel epel-debuginfo epel-source extras nginx updates webtatic
Cleaning up list of fastest mirrors
Loaded plugins: fastestmirror
Determining fastest mirrors
epel/x86_64/metalink                                                                                                                    | 7.7 kB  00:00:00
epel-debuginfo/x86_64/metalink                                                                                                          | 8.0 kB  00:00:00
epel-source/x86_64/metalink                                                                                                             | 7.7 kB  00:00:00
 * base: mirror.kakao.com
 * epel: ftp.riken.jp
 * epel-debuginfo: ftp.riken.jp
 * epel-source: ftp.riken.jp
 * extras: mirror.kakao.com
 * updates: mirror.kakao.com
 * webtatic: us-east.repo.webtatic.com
base                                                                                                                                    | 3.6 kB  00:00:00
epel                                                                                                                                    | 5.3 kB  00:00:00
epel-debuginfo                                                                                                                          | 3.0 kB  00:00:00
epel-source                                                                                                                             | 4.1 kB  00:00:00
extras                                                                                                                                  | 3.4 kB  00:00:00
nginx                                                                                                                                   | 2.9 kB  00:00:00
updates                                                                                                                                 | 3.4 kB  00:00:00
webtatic                                                                                                                                | 3.6 kB  00:00:00
(1/13): base/7/x86_64/group_gz                                                                                                          | 166 kB  00:00:00
(2/13): epel/x86_64/group_gz                                                                                                            |  88 kB  00:00:00
(3/13): epel/x86_64/updateinfo                                                                                                          | 978 kB  00:00:00
(4/13): epel-debuginfo/x86_64/primary_db                                                                                                | 901 kB  00:00:00
(5/13): epel-source/x86_64/updateinfo                                                                                                   | 978 kB  00:00:00
(6/13): extras/7/x86_64/primary_db                                                                                                      | 205 kB  00:00:00
(7/13): epel-source/x86_64/primary_db                                                                                                   | 2.4 MB  00:00:00
(8/13): base/7/x86_64/primary_db                                                                                                        | 6.0 MB  00:00:01
(9/13): nginx/7/x86_64/primary_db                                                                                                       |  46 kB  00:00:01
(10/13): epel/x86_64/primary_db                                                                                                         | 6.8 MB  00:00:02
(11/13): webtatic/x86_64/group_gz                                                                                                       |  448 B  00:00:00
(12/13): updates/7/x86_64/primary_db                                                                                                    | 6.4 MB  00:00:02
(13/13): webtatic/x86_64/primary_db                                                                                                     | 160 kB  00:00:01
Available Packages
Name        : nginx
Arch        : x86_64
Epoch       : 1
Version     : 1.16.0
Release     : 1.el7.ngx
Size        : 766 k
Repo        : nginx/7/x86_64
Summary     : High performance web server
URL         : http://nginx.org/
License     : 2-clause BSD-like license
Description : nginx [engine x] is an HTTP and reverse proxy server, as well as
            : a mail proxy server.

 

PKG 설치 

 

1. nginx설치

[root@cluster01 ~]# yum install nginx -y

 2. daemon 실행 및 시스템 재기동시 실행 되도록 설정 

[root@cluster01 ~]# systemctl status nginx         ##daemon동작상태 확인
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: http://nginx.org/en/docs/
[root@cluster01 ~]# systemctl enable nginx          ## 시스템 기동시 자동 실행
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@cluster01 ~]# systemctl restart nginx         ## daemon실행
[root@cluster01 ~]# systemctl status nginx
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since 화 2019-06-25 16:57:38 KST; 5s ago
     Docs: http://nginx.org/en/docs/
  Process: 27365 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 27366 (nginx)
   CGroup: /system.slice/nginx.service
           ├─27366 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
           └─27367 nginx: worker process

 6월 25 16:57:38 cluster01 systemd[1]: Starting nginx - high performance web server...
 6월 25 16:57:38 cluster01 systemd[1]: Started nginx - high performance web server.
[root@cluster01 ~]# ps -elf | grep nginx
1 S root     27366     1  0  80   0 - 11585 sigsus 16:57 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
5 S nginx    27367 27366  0  80   0 - 11687 ep_pol 16:57 ?        00:00:00 nginx: worker process
0 S root     27370 21268  0  80   0 - 29218 pipe_w 16:57 pts/0    00:00:00 grep --color=auto nginx

 

3. 확인 

 

 

기본 index.html파일을 "/usr/share/nginx/html" 하위에 존재한다. 

시스템 또는 설정마다 다를 수 있기에  /etc/nginx/nginx.conf 에 설정된 정보로 확인 하는 것이 좋다

 

</etc/nginx/nginx.conf  파일 내용>

[root@cluster01 html]# vi /etc/nginx/nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;             ### Log  저장 위치
pid        /var/run/nginx.pid;                        ### PID저장 위치


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;                ### 기타 config 파일위치, 해당 위치에 있는 config 파일을 전부 loading한다.
}

 

</etc/nginx/conf.d  하위 내용>

[root@cluster01 conf.d]# pwd
/etc/nginx/conf.d
[root@cluster01 conf.d]# ls
default.conf

 

/etc/nginx/ngix.conf의 위치를 변경하고 하고자 할 경우 

예를 들어 nginx  config 파일이 /var/nginx/nginx.conf에 있다면

"/usr/lib/systemd/system/nginx.service"내 "ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf" 를 "ExecStart=/usr/sbin/nginx -c /var/nginx/nginx.conf" 변경하고 "systemctl daemon-reload" 를 통해 반영하고 daemon를 다시 시작한다. 

 

** 어디까지나 변경하고자 할때의 예시이오니 꼭 따라 할 필요는 없습니다. 

# mkdir /var/nginx
# mv /etc/nginx/nginx.conf /var/nginx/
# vi /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /var/nginx/nginx.conf   ##/etc/nginx/nginx.conf를 변경한다.
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target
# systemctl daemon-reload              ### daemon를 실행해주는 service 파일을 변경하고 나서 변경여부를 반영하기 위함.
# systemctl restart nginx
반응형