JAVA和Nginx 教程大全

网站首页 > 精选教程 正文

centos7系统下Nginx配置搭建 centos7系统下nginx配置搭建手册

wys521 2024-10-05 23:57:05 精选教程 24 ℃ 0 评论

一. gcc 安装

安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境。

yum install gcc-c++

二. PCRE pcre-devel 安装

PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库,nginx也需要此库。

yum install -y pcre pcre-devel

三. zlib 安装

zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。

yum install -y zlib zlib-devel

四. OpenSSL 安装

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL 协议,并提供丰富的应用程序供测试或其它目的使用。

nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL库。

yum install -y openssl openssl-devel

五.使用wget命令下载

wget -c https://nginx.org/download/nginx-1.14.2.tar.gz

1.解压

tar -zxvf nginx-1.14.2.tar.gz cd nginx-1.14.2

2.配置

默认配置(推荐)

./configure

自定义配置(不推荐)

./configure \ --prefix=/usr/local/nginx \ --conf-path=/usr/local/nginx/conf/nginx.conf \ --pid-path=/usr/local/nginx/conf/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --with-http_gzip_static_module \ --http-client-body-temp-path=/var/temp/nginx/client \ --http-proxy-temp-path=/var/temp/nginx/proxy \ --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \ --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \ --http-scgi-temp-path=/var/temp/nginx/scgi

注:将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录

3.编译安装

make

make install

4.查找安装路径

whereis nginx

5.启动、停止nginx

cd /usr/local/nginx/sbin/

#启动

./nginx

#关闭进程

./nginx -s stop

#平滑关闭

./nginx -s quit

#重启

./nginx -s reload

#查看状态

./nginx -V

./nginx -s quit:此方式停止步骤是待nginx进程处理任务完毕进行停止。

./nginx -s stop:此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。

6.查询nginx进程

ps aux|grep nginx

7.重启 nginx

先停止再启动:

对 nginx 进行重启相当于先停止再启动,即先执行停止命令再执行启动命令。如下:

./nginx -s quit

./nginx

重新加载配置

当 nginx的配置文件 nginx.conf 修改后,要想让配置生效需要重启 nginx,使用-s reload不用先停止 nginx再启动 nginx 即可将配置信息在 nginx 中生效,如下:

./nginx -s reload

启动成功后,在浏览器成功访问。

配置文件的修改

进入nginx 配置文件目录,编辑修改nginx配置文件

cd /usr/local/nginx/conf

vim /usr/local/nginx/conf/nginx.conf

(文件省略)

六.防火墙设置

关闭防火墙,或者添加防火墙规则

#查看防火墙状态

firewall-cmd --state

#停止防火墙

systemctl stop firewalld.service

#禁止开机启动

systemctl disable firewalld.service

或者编辑配置文件

vim /etc/sysconfig/iptables

添加这样一条开放80端口的规则后保存

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

重启服务

#重启防火墙使配置生效

systemctl restart iptables.service

#设置防火墙开机启动

systemctl enable iptables.service

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表