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
    评论
好的,下面是centos7.9编译安装nginx的步骤: 1. 安装编译nginx所需的依赖库: ``` yum install -y gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel libxml2 libxml2-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxslt libxslt-devel gd gd-devel perl perl-devel perl-ExtUtils-Embed ``` 2. 下载nginx源码包: 可以到官网(https://nginx.org/en/download.html)下载最新的稳定版nginx源码包。 3. 解压源码包: ``` tar zxvf nginx-xxx.tar.gz ``` 4. 进入nginx源码目录,执行以下命令进行编译和安装: ``` ./configure --with-http_stub_status_module --with-http_ssl_module make && make install ``` 其中,--prefix参数指定nginx安装目录,--with-http_stub_status_module参数启用状态页模块,--with-http_ssl_module参数启用SSL加密支持。 5. 配置nginx: 进入/usr/local/nginx/conf目录,编辑nginx.conf文件,进行配置。这里给出一个简单的配置文件示例: ``` user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /usr/local/nginx/conf/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 /var/log/nginx/access.log main; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root /usr/local/nginx/html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/local/nginx/html; } } server { listen 443 ssl; server_name localhost; ssl_certificate /usr/local/nginx/conf/server.crt; ssl_certificate_key /usr/local/nginx/conf/server.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { root /usr/local/nginx/html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/local/nginx/html; } } } ``` 其中,listen参数指定监听端口,server_name参数指定域名,root参数指定网站根目录,ssl_certificate和ssl_certificate_key参数指定SSL证书和私钥文件路径。 6. 启动nginx: 进入/usr/local/nginx/sbin目录,执行以下命令启动nginx: ``` ./nginx ``` 7. 检查nginx是否启动成功: 执行以下命令查看nginx进程是否存在: ``` ps -ef | grep nginx ``` 执行以下命令检查nginx监听的端口是否打开: ``` netstat -tlnp | grep nginx ``` 如果输出类似以下内容,则说明nginx已经成功安装并启动: ``` tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN xxxx/nginx tcp 0 0 :::443 :::* LISTEN xxxx/nginx ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

空间指挥员

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

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

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

打赏作者

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

抵扣说明:

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

余额充值