前端部署nginx服务器
linux版本:CentOS7 64位
1. 命令下载式安装
(1)安装前先查看nginx有无安装:yum list | grep nginx,或者yum list installed | grep "nginx",如果已经安装了,则会出现@的标识
未安装情况下:
已安装情况下:
(2)安装nginx:yum install nginx(手动选择),或者yum -y install nginx(自动选择,跳过Is this OK[y/d/N),然后nginx -v,查看版本号
(3)相关文件夹查看:rpm -ql nginx,查看 Nginx 被安装到了什么地方,有哪些相关目录,其中位于 /etc
目录下的主要是配置文件,还有一些文件见下图:
(4)主要关注的文件夹有两个:
/etc/nginx/conf.d/
文件夹,是我们进行子配置的配置项存放处,/etc/nginx/nginx.conf
主配置文件会默认把这个文件夹中所有子配置项都引入; /usr/share/nginx/html/
文件夹,通常静态文件都放在这个文件夹,也可以根据你自己的习惯放其他地方;
(5)安装之后开启 Nginx,如果系统开启了防火墙,那么需要设置一下在防火墙中加入需要开放的端口,下面列举几个常用的防火墙操作(没开启的话不用管这个):
systemctl start firewalld # 开启防火墙
systemctl stop firewalld # 关闭防火墙
systemctl status firewalld # 查看防火墙开启状态,显示running则是正在运行
firewall-cmd --reload # 重启防火墙,永久打开端口需要reload一下
# 添加开启端口,--permanent表示永久打开,不加是临时打开重启之后失效
firewall-cmd --permanent --zone=public --add-port=8888/tcp
# 查看防火墙,添加的端口也可以看到
firewall-cmd --list-all
(6)设置 Nginx 的开机启动:systemctl enable nginx
(7)启动 Nginx :systemctl start nginx,然后访问你的 IP,这时候就可以看到 Nginx 的欢迎页面了~ Welcome to nginx!
(8)Nginx操作常用命令:
nginx -s reload # 向主进程发送信号,重新加载配置文件,热重启
nginx -s reopen # 重启 Nginx
nginx -s stop # 快速关闭
nginx -s quit # 等待工作进程处理完成后关闭
nginx -T # 查看当前 Nginx 最终的配置
nginx -t -c <配置路径> # 检查配置是否有问题,如果已经在配置目录,则不需要-c
(9)systemctl
是 Linux 系统应用管理工具 systemd
的主命令,用于管理系统,我们也可以用它来对 Nginx 进行管理,相关命令如下:
systemctl start nginx # 启动 Nginx
systemctl stop nginx # 停止 Nginx
systemctl restart nginx # 重启 Nginx
systemctl reload nginx # 重新加载 Nginx,用于修改配置后
systemctl enable nginx # 设置开机启动 Nginx
systemctl disable nginx # 关闭开机启动 Nginx
systemctl status nginx # 查看 Nginx 运行状态
(10)卸载Nginx:
停止Nginx:service nginx stop
删除Nginx的自动启动:chkconfig nginx off
删除Nginx本地文件:
rm -rf /usr/sbin/nginx
rm -rf /etc/nginx
rm -rf /etc/init.d/nginx
yum清理:yum remove ngin
2. 源码下载安装
(1)在安装nginx前首先要确认系统中安装了gcc、pcre-devel、zlib-devel、openssl-devel,
安装命令:
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
(2)nginx下载地址:https://nginx.org/download/,下载“nginx-1.9.9.tar.gz”,移动到/usr/local/下
## 解压 tar -zxvf nginx-1.9.9.tar.gz
##进入nginx目录 cd nginx-1.9.9
## 配置
./configure --prefix=nginx安装路径 --with-http_ssl_module
例如:./configure --prefix=/usr/local/nginx
## 安装
make
make install
或者:make && make install
(3)测试是否安装成功
# cd到刚才配置的安装目录/usr/loca/nginx/
./sbin/nginx -t
错误信息:
nginx: [alert] could not open error log file: open() "/usr/local/nginx/logs/error.log" failed (2: No such file or directory)
2016/09/13 19:08:56 [emerg] 6996#0: open() "/usr/local/nginx/logs/access.log" failed (2: No such file or directory)
原因分析:nginx/目录下没有logs文件夹
解决方法:
mkdir logs chmod 700 logs
正常情况的信息输出:
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
启动nginx
cd /usr/local/nginx/sbin ./nginx //启动nginx
(4)nginx开机自启动参考链接:https://blog.csdn.net/J080624/article/details/79240685?utm_source=blogxgwz5, https://blog.csdn.net/qq_40406061/article/details/83040462
(5)如果nginx安装目录与配置目录不在一个地方,则启动时用下面的命令:
启动代码格式:nginx安装目录地址 -c nginx配置文件地址
例如:/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
(6)验证nginx配置文件是否正确
方法一:进入nginx安装目录 /usr/local/nginx/sbin/ 下,输入命令./nginx -t
看到如下显示
方法二:在启动命令-c前加-t,例如:/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf (7)重启Nginx服务 进入nginx可执行目录 /usr/local/nginx/sbin/ 下,输入命令nginx.conf syntax is ok
nginx.conf test is successful
./nginx -s reload
即可
(8)强制停止pkill -9 nginx
参考链接:
命令式安装参考链接:https://juejin.im/post/5ea931866fb9a043815146fb
Windows服务器nginx安装与配置参考链接:https://www.jianshu.com/p/129378a49245
源码式安装参考链接:https://www.jianshu.com/p/e9f120ea3a5f