文章目录
安装
安装的先决条件
搭建yum源
安装nginx
配置
安装
安装的先决条件
sudo yum install yum-utils
搭建yum源
To set up the yum repository, create the file named /etc/yum.repos.d/nginx.repo with the following contents:
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
By default, the repository for stable nginx packages is used. If you would like to use mainline nginx packages, run the following command:
sudo yum-config-manager --enable nginx-mainline
安装nginx
To install nginx, run the following command:
sudo yum install nginx
When prompted to accept the GPG key, verify that the fingerprint matches 573B FD6B 3D8F BC64 1079 A6AB ABF5 BD82 7BD9 BF62, and if so, accept it.
安装后查看nginx是否正常启动,如果出现下面情况,端口被占用,重启下主机。
[root@gsl371 ~]# 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 Fri 2019-06-21 11:43:54 CST; 3h 9min ago
Docs: http://nginx.org/en/docs/
Process: 3810 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=1/FAILURE)
Jun 21 11:43:51 gsl371 nginx[3810]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Jun 21 11:43:52 gsl371 nginx[3810]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Jun 21 11:43:52 gsl371 nginx[3810]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Jun 21 11:43:53 gsl371 nginx[3810]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Jun 21 11:43:53 gsl371 nginx[3810]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Jun 21 11:43:54 gsl371 nginx[3810]: nginx: [emerg] still could not bind()
Jun 21 11:43:54 gsl371 systemd[1]: nginx.service: control process exited, code=exited status=1
Jun 21 11:43:54 gsl371 systemd[1]: Failed to start nginx - high performance web server.
Jun 21 11:43:54 gsl371 systemd[1]: Unit nginx.service entered failed state.
Jun 21 11:43:54 gsl371 systemd[1]: nginx.service failed.
如果仍然被占用,可通过查看端口命令找到被占用的端口,杀死相应的进程。
正常如下
[root@gsl371 ~]# 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 Fri 2019-06-21 15:07:08 CST; 54s ago
Docs: http://nginx.org/en/docs/
Process: 2220 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 2221 (nginx)
CGroup: /system.slice/nginx.service
├─2221 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─2222 nginx: worker process
Jun 21 15:07:08 gsl371 systemd[1]: Starting nginx - high performance web server...
Jun 21 15:07:08 gsl371 systemd[1]: Started nginx - high performance web server.
放通80端口,打开对应网址,出现欢迎界面,安装成功。
配置
The way nginx and its modules work is determined in the configuration file. By default, the configuration file is named nginx.conf and placed in the directory /usr/local/nginx/conf, /etc/nginx, or /usr/local/etc/nginx.