软件环境
- 系统版本:Centos7.5
- Nginx版本:Nginx1.16.1
本文参考官方安装教程 点击前往
1 安装yum-utils
使用yum进行安装
yum install yum-utils -y
2 配置Nginx的yum源
创建相应的配置文件
vi /etc/yum.repos.d/nginx.repo
没有vim的先安装
yum install vim -y
写入配置并保存
[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
module_hotfixes=true
[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
module_hotfixes=true
enabled=1
为需要安装的版本,默认为stable
版本,可以使用yum命令切换
# 切换到mainline
yum-config-manager --enable nginx-mainline
# 切换到stable
yum-config-manager --enable nginx-stable
3 安装并配置Nginx
安装
yum install nginx -y
启动服务并输入网址检查是否安装成功
#重载守护进程
systemctl daemon-reload
#查看服务状态
systemctl status nginx
#启动服务
systemctl start nginx
#停止服务
systemctl stop nginx
#重启服务
systemctl restart nginx
#开机自启动
systemctl enable nginx
#取消自启动
systemctl disable nginx
如果成功可以看到
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 Sat 2020-01-18 16:21:09 HKT; 13min ago
Docs: http://nginx.org/en/docs/
Main PID: 34373 (nginx)
CGroup: /system.slice/nginx.service
├─34373 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─34374 nginx: worker process
Jan 18 16:21:09 localhost.localdomain systemd[1]: Starting nginx - high performance web server...
Jan 18 16:21:09 localhost.localdomain systemd[1]: Started nginx - high performance web server.
上面看到的配置文件是/etc/nginx/nginx.conf
# 查看配置的命令
cat /etc/nginx/nginx.conf
#配置文件如下
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;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
# 此处可以看到配置文件所在目录
include /etc/nginx/conf.d/*.conf;
}
查看配置文件所在目录
ls /etc/nginx/conf.d/
#只有一个默认配置文件
default.conf
修改配置
vi /etc/nginx/conf.d/default.conf
记录,分享,交流。