使用 Nginx 部署静态服务

一、静态服务程序部署

安装方式:

  • 方式一:yum安装nginx程序 (关注)
  • 方式二:编译安装nginx程序 (了解)

1.1 yum 安装

(1)优化系统环境

第一个步骤:优化系统环境

① 优化selinux安全程序–关闭

临时关闭

wiki
1
2
[root@xiaoQ ~]# getenforce 
[root@xiaoQ ~]# setenforce 0

永久关闭(需要重启系统)

wiki
1
2
[root@xiaoQ ~]# vim /etc/selinux/config 
7 SELINUX=disabled

② 优化系统防火墙服务-关闭

wiki
1
2
3
4
# 临时关闭
[root@xiaoQ ~]# systemctl stop firewalld
# 永久关闭
[root@xiaoQ ~]# systemctl disable firewalld

③ 优化系统时间信息

wiki
1
2
3
# 防止后期查询日志出现问题,同步实践
[root@xiaoQ ~]# ntpdate ntp1.aliyun.com
27 Mar 16:41:51 ntpdate[10207]: step time server 120.25.115.20 offset -0.821817 sec

④ 优化系统yum下载源信息

wiki
1
2
3
4
5
6
7
8
9
10
11
12
# 进入 yum 源目录
[root@xiaoQ ~]# cd /etc/yum.repos.d/
# 把原先的内容进行备份
[root@xiaoQ ~]# mkdir -p /etc/yum.repos.d/backup
# 把原先的内容移到对应的目录中
[root@xiaoQ ~]# find /etc/yum.repos.d/ -type f -exec mv {} /etc/yum.repos.d/backup \;
# 查看目录现存内容
[root@xiaoQ ~]# ll /etc/yum.repos.d/
total 4
drwxr-xr-x 2 root root 4096 Mar 27 16:44 backup
# 进行base源优化:
[root@xiaoQ ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

参考地址:https://developer.aliyun.com/mirror/

⑤ epel优化

wiki
1
[root@xiaoQ ~]# curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo

参考地址:https://developer.aliyun.com/mirror/

⑥ nginx优化

wiki
1
2
3
4
5
6
7
8
9
10
[root@xiaoQ ~]# 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

参考地址:https://nginx.org/en/linux_packages.html#RHEL

(2)下载nginx软件程序

wiki
1
2
# 直接安装 ngixn
[root@xiaoQ ~]# yum install -y nginx

(3)运行启动程序

wiki
1
2
3
4
5
6
7
8
# 启动 nginx
[root@xiaoQ ~]# systemctl start nginx
# 设置开启自启 nginx
[root@xiaoQ ~]# systemctl enable nginx
# 查看进程
[root@xiaoQ ~]# ps -ef | grep nginx
# 查看端口进程
[root@xiaoQ ~]# netstat -lntup|grep nginx

然后把需要的静态文件放在:/usr/share/nginx/html目录下即可

浏览器访问:http:\\\主机地址

看到nginx程序欢迎页面

1.2 编译安装

不推荐

(1)安装nginx的依赖包

安装nginx的依赖包(pcre-devel openssl-devel)

wiki
1
2
# 假设不进行安装
[root@xiaoQ ~]# yum install -y pcre-devel openssl-devel

(2)下载nginx软件

下载nginx软件—1.10.2 复制链接地址(统一位置进行下载)

wiki
1
2
3
[root@xiaoQ ~]# mkdir -p /root/tools
[root@xiaoQ ~]# cd /root/tools
[root@xiaoQ ~]# wget -q http://nginx.org/download/nginx-1.22.1.tar.gz

(3)编译安装

解压要编译安装的软件(解压软件—配置(./configure)—做菜(编译 make)—上菜(安装 make install))

wiki
1
2
3
4
5
6
7
8
9
10
[root@xiaoQ ~]# tar xf nginx-1.22.1.tar.gz
[root@xiaoQ ~]# cd nginx-1.22.1
# 创建web服务程序www用户,让虚拟用户管理这个程序
[root@xiaoQ ~]# useradd -s /sbin/nologin -M www
[root@xiaoQ ~]# ./configure --prefix=/application/nginx-1.22.1 --user=www --group=www --with-http_stub_status_module --with-http_ssl_module

[root@xiaoQ ~]# make
[root@xiaoQ ~]# make install
# 安装完成一个软件要做一个软链接,这样后期升级直接替换成新版本即可
[root@xiaoQ ~]# ln -s /application/nginx-1.22.1 /application/nginx

(4) 启动nginx软件程序

wiki
1
2
3
4
5
6
# 配置环境变量命令
[root@xiaoQ ~]# vim /etc/profile
[root@xiaoQ ~]# export PATH="/applicaton/nginx/sbin/:$PATH"
# 更新文件
[root@xiaoQ ~]# source /etc/profile
[root@xiaoQ ~]# nginx

然后把需要的静态文件放在:/usr/share/nginx/html目录下即可

浏览器访问:http:\\主机地址

至此软件编译安装完毕;

二、网站服务应用配置

2.1 nginx程序安装后生成数据文件信息

目录 内容
/etc/nginx nginx程序主目录
/etc/nginx/nginx.conf nginx程序主配置文件
/etc/nginx/conf.d/ 此目录中,会存储nginx程序虚拟主机(不同网站)配置文件
/etc/nginx/mime.types 媒体资源类型文件(定义nginx能处理哪些请求资源)
/var/log/nginx 日志信息存储路径(访问日志-分析 错误日志-分析错误)
/usr/share/nginx/html 网站服务站点目录(用户访问资源统一存储路径)