Web服务器基础 -- Nginx 环境部署

前言

本环境是基于 Centos 7.8 系统构建Nginx学习环境
安装 Nginx-1.18.0


系统要求

Centos 7.8 系统 :
cup:2*2 内存 2G 硬盘20G NAT网络模式(可访问互联网) 基于MIni 安装

1、Yum 部署

配置nginx yum源

[root@node01 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true


[root@node01 ~]# yum repolist 
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.cqu.edu.cn
 * extras: mirrors.cqu.edu.cn
 * updates: mirrors.cqu.edu.cn
repo id                                repo name                                                       status
base/7/x86_64                          CentOS-7 - Base                                                 10,072
epel/x86_64                            Extra Packages for Enterprise Linux 7 - x86_64                  13,524
extras/7/x86_64                        CentOS-7 - Extras                                                  451
nginx-stable/7/x86_64                  nginx stable repo                                                  210
updates/7/x86_64                       CentOS-7 - Updates                                               1,640
repolist: 25,897

安装nginx

[root@node01 ~]# yum install nginx -y

启动nginx服务

[root@node01 ~]# systemctl enable --now nginx
[root@node01 ~]# netstat -lnutp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1495/nginx: master 

浏览器访问:http://192.168.5.11/
在这里插入图片描述

2、源码 部署

安装编译依赖包

[root@node02 ~]# yum install pcre-devel openssl-devel -y
[root@node02 ~]# yum install gcc gcc-c++ make -y

上传源码安装包,解压

[root@node02 ~]# ll
total 1020
-rw-------. 1 root root    1228 Jan 29 18:57 anaconda-ks.cfg
-rw-r--r--  1 root root 1039530 Apr 21  2020 nginx-1.18.0.tar.gz
[root@node02 ~]# tar xf nginx-1.18.0.tar.gz -C /usr/local/src/
[root@node02 ~]# cd /usr/local/src/nginx-1.18.0/
[root@node02 nginx-1.18.0]# ll
total 760
drwxr-xr-x 6 1001 1001    326 Feb 21 14:48 auto
-rw-r--r-- 1 1001 1001 302863 Apr 21  2020 CHANGES
-rw-r--r-- 1 1001 1001 462213 Apr 21  2020 CHANGES.ru
drwxr-xr-x 2 1001 1001    168 Feb 21 14:48 conf
-rwxr-xr-x 1 1001 1001   2502 Apr 21  2020 configure
drwxr-xr-x 4 1001 1001     72 Feb 21 14:48 contrib
drwxr-xr-x 2 1001 1001     40 Feb 21 14:48 html
-rw-r--r-- 1 1001 1001   1397 Apr 21  2020 LICENSE
drwxr-xr-x 2 1001 1001     21 Feb 21 14:48 man
-rw-r--r-- 1 1001 1001     49 Apr 21  2020 README
drwxr-xr-x 9 1001 1001     91 Feb 21 14:48 src

编译、安装

[root@node02 nginx-1.18.0]# ./configure --user=nginx --group=nginx \
> --prefix=/usr/local/nginx \
> --with-http_ssl_module \
> --with-http_gunzip_module \
> --with-http_gzip_static_module \
> --with-http_auth_request_module \
> --with-http_stub_status_module

[root@node02 nginx-1.18.0]# make
[root@node02 nginx-1.18.0]# make install

创建用户和组

[root@node02 ~]# groupadd -r -g 995 nginx
[root@node02 ~]# useradd -r -g 995 -u 995 -M -s /sbin/nologin nginx

提供系统启动服务脚本

[root@node02 nginx-1.18.0]# 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=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"

[Install]
WantedBy=multi-user.target

添加nginx环境变量

[root@node02 ~]# vim /etc/profile.d/nginx.sh
export PATH=$PATH://usr/local/nginx/sbin/
[root@node02 ~]# source /etc/profile.d/nginx.sh

启动服务、查看服务状态

[root@node02 nginx-1.18.0]# systemctl daemon-reload
[root@node02 nginx-1.18.0]# systemctl enable --now nginx
[root@node02 nginx-1.18.0]# netstat -lnutp | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      4295/nginx: master

浏览器访问:http://192.168.5.12/
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/XY0918ZWQ/article/details/113917243