1、全局模块
worker_processes 1;
工作进程数,一般设置成服务器内核数的2倍(一般不超过8个,超过8个反而会降低性能,一般是4个,1-2个也可以)
处理进程的过程必然涉及配置文件和展示页面,也就是涉及打开文件的数量
linux默认打开的文件数是1024个
修改最大文件数 /etc/security/limits.conf
![](https://img-blog.csdnimg.cn/direct/9af4b65ebc094b33bbbedbd3bfa9c0b5.png)
alias和root之间匹配工作目录的区别:
root的匹配模式是拼接
root的工作目录/opt/test1,访问的uri /xy102
location /xy102
工作目录/opt/test1
访问的是/opt/test1/xy102
alias匹配nginx的工作目录,路径是绝对路径
location /xy102
alias /opt/test1/xy102/;
alias只能写在http模块当中server模块的location模块当中
root可以写在server模块,也可以在http,也可以在location
使用alias匹配工作目录不能够使用重定向功能
2、events模块
events {
workers_connections 1024;
}
决定了nginx能够处理的连接数,连接数和worker_processes的数值相乘
3、stream四层代理模块
4、http模块
转发和处理http请求,设置代理(正向代理,反向代理),缓存,定义日志格式,重定向配置
在http模块当中,包含:
server模块,http里面可以有多个server模块
在server模块当中包含:location模块
在server当中可以有多个location
![](https://img-blog.csdnimg.cn/direct/717cfb890ed34940a0a8425804d44509.png)
5、实验
5.1统计访问状态
修改配置文件
![](https://img-blog.csdnimg.cn/direct/de1cbc6a80ef4354ac69591cc115a4e4.png)
![](https://img-blog.csdnimg.cn/direct/2607196c24a4439892c24c98f8c61eed.png)
各行的含义:
Active connections 当前活动的连接数
server accepts handled requests 表示已经处理的连接数
2 2 21
从左往右表示已经处理的连接数,成功建立连接的次数,已经处理的请求数
Reading:0 writing:1 waiting:1
reading:表示服务端正在从客户端读取请求的数据
writing:表示服务端正在把响应数据发送给客户端
waiting:表示有连接处于空闲状态,等待新的请求
5.2基于密码的授权进行访问控制
先安装httpd-tools,htpasswd的工具
![](https://img-blog.csdnimg.cn/direct/2c044fb9b31e4143a31e84b248084d84.png)
生成一个passwd.db的文件,用户abc可以对这个文件进行处理,设置密码为123
![](https://img-blog.csdnimg.cn/direct/01e328588f75478896eaa8a862dd3f19.png)
将passwd.db的所有者改为nginx,并修改权限为400
![](https://img-blog.csdnimg.cn/direct/8d2e58ee3181473c9ec2cd1ab435e30c.png)
修改配置文件
![](https://img-blog.csdnimg.cn/direct/f831ed49458246d4a15b67e91d04e726.png)
加密用户才可以访问
![](https://img-blog.csdnimg.cn/direct/8032585902aa49c0a9efd09ac3b07139.png)
5.3基于客户端的访问控制
根据ip地址进行控制
拒绝192.168.233.62访问,允许其他所有访问
![](https://img-blog.csdnimg.cn/direct/d5a30727c1f94963a2512684ccf2322c.png)
![](https://img-blog.csdnimg.cn/direct/9ad84eaa3c4545d294eb7baf5a3f1e1b.png)
5.4基于域名的nginx的主机
修改配置文件
![](https://img-blog.csdnimg.cn/direct/2a3310fa5bf94493b212773d09995915.png)
![](https://img-blog.csdnimg.cn/direct/e9076710d161491ba12d417bf260f172.png)
vim /etc/hosts做一个本地映射
![](https://img-blog.csdnimg.cn/direct/87173acb20c64c2ca431908b85cda7bf.png)
访问成功
![](https://img-blog.csdnimg.cn/direct/6d558218ccf94b3e907350fa5506baeb.png)
多个server,修改配置文件
![](https://img-blog.csdnimg.cn/direct/962cc8b25456413ea5495503e66d65f5.png)
![](https://img-blog.csdnimg.cn/direct/1bb07e6ce9fc417c8179b75f763f3c41.png)
本地映射
![](https://img-blog.csdnimg.cn/direct/96c13acf6f5247eb97a594fa5a11d3e1.png)
访问成功
![](https://img-blog.csdnimg.cn/direct/630d622c5f384521b4c9f690129e9fe2.png)
5.5基于ip地址的虚拟主机
创建虚拟网卡
![](https://img-blog.csdnimg.cn/direct/231f3f6956a24cbaae7512ee280bdc22.png)
更改监听地址
![](https://img-blog.csdnimg.cn/direct/77a314e937474edf8dc069a7af90ae3e.png)
![](https://img-blog.csdnimg.cn/direct/0eba4e6d8937441ab48715e423630acf.png)
访问成功
![](https://img-blog.csdnimg.cn/direct/c2cbab1497554ab0a57ebf8ecad504dd.png)
5.6基于端口实现多个虚拟主机
更改端口号(范围1-65535)
![](https://img-blog.csdnimg.cn/direct/a829c332c8814d91bc8a3453bc64a4f1.png)
![](https://img-blog.csdnimg.cn/direct/d79c246718c54f27b47cf3c5a350bafe.png)
访问8080端口
![](https://img-blog.csdnimg.cn/direct/363a6e4d3ba04c96af77056d1f99ae97.png)
访问8888端口
![](https://img-blog.csdnimg.cn/direct/f402c2929312411eaae6eb843d5f332c.png)
5.7多个配置文件
进入配置文件,添加一个include
![](https://img-blog.csdnimg.cn/direct/be1a0df2c55f4e64a4de40810e3dd4d8.png)
创建conf.d目录
mkdir conf.d
创建新的文件test1.conf,添加server作为独立的配置文件
![](https://img-blog.csdnimg.cn/direct/05086ae247e140df9987fe0c8ac43b4f.png)
创建目录test1、test2
[root@localhost opt]# mkdir -p conf/test1
[root@localhost opt]# mkdir -p conf/test2
分别在test1、test2输入内容
[root@localhost conf]# echo "this is test1" > test1/index.html
[root@localhost conf]# echo "this is test2" > test2/index.html
访问成功
![](https://img-blog.csdnimg.cn/direct/cd7fc6c52df8483aa8187f756b76e77e.png)
![](https://img-blog.csdnimg.cn/direct/fb0d9d899ea34962bf20199f5e48e014.png)
6、nginx的优化与防盗链
6.1隐藏版本号
vim nginx.conf
![](https://img-blog.csdnimg.cn/direct/a1a46094cce04df891900b770409165f.png)
无版本号
![](https://img-blog.csdnimg.cn/direct/dbbc1cece3a54282be0bfd7f7d6b23b7.png)
6.2修改用户与组 ![](https://img-blog.csdnimg.cn/direct/e2e776cf0d354595a8d132c745db8ee3.png)
6.3设置页面的缓存时间,主要是针对动态页面
设置图片的缓存
![](https://img-blog.csdnimg.cn/direct/535d4e955b1246298091d64a038319ae.png)
6.4日志分割
apache是自带日志分割的,按天来进行收集日志
apache的日志access.log error.log
nginx没有自动分割的功能,借助脚本来实现分割
![](https://i-blog.csdnimg.cn/direct/ec5ea6f97b0a47ad83f331ec9196599d.png)
6.5更改进程数以及设置cpu绑定
![](https://i-blog.csdnimg.cn/direct/3f0c0a10478c4aa38344f06ec53f4333.png)
6.6连接超时
![](https://i-blog.csdnimg.cn/direct/0cbc9bf5a5aa48ce99ee22c24b71a9fe.png)
6.7配置页面压缩
gzip on
gzip_min_length 1k;
#最小的压缩文件,小于等于1k的文件就不压缩了
gzip_buffers 4 64k;
#设置压缩的缓冲区,4个,每个缓冲区的大小为64k
gzip_comp_level 6;
#压缩比例为1-9,数字越小,压缩的比例越小,速度越快;数字越大,压缩的比例就越高,速度越慢
gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss image/jpg image/jpeg image/png image/gif application/x-httpd-php application/javascript application/json;
#支持压缩的类型
6.8回收TIME_WAIT
time_wait是tcp连接当做的一种状态,出现在四次挥手之后,处于等待状态,双方不再发送数据
time_wait所占用的系统资源很小,数量比较少,完全可以忽略不计,但是太多了就有一定的影响。
连接断开(四次挥手)之后,尽快的把time_wait状态的连接进行回收
netstat -n | awk '/^tcp/ {++s[$NF]} END {for (a in s) print a s[a]}
#统计当前系统的连接状态
修改内核文件
![](https://i-blog.csdnimg.cn/direct/969adfa1a77d40d69f52bed2e28d6d39.png)
sysctl -p
#立即生效
6.9防盗链
![](https://i-blog.csdnimg.cn/direct/8690cdcd765e4d1c9992ef01feb3056f.png)
vim index.html
echo "192.168.230.10 <www.xy102.com>" >> /etc/hosts
echo "192.168.230.20 <www.xy103.com>" >> /etc/hosts
7、lnmp+DIS架构
是论坛的一个服务
l:linux 操作系统
n:nginx 前端页面的web服务
php:动态请求转发的中间件
m:mysql 数据库,保存用户和密码以及论坛的相关内容
![](https://i-blog.csdnimg.cn/direct/cbda00c4665f480a9bfdf4d0fceee91d.png)
7.1安装数据库
将mysql的包拖进去,并且解压
![](https://i-blog.csdnimg.cn/direct/faed2b2aadcb4079a5542a5b237ba72d.png)
将包改名为mysql,移动到/usr/local
![](https://i-blog.csdnimg.cn/direct/d9e15ad2b74a4d13ab84149ee38d8e78.png)
创建mysql的程序用户
[root@localhost opt]# useradd -M -s /sbin/nologin mysql
修改所有者和所在组
[root@localhost opt]# chown -R mysql.mysql /usr/local/mysql
修改mysql主配置文件的所有者和所在组
[root@localhost opt]# chown mysql.mysql /etc/my.cnf
修改主配置文件
vim /etc/my.cnf
port = 3306[client]
port = 3306
#客户端访问的端口
socket=/usr/local/mysql/mysql.sock
#指定mysql的通信套接字文件
[mysqld]
#mysql的安装目录
datadir=/usr/local/mysql/data
#mysql数据保存的目录
port = 3306
character-set-server=utf8
#字符集的编码
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
#任意地址都可以访问数据库
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
#mysql默认的存储引擎
max_allowed_packet=16M
server-id = 1
sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
#mysql支持的数据类型和相关的模块
将文件放到/etc/profile里,让系统可以识别
echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile
初始化数据库
./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
将文件复制到mysqld
给一个执行权限
chmod +x /etc/init.d/mysqld
读取配置文件,重启mysqld
systemctl daemon-reload
systemctl restart mysqld
查看是否有3306这个端口
![](https://i-blog.csdnimg.cn/direct/e671e02269014af8bdbf95bd24210745.png)
初始化数据库的密码
数据库安装完毕
在数据库中创建用户
mysql> create user 'root'@"%" identified by '123456';
Query OK, 0 rows affected (0.01 sec)
赋权
mysql> grant all privileges on *.* to 'root'@'%';
Query OK, 0 rows affected (0.00 sec)
刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
navicat远程登录,修改加密方式,刷新
mysql> alter user 'root'@'%' identified with mysql_native_password by "123456";
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
安装php的依赖环境
yum -y install gd \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel \
oniguruma-devel \
sqlite-devel
将php的安装包拖进来并解压
编译安装,开始配置
./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-pdo-mysql \
--with-openssl \
--with-sqlite-devel \
--with-oniguruma-devel \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip
配置成功
![](https://i-blog.csdnimg.cn/direct/888b2b52d73140a8aa85ce3b2c024a81.png)
安装
make -j 4 && make install
安装完成
![](https://i-blog.csdnimg.cn/direct/aabd0afb61ea4baa9c1deeb1ef1443a1.png)
优化两个线路,让系统识别到
[root@nginx1 php-8.1.27]# ln -s /usr/local/php/bin/* /usr/local/bin/
[root@nginx1 php-8.1.27]# ln -s /usr/local/php/sbin/* /usr/local/sbin/
将主配置文件php.ini-development复制到/usr/local/php/lib,改名为php.ini
cp php.ini-development /usr/local/php/lib/php.ini
打开/usr/local/php/lib/php.ini,修改976和1181行的内容
![](https://i-blog.csdnimg.cn/direct/5ddf10cb97ad49aca0f866f492b286f4.png)
![](https://i-blog.csdnimg.cn/direct/cfff11bce64741afbf918e85ef859904.png)
修改进程服务的配置文件
复制php-fpm.conf.default
![](https://i-blog.csdnimg.cn/direct/ab4989fe1b304b91abcc1684e90395d0.png)
打开php-fpm.conf,取消17行的注释
![](https://i-blog.csdnimg.cn/direct/ff15c6bfe9e549d28f2907f39c959109.png)
调整扩展配置文件
复制<www.conf.default>
![](https://i-blog.csdnimg.cn/direct/5a07ef9d1908457f8be3ad7a8dc206ed.png)
启动php
/usr/local/php/sbin/php-fpm -c
/usr/local/php/lib/php.ini
查看端口号
![](https://i-blog.csdnimg.cn/direct/e8da6a4aef304f13998e8ddc927a72b5.png)
添加系统配置
![](https://i-blog.csdnimg.cn/direct/d59fa013472549f889e2e90145d440f7.png)
配置nginx的动态转发
![](https://i-blog.csdnimg.cn/direct/6d41216301914bebb78417350907f31e.png)
![](https://i-blog.csdnimg.cn/direct/84ea76d7ca2842969175615638d246a9.png)
<?php
phpinfo();
?>
浏览器访问成功
![](https://i-blog.csdnimg.cn/direct/787f55a007574760b6fc2e8c5a648ccf.png)
进入数据库,创建专门用来保存论坛信息的用户和库
mysql -u root -p123456
创建库,名为bbs
create database bbs;
![](https://i-blog.csdnimg.cn/direct/1300ae0d6a384d31ad903b518515f630.png)
创建用户bbsuser,任意地址都可以连接,密码为admin123
create user 'bbsuser'@'%' identified by 'admin123'
赋权,刷新
mysql> grant all privileges on bbs.* to 'bbsuser'@'%';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
创建本地连接用户
create user 'bbsuser'@'localhost' identified by 'admin123';
赋权,刷新
mysql> grant all privileges on bbs.* to 'bbsuser'@'localhost';
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
指定用户登录
![](https://i-blog.csdnimg.cn/direct/70f068edfd084921a8221869c65fef81.png)
连接数据库成功
![](https://i-blog.csdnimg.cn/direct/62e0f45861e34376ac623348154af8c1.png)
7.2安装discuz论坛
将discuz安装包拖进去,解压
unzip Discuz_X3.5_SC_UTF8.zip -d /opt/dis
复制upload
![](https://i-blog.csdnimg.cn/direct/faae31ea6e884a9ea5614c673a4345bc.png)
修改所有者所在组和权限
[root@localhost html]# chown -R nginx.nginx bbs/
[root@localhost html]# chmod -R 777 bbs/
开始访问
安装成功
![](https://i-blog.csdnimg.cn/direct/b40e309ac51a44a0bb0dcf4b8776902d.png)
论坛搭建成功
![](https://i-blog.csdnimg.cn/direct/16289e6ae74c412c9ff6978e7b272f34.png)