【背景说明】
1、很多情况下,公司的生产环境一般都是内网,而大多数的内网Linux环境,一般都是无法直接在线安装nginx
等工具的,此时就可以通过rpm
安装包进行离线安装及配置。
2、安装nginx
一般建议使用root
用户创建一个对应的nginx
普通用户来完成nginx
服务器的后续运维工作~
Linux添加用户可以参考我的这篇博文https://blog.csdn.net/JustinQin/article/details/101677631
lsb_release -a
Linux Centos 7.7
nginx-1.20.1-1.el7.ngx.x86_64.rpm
http://nginx.org/packages/centos/7/x86_64/RPMS/稳定版本
就行nginx
上传安装包到服务路径:/usr/nginx/
/usr/nginx/nginx-1.20.1-1.el7.ngx.x86_64.rpm
nginx
用户有权限的目录下创建,如果没有nginx
用户,可以申请下用root
用户创建nginx
用户。cd /usr/nginx
rpm -ivh --test nginx-1.20.1-1.el7.ngx.x86_64.rpm
--test
表示仅检查安装环境不实际安装,若正常,提示如下图:
cd /usr/nginx/
rpm -ivh nginx-1.20.1-1.el7.ngx.x86_64.rpm
rpm -qa | grep nginx
root
用户安装后,这些目录、文件很多都是root
权限的,需要重新授权给nginx
或者普通用户。rpm
安装包列表清单rpm -qpl nginx-1.20.1-1.el7.ngx.x86_64.rpm
nginx
用户用户名
:用户组名
资源路径
chown -R nginx:nginx /etc/logrotate.d/nginx
chown -R nginx:nginx /etc/nginx
chown -R nginx:nginx /etc/nginx/conf.d
chown -R nginx:nginx /etc/nginx/conf.d/default.conf
chown -R nginx:nginx /etc/nginx/fastcgi_params
chown -R nginx:nginx /etc/nginx/mime.types
chown -R nginx:nginx /etc/nginx/modules
chown -R nginx:nginx /etc/nginx/nginx.conf
chown -R nginx:nginx /etc/nginx/scgi_params
chown -R nginx:nginx /etc/nginx/uwsgi_params
chown -R nginx:nginx /usr/lib/systemd/system/nginx-debug.service
chown -R nginx:nginx /usr/lib/systemd/system/nginx.service
chown -R nginx:nginx /usr/lib64/nginx
chown -R nginx:nginx /usr/lib64/nginx/modules
chown -R nginx:nginx /usr/libexec/initscripts/legacy-actions/nginx
chown -R nginx:nginx /usr/libexec/initscripts/legacy-actions/nginx/check-reload
chown -R nginx:nginx /usr/libexec/initscripts/legacy-actions/nginx/upgrade
chown -R nginx:nginx /usr/sbin/nginx
chown -R nginx:nginx /usr/sbin/nginx-debug
chown -R nginx:nginx /usr/share/doc/nginx-1.20.1
chown -R nginx:nginx /usr/share/doc/nginx-1.20.1/COPYRIGHT
chown -R nginx:nginx /usr/share/man/man8/nginx.8.gz
chown -R nginx:nginx /usr/share/nginx
chown -R nginx:nginx /usr/share/nginx/html
chown -R nginx:nginx /usr/share/nginx/html/50x.html
chown -R nginx:nginx /usr/share/nginx/html/index.html
chown -R nginx:nginx /var/cache/nginx
chown -R nginx:nginx /var/log/nginx
root
用户基于rpm安装包安装完成后,切回nginx
用户日志备份
以及更灵活的部署前端静态资源
,建议重新规划日志目录
和静态资源目录
日志目录
和静态资源目录
mkdir -p /usr/nginx/logs/nginx
mkdir -p /usr/nginx/nginx/html
cp /usr/share/nginx/html/50x.html /usr/nginx/nginx/html/50x.html
cp /usr/share/nginx/html/index.html /usr/nginx/nginx/html/index.html
/etc/nginx
nginx默认配置文件的目录
nginx.conf
nginx主配置文件,文件最后配置了自动加载conf.d
下*.conf
的配置/etc/nginx/conf.d
nginx的*.conf
多个子配置存放目录
default.conf
nginx子配置文件,主要用于端口端口
、日志
、负载均衡
、反向代理
等配置。/usr/sbin/nginx
可执行二进制文件,nginx的启停等执行命令前缀
/usr/share/nginx/html
静态资源目录
本文这里已经改为/usr/nginx/nginx/html
/var/log/nginx
日志文件存放目录
本文这里已经改为/usr/nginx/logs/nginx
10080
端口为例,具体端口根据实际项目需要进行修改。10080.conf
cd /etc/nginx/conf.d
cp default.conf default.conf.bak
mv default.conf 10080.conf
server {
#当前模块所监听的端口
listen 10080;
server_name localhost;
#定义当前server模块的独立日志文件,非root用户运行时需要调整为应用用户有权限读写的目录
access_log /usr/nginx/logs/nginx/host.access.log main;
location / {
#站点的根目录,web资源的默认安装目录,非root用户运行时需要调整为应用用户有权限读写的目录
root /usr/nginx/nginx/html;
#默认nginx首页文件,访问http://ip:port时显示的页面文件
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
#站点的根目录,web资源的默认安装目录,非root用户运行时需要调整为应用用户有权限读写的目录
root /usr/nginx/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
nginx.conf
cd /etc/nginx/
cp nginx.conf nginx.conf.bak
user nginx;
worker_processes auto;
#配置全局错误日志路径及日志级别,错误日志级别[debug|info|notice|warn|error|crit]
#非root用户运行时需要调整为应用用户有权限读写的目录
error_log /usr/nginx/logs/nginx/error.log notice;
#nginx进程文件,非root用户运行时需要调整为应用用户有权限读写的目录
pid /usr/nginx/nginx/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 /usr/nginx/logs/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
如果nginx已经处于运行状态,那么不需要重启nginx,只需要刷新nginx配置即可,刷新配置前先检查下nginx配置是否正确,检查命令 nginx -t
,配置正确则刷新nginx配置,刷新命令 nginx -s reload
如果nginx处于未运行状态,先检查下nginx配置是否正确,检查命令 nginx -t
,配置正确则直接启动nginx,启动命令为 nginx
或/usr/sbin/nginx
–说明:
普通用户nginx
,启动nginx时,可能会提示如下警告信息:
nginx:[warn] ***
表示nginx: master process主进程只能对超级用户root生效即root启动nginx时生效,该
警告不影响nginx的正常使用。
nginx其他常用命令
nginx -s stop
停止 nginx
nginx -s reload
刷新nginx配置(不重启nginx的情况下,重新加载最新nginx配置文件)
nginx -s reopen
新开nginx日志(不重启的情况下,当access.log等日志文件不存在时会新生成对应日志文件)
打开浏览器,访问 http//ip:port
IP改成自己服务器(ifconfig
查看)和port
表示配置的监听端口号。
如访问提示403 Forbidden
相关报错提示信息,则拷贝nginx默认html欢迎页面到静态资源目录
下
cp /usr/share/nginx/html/50x.html /usr/nginx/nginx/html/50x.html
cp /usr/share/nginx/html/index.html /usr/nginx/nginx/html/index.html
监听端口号
端口,则需要使用root
添加监听端口号
后,再重启防火墙,再重新访问~关于防火墙的检查,这个根据实际情况进行配置,如果防火墙本身未开启,也不会影响nginx的访问
但是如果你的Linux环境开启了防火墙,要确保nginx监听的相关端口号,要被添加到防火墙中,才可以正常访问nginx。
这里使用root
用户操作,放开nginx的tcp/10080
端口号
查看防火墙已放开端口列表
firewall-cmd --permanent --zone=public --list-ports
放开10080/tcp
端口
firewall-cmd --permanent --zone=public --add-port=10080/tcp
再次查看端口
firewall-cmd --permanent --zone=public --list-ports
重启防火墙
systemctl restart firewalld.service
其他防火墙常用命令
删除防火墙指定端口firewall-cmd --permanent --zone=public --remove-port=10080/tcp
权限问题?
若使用nginx
用户启动nginx时,提示权限问题,则使用root
用户重新操作【配置权限】
章节的chown
授权给nginx
用户。
default.conf
配置问题
要仔细检查nginx的/etc/nginx.conf
和/etc/nginx/conf.d/default.conf
两个主要配置信息
比如,我当时就错将nginx.conf
的内容拷贝到10080.conf
中了,则nginx -t
测试时会提示报错如下:
nginx: [warn] the “user” directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:1
nginx: [emerg] "user" directive is not allowed here in /etc/nginx/conf.d/10080.conf:1
nginx: configuration file /etc/nginx/nginx.conf test failed