CentOS7 で nginxのインストールと起動

## 1. nginx のインストール
参照：[こちら](https://qiita.com/MuuKojima/items/afc0ad8309ba9c5ed5ee)

### リポジトリの追加

```
# vim /etc/yum.repos.d/nginx.repo
```

```
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=0
enabled=1
```

### インストール

```
# yum install nginx
```


### バージョン確認

```
# nginx -v
nginx version: nginx/1.13.6
```

## 2. 自動起動設定

```
# systemctl enable nginx
ln -s '/usr/lib/systemd/system/nginx.service' '/etc/systemd/system/multi-user.target.wants/nginx.service'

```

## 3. 起動

```
# systemctl start nginx
Job for nginx.service failed. See 'systemctl status nginx.service' and 'journalctl -xn' for details.
```
なんかエラーになった。
systemctl status nginx.service　で確認してみる。

```
# systemctl status nginx.service
nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled)
   Active: failed (Result: exit-code) since Fri 2017-10-13 13:16:35 JST; 2min 11s ago
     Docs: http://nginx.org/en/docs/
  Process: 8579 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=1/FAILURE)
  Process: 8576 ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)

Oct 13 13:16:32 dti-vps-srv717 nginx[8576]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Oct 13 13:16:32 dti-vps-srv717 nginx[8579]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Oct 13 13:16:33 dti-vps-srv717 nginx[8579]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Oct 13 13:16:33 dti-vps-srv717 nginx[8579]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Oct 13 13:16:34 dti-vps-srv717 nginx[8579]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Oct 13 13:16:34 dti-vps-srv717 nginx[8579]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Oct 13 13:16:35 dti-vps-srv717 nginx[8579]: nginx: [emerg] still could not bind()
Oct 13 13:16:35 dti-vps-srv717 systemd[1]: nginx.service: control process exited, code=exited status=1
Oct 13 13:16:35 dti-vps-srv717 systemd[1]: Failed to start nginx - high performance web server.
Oct 13 13:16:35 dti-vps-srv717 systemd[1]: Unit nginx.service entered failed state.
```

既に80番ポートが使用されてるためっぽい。。
lsofコマンドでListenしているプロセルを調べる。

```
# lsof -i:80
COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
httpd    365   root    4u  IPv6 557661      0t0  TCP *:http (LISTEN)
httpd   3799 apache    4u  IPv6 557661      0t0  TCP *:http (LISTEN)
httpd   3824 apache    4u  IPv6 557661      0t0  TCP *:http (LISTEN)
httpd   7338 apache    4u  IPv6 557661      0t0  TCP *:http (LISTEN)
httpd   7352 apache    4u  IPv6 557661      0t0  TCP *:http (LISTEN)
httpd   7368 apache    4u  IPv6 557661      0t0  TCP *:http (LISTEN)
```
Apacheか・・・
Apacheを停止させ、nginxを起動することにする。

```
Apache 停止
# systemctl stop httpd.service

nginx 起動
# systemctl start nginx

確認
# lsof -i:80
COMMAND  PID  USER   FD   TYPE     DEVICE SIZE/OFF NODE NAME
nginx   8606  root    6u  IPv4 1575352318      0t0  TCP *:http (LISTEN)
nginx   8607 nginx    6u  IPv4 1575352318      0t0  TCP *:http (LISTEN)
```

## 4. 表示確認
http://サーバのアドレス/
![nginx.jpg](https://qiita-image-store.s3.amazonaws.com/0/118362/a9a35696-b558-f848-25b2-fc374595e7b2.jpeg)

無事にWelcome画面が出た