軟件環境
- 系統版本: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
記錄,分享,交流。