使用CentOS7启动NginX

我想尝试使用NginX,所以我创建了一个环境并启动了它。

环境

    • centos7(Vagrant)

 

    • shellはzsh(あまり関係ない)

 

    hostnameはproxy(あまり関係ない)

实际进行的工作。

首先,可以参考这里的内容来创建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                                                                                                                           

完了しました!

确认版本

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.
image.png
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相比,它的配置似乎更直观易懂。

广告
将在 10 秒后关闭
bannerAds