CentOS7源码安装Nginx及其配置

安装镜像源

在华为官网下载https://mirrors.huaweicloud.com/homeNginx镜像源
CentOS7源码安装Nginx及其配置_第1张图片
CentOS7源码安装Nginx及其配置_第2张图片
我下载的是1.20.0版本的,复制链接,在虚拟机中wget -c 下载

 wget -c https://repo.huaweicloud.com/nginx/nginx-1.20.0.tar.gz

CentOS7源码安装Nginx及其配置_第3张图片
ls查看一下,我们系统里就有了nignx-1.20.0.的压缩包了
在这里插入图片描述
解压

tar xf nginx-1.20.0.tar.gz  -C /usr/local/src/

我解压到 /usr/local/src/这个路径下,我们切换到这个目录下去查看一下,发现解压成功
CentOS7源码安装Nginx及其配置_第4张图片
CentOS7源码安装Nginx及其配置_第5张图片
查看README文件,可以看到nginx的官方站点
CentOS7源码安装Nginx及其配置_第6张图片
CentOS7源码安装Nginx及其配置_第7张图片
创建系统用户

useradd -r -s /sbin/nologin -M nginx

在这里插入图片描述
查看帮助
查看我们所需要的模块,–help来查看帮助,显示的所有模块,我们需要什么就下载什么模块

./configure --help

CentOS7源码安装Nginx及其配置_第8张图片
配置模块
我们先配置一部分的模块,运行之后,看缺什么就补什么

[root@localhost nginx-1.20.1]# ./configure --prefix=/usr/local/ngnix \
> --user=nginx --group=nginx \ #配置路径
> --with-threads \  #支持线程
> --with-http_ssl_module\  #加密
> --with-http_sub_module \  
> --with-http_gzip_static_module \ #gzip压缩
> --with-http_auth_request_module \ #请求
> --with-http_stub_status_module\
> --with-http_perl_module \  #正则表达式
> --with-stream \  #TCP/IP
> --with-pcre  #正则表达式

CentOS7源码安装Nginx及其配置_第9张图片
安装启动环境

yum install gcc gcc-c++ make -y

CentOS7源码安装Nginx及其配置_第10张图片
添加补充模块
然后再执行一遍模块的执行,看看还缺什么模块
第一次会发现缺少HTTP的需求模块
在这里插入图片描述

yum install pcre-devel -y

CentOS7源码安装Nginx及其配置_第11张图片
再去执行安装模块那一步,会发现还是缺少OpenSSL模块
在这里插入图片描述

yum install openssl-devel -y

CentOS7源码安装Nginx及其配置_第12张图片
再运行安祖模块命令
CentOS7源码安装Nginx及其配置_第13张图片
显示这个模块就代表成功了
接下来

make

CentOS7源码安装Nginx及其配置_第14张图片

make install

CentOS7源码安装Nginx及其配置_第15张图片
查看目录结构
用tree查看一下/usr/local/nginx/的目录结构

tree /usr/local/nginx/

如果没有tree模块用yum install tree下载tree命令

yum install tree

CentOS7源码安装Nginx及其配置_第16张图片
conf:配置文件
html:网页目录
logs:日志文件
sbin:命令

软连接命令文件
软连接之后就可以直接使用这个命令了

ln -sv /usr/local/nginx/sbin/nginx /usr/sbin/nginx

在这里插入图片描述
查看版本

nginx -v

在这里插入图片描述
配置启动脚本文件

vim /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
PIDFile=/usr/local/nginx/logs/nginx.pid
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=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
 
 
[Install]
WantedBy=multi-user.target

CentOS7源码安装Nginx及其配置_第17张图片
关闭防火墙
关闭防火墙命令
注意:只有关闭防火墙之后才能成功启动nginx,否则用ip访问不成功

systemctl stop firewalld

查看防火墙状态

firewall-cmd --state

在这里插入图片描述

启动nginx

[root@localhost nginx]# systemctl daemon-reload 
[root@localhost nginx]# systemctl start nginx
[root@localhost nginx]# nginx -t

显示successful就代表启动成功了
CentOS7源码安装Nginx及其配置_第18张图片
验证
如果安装成功,默认主页就有我们的nginx了,用自己的ip地址访问一下显示下面的页面就代表安装成功了
CentOS7源码安装Nginx及其配置_第19张图片

配置

完成安装之后,现在进行nginx的配置

修改配置文件
我们可以先切换到我们的conf目录文件下,查看是否有nginx.conf的配置文件,然后vim
加入编辑模式

vim conf/nginx.conf

现在在配置文件中添加我们nginx的路径,我的nginx的路径在/usr/local/下,所以我就修改成这个路径,查看路径用pwd命令

 include /usr/local/nginx/default.d/*.conf;

CentOS7源码安装Nginx及其配置_第20张图片

配置文件说明
CentOS7源码安装Nginx及其配置_第21张图片

测试语法

测试一下我们修改配置文件之后是否还能成功启动nginx,下图说明成功

nginx -t

CentOS7源码安装Nginx及其配置_第22张图片
重启nginx

systemctl restart nginx

你可能感兴趣的:(nginx,运维,linux)