CentOS7.9安装配置Nginx

1 安装所需环境

Nginx 是 C语言 开发,建议在 Linux 上运行,当然,也可以安装 Windows 版本,本篇则使用 CentOS 7 作为安装环境。

1.1 gcc 安装

安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:

yum install -y gcc

1.2 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

1.3 zlib 安装

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

yum install -y zlib zlib-devel

1.4 OpenSSL 安装

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

yum install -y openssl openssl-devel

1.5 wget 安装

yum -y install wget

2 安装nginx
注意:安装目录自选选择
下载nginx安装包
wget下载:

wget http://nginx.org/download/nginx-1.20.1.tar.gz

官网下载:
直接下载.tar.gz安装包,地址:https://nginx.org/en/download.html
2、把压缩包解压

tar -zxvf nginx-1.20.1.tar.gz

3、切换到cd nginx-1.20.1/下面

cd nginx-1.20.1/

执行三个命令:

./configure
make
make install

3 启动、停止nginx
切换到/usr/local/nginx安装目录

cd /usr/local/nginx/sbin/

启动:

./nginx 

停止:

./nginx -s stop

退出:

./nginx -s quit

重新启动:

./nginx -s reload

查看nginx服务是否启动成功:

ps -ef | grep nginx

4 开机自启动
即在rc.local增加启动代码就可以了。

vi /etc/rc.local

增加一行 /usr/local/nginx/sbin/nginx
设置执行权限:

chmod 755 rc.local

5 设置系统服务
1、编写启动脚本,内容如下:

vi /usr/lib/systemd/system/nginx.service

#添加如下内容
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
 
 
[Service] 
Type=forking
# 路径对应安装路径
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install] 
WantedBy=multi-user.target

2、使用

systemctl start|stop|reload|restart|status nginx.service
#开机自启:
systemctl enable nginx.service

#关闭开机自启:
systemctl disable nginx.service

#重启(先杀死再启动):
systemctl restart nginx.service

注意:开启启动和设置系统服务任选其一即可,不可重复。

6 设置环境变量

vi /etc/profile

1.新加下列代码

export NGINX_HOME=/usr/local/nginx
export PATH=$PATH:$NGINX_HOME/sbin

2.执行source /etc/profile,重新加载配置文件

source /etc/profile

3.热启动nginx(刷新配置)

nginx -s reload
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

空间指挥员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值