前端网页发布到nginx_前端部署nginx服务器

本文介绍了如何在 CentOS7 系统上安装 Nginx,包括命令行下载安装和源码下载安装两种方式,并详细讲解了配置、启动、管理以及检查配置正确性的步骤。此外,还提到了防火墙设置和卸载 Nginx 的方法。
摘要由CSDN通过智能技术生成

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

启动代码格式:nginx安装目录地址 -c nginx配置文件地址

例如:/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

(6)验证nginx配置文件是否正确

方法一:进入nginx安装目录 /usr/local/nginx/sbin/ 下,输入命令./nginx -t看到如下显示

nginx.conf syntax is ok

nginx.conf test is successful

方法二:在启动命令-c前加-t,例如:/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf

(7)重启Nginx服务

进入nginx可执行目录 /usr/local/nginx/sbin/ 下,输入命令./nginx -s reload即可

(8)强制停止pkill -9 nginx

参考链接:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值