CentOS7でNginXを起動する

NginXを使ってみたいと思ったので環境を作って起動してみる。

# 環境
- centos7（Vagrant）
- shellはzsh（あまり関係ない）
- hostnameはproxy（あまり関係ない）

# 実際にした作業

まずは[こちら](http://nginx.org/en/linux_packages.html)を参考にしてrepoファイルを作成

```
proxy:[~] 
% cat /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
```
そして`yum install`

```
proxy:[~] 
% sudo yum install nginx
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.nara.wide.ad.jp
 * extras: mirrors.cat.net
 * updates: mirrors.cat.net
nginx                                                                                                                                  | 2.9 kB  00:00:00     
nginx/x86_64/primary_db                                                                                                                |  35 kB  00:00:01     
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ nginx.x86_64 1:1.14.0-1.el7_4.ngx を インストール
--> 依存性解決を終了しました。

依存性を解決しました

==============================================================================================================================================================
 Package                          アーキテクチャー                  バージョン                                         リポジトリー                      容量
==============================================================================================================================================================
インストール中:
 nginx                            x86_64                            1:1.14.0-1.el7_4.ngx                               nginx                            750 k

トランザクションの要約
==============================================================================================================================================================
インストール  1 パッケージ

総ダウンロード容量: 750 k
インストール容量: 2.6 M
Is this ok [y/d/N]: y
Downloading packages:
nginx-1.14.0-1.el7_4.ngx.x86_64.rpm                                                                                                    | 750 kB  00:00:12     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  インストール中          : 1:nginx-1.14.0-1.el7_4.ngx.x86_64                                                                                             1/1 
----------------------------------------------------------------------

Thanks for using nginx!

Please find the official documentation for nginx here:
* http://nginx.org/en/docs/

Please subscribe to nginx-announce mailing list to get
the most important news about nginx:
* http://nginx.org/en/support.html

Commercial subscriptions for nginx are available on:
* http://nginx.com/products/

----------------------------------------------------------------------
  検証中                  : 1:nginx-1.14.0-1.el7_4.ngx.x86_64                                                                                             1/1 

インストール:
  nginx.x86_64 1:1.14.0-1.el7_4.ngx                                                                                                                           

完了しました!
```
versionを確認

```
proxy:[~] 
% nginx -v
nginx version: nginx/1.14.0
proxy:[~] 
% sudo systemctl status nginx
● 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/
```
起動してみる

```
proxy:[~] 
% sudo systemctl start nginx.service 
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
proxy:[~] 
% sudo systemctl status nginx       
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since 火 2018-09-11 22:00:29 JST; 8s ago
     Docs: http://nginx.org/en/docs/
  Process: 3699 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=1/FAILURE)

 9月 11 22:00:27 proxy nginx[3699]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
 9月 11 22:00:27 proxy nginx[3699]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
 9月 11 22:00:28 proxy nginx[3699]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
 9月 11 22:00:28 proxy nginx[3699]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
 9月 11 22:00:29 proxy nginx[3699]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
 9月 11 22:00:29 proxy systemd[1]: nginx.service: control process exited, code=exited status=1
 9月 11 22:00:29 proxy nginx[3699]: nginx: [emerg] still could not bind()
 9月 11 22:00:29 proxy systemd[1]: Failed to start nginx - high performance web server.
 9月 11 22:00:29 proxy systemd[1]: Unit nginx.service entered failed state.
 9月 11 22:00:29 proxy systemd[1]: nginx.service failed.
```
起動しない...
そういやApache起動中だったので、そちらを止めて再度NginX起動！

```
proxy:[~] 
% sudo systemctl start nginx.service
proxy:[~] 
% sudo systemctl status nginx
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since 火 2018-09-11 22:02:44 JST; 3s ago
     Docs: http://nginx.org/en/docs/
  Process: 4050 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 4051 (nginx)
   CGroup: /system.slice/nginx.service
           ├─4051 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
           └─4052 nginx: worker process

 9月 11 22:02:44 proxy systemd[1]: Starting nginx - high performance web server...
 9月 11 22:02:44 proxy systemd[1]: PID file /var/run/nginx.pid not readable (yet?) after start.
 9月 11 22:02:44 proxy systemd[1]: Started nginx - high performance web server.
```

NginXが起動したのでブラウザで確認する
![image.png](https://qiita-image-store.s3.amazonaws.com/0/169092/8eda4e84-1306-0e9f-c64d-26ab96d998a6.png)
ようこそ**NginX**に！！
一応常事起動にしておく

```
proxy:[~] 
% sudo systemctl enable nginx.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
```
今日はここまで
今後勉強していくために準備をした感じでした
# おまけ
初期設定を見てみる（コメントアウトは除外）

```
proxy:[~] 
% cat /etc/nginx/nginx.conf | grep -v # | uniq         

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.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;

    keepalive_timeout  65;

    include /etc/nginx/conf.d/*.conf;
}
proxy:[~] 
% cat /etc/nginx/conf.d/default.conf | grep -v # | uniq
server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm; // ←さっき見たのはコレ
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}
```
あまり詳しくないけどApacheに比べて設定が直感的にわかりやすそう