零基础学习-编译安装Nginx(1-8)

第一步;安装组件

[root@Centos8 ~]#yum -y install gcc pcre-devel openssl-devel zlib-devel

在这里插入图片描述
第二步;为nginx创建一个不登陆的用户 并切换到/usr/local/src/下

[root@Centos8 ~]#useradd -s /sbin/nologin nginx
[root@Centos8 ~]#cd /usr/local/src/

在这里插入图片描述
第三步;wget下载源、解压后进入nginx的目录下进行编译

[root@centos8 src]#wget http://nginx.org/download/nginx-1.18.0.tar.gz
[root@centos8 src]#tar xf nginx-1.18.0.tar.gz
[root@centos8 src]#cd nginx-1.18.0/

在这里插入图片描述

下载链接在http://nginx.org/en/download.html
在这里插入图片描述
第四步;编译安装

[root@Centos8 nginx-1.18.0]#./configure --prefix=/apps/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_ssl_module \
> --with-http_v2_module \
> --with-http_realip_module \
> --with-http_stub_status_module \
> --with-http_gzip_static_module \
> --with-pcre \
> --with-stream \
> --with-stream_ssl_module \
> --with-stream_realip_module
[root@Centos8 nginx-1.18.0]#make && make install

说明:make && make install的意识是;
执行make如果没有发生错误就执行make install,与&&一起的还有||,不过意思不一样,&&是与,||是或;

第五步:修改权限,nginx完成安装以后,有四个主要的目录

[root@Centos8 nginx-1.18.0]#chown -R nginx.nginx /apps/nginx
[root@Centos8 nginx-1.18.0]#ll /apps/nginx/
total 0
drwxr-xr-x 2 nginx nginx 333 Jun 10 08:50 conf
drwxr-xr-x 2 nginx nginx  40 Jun 10 08:50 html
drwxr-xr-x 2 nginx nginx   6 Jun 10 08:50 logs
drwxr-xr-x 2 nginx nginx  19 Jun 10 08:50 sbin

四个目录说明;
conf:保存nginx所有的配置文件,其中nginx.conf是nginx服务器的最核心最主要的配置文件,其他
的.conf则是用来配置nginx相关的功能的,例如fastcgi功能使用的是fastcgi.conf和
fastcgi_params两个文件,配置文件一般都有个样板配置文件,是文件名.default结尾,使用的使用将其
复制为并将default去掉即可。
html目录中保存了nginx服务器的web文件,但是可以更改为其他目录保存web文件,另外还有一个50x的web
文件是默认的错误页面提示页面。
logs:用来保存nginx服务器的访问日志错误日志等日志,logs目录可以放在其他路径,比
如/var/logs/nginx里面。
sbin:保存nginx二进制启动脚本,可以接受不同的参数以实现不同的功能。

第六步:验证版本及编译参数

[root@Centos8 nginx-1.18.0]#ls /apps/nginx/sbin/
nginx

在这个时候 直接执行nginx -v是查看不了任何东西的,我之前在这一步老是会被遗忘,原因是没有设置变量

[root@Centos8 nginx-1.18.0]#nginx -v
-bash: nginx: command not found
[root@Centos8 nginx-1.18.0]#nginx -V
-bash: nginx: command not found

对于单个文件来说,直接ln -s建立软链接 也是可以!
当设置完成后 再次查看 版本信息什么都有了。

[root@Centos8 nginx]#ln -s /apps/nginx/sbin/nginx /usr/sbin/   #ln -s建立软链接 
[root@Centos8 nginx]#cd
[root@Centos8 ~]#nginx -v
nginx version: nginx/1.18.0
[root@Centos8 ~]#nginx -V
nginx version: nginx/1.18.0
built by gcc 8.4.1 20200928 (Red Hat 8.4.1-1) (GCC) 
built with OpenSSL 1.1.1g FIPS  21 Apr 2020

第七步;启动和停止 nginx 测试访问 web 界面

[root@Centos8 ~]#nginx           #开启
[root@Centos8 ~]#ss -ntl
[root@Centos8 ~]#nginx -s stop   #开启 
[root@Centos8 ~]#ss -ntl        

在这里插入图片描述
测试访问web界面;
在这里插入图片描述
第七步;创建 Nginx 自启动文件

#复制同一版本的nginx的yum安装生成的service文件

[root@centos8 ~]#vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/apps/nginx/run/nginx.pid
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

#创建目录

[root@centos8 ~]#mkdir /apps/nginx/run/

#修改配置文件

[root@centos8 ~]#vim /apps/nginx/conf/nginx.conf
pid /apps/nginx/run/nginx.pid;
root@Centos8 nginx]#chown -R nginx.nginx /apps/nginx/run #更改run所属组

在这里插入图片描述
改成相对路径或者绝对路径都可以:
在这里插入图片描述
在这里插入图片描述
在这里查看run目前没有生成pid文件;
在这里插入图片描述

第八步:验证 Nginx 自启动文件

[root@Centos8 nginx]#systemctl daemon-reload         #daemon加载services文件
[root@Centos8 nginx]#systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.

目前端口确认是打开的
在这里插入图片描述
而网页也是可以正常
在这里插入图片描述

[root@Centos8 nginx]#ll /apps/nginx/run/
total 4
-rw-r--r-- 1 root root 5 Jun 10 09:26 nginx.pid           #查看目前pid文件已经生成

在这里插入图片描述
执行pstree -p 查看
在这里插入图片描述
至此 编译安装nginx 完成

谢谢观赏

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值