Web服务器群集--Nginx服务与LNMP部署
Nginx属于一款轻量级的HTTP服务器软件,由俄罗斯的 "IgorSysoev" 开发,具有稳定、高效的特性。LNMP是指一组通常一起使用来运行动态网站或者服务器的自由软件名称首字母缩写。L指Linux,N指Nginx,M一般指MySQL,也可以指MariaDB,P一般指PHP,也可以指Perl或Python。
·
前言
- Nginx属于一款轻量级的HTTP服务器软件,由俄罗斯的 “Igor Sysoev” 开发,具有稳定、高效的特性。
- LNMP是指一组通常一起使用来运行动态网站或者服务器的自由软件名称首字母缩写。L指Linux,N指Nginx,M一般指MySQL,也可以指MariaDB,P一般指PHP,也可以指Perl或Python。
1.Nginx服务基础
1.1 概述
- Nginx专为性能优化而开发,其最知名的优点是它的稳定性和低系统资源消耗,以及对HTTP并发连续的高处理能力。正因为如此,大量提供社交网络、新闻资讯、电子商务及虚拟主机等 服务的企业纷纷选择Nginx来提供Web服务。
1.2 Nginx 编译安装
1.2.1 安装环境依赖包并创建用户和组
[root@localhost ~]# ls -lh //上传压缩包
[root@localhost ~]# systemctl stop firewalld //关闭防火墙设置
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# grep -v "#" /etc/selinux/config
SELINUX=disabled
SELINUXTYPE=targeted
[root@localhost ~]# yum -y install pcre-devel zlib-devel gcc gcc-c++ make //安装依赖包
[root@localhost ~]# useradd -M -s /sbin/nologin nginx //创建运行用户和组
[root@localhost ~]# tar zxvf nginx-1.12.2.tar.gz
1.2.2 编译安装Nginx
[root@localhost ~]# cd nginx-1.12.2/
[root@localhost nginx-1.12.2]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
[root@localhost nginx-1.12.2]# make && make install
1.2.3 路径优化
- 为了使Nginx服务器的运行更加方,可以为主程序nginx 创建链接文件;以便管理员直接指向"nginx"命令就可以调用Nginx的主程序。
[root@localhost nginx-1.12.2]# cd
[root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
1.2.4 Nginx的运行控制
- 检查配置文件
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
- 启动、停止Nginx
[root@localhost ~]# nginx //启动服务
[root@localhost ~]# netstat -anpt | grep nginx
tcp 0 0 192.168.140.15:80 0.0.0.0:* LISTEN 81424/nginx: master
[root@localhost ~]# killall -1 nginx //安全重启
[root@localhost ~]# killall -3 nginx //停止服务
- 制作Nginx脚本
[root@localhost ~]# vi /etc/init.d/nginx //制作nginx脚本,添加service管理Nginx
#!/bin/bash
#chkconfig: 35 80 20
#description: Nginx HTTP Server
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
reload)
kill -s HUP $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
[root@localhost ~]# chmod +x /etc/init.d/nginx
[root@localhost ~]# chkconfig --add nginx
[root@localhost ~]# chkconfig --list
1.3 Nginx.conf 配置文件应用
1.3.1 访问统计
[root@localhost ~]# ln -s /usr/local/nginx/conf/nginx.conf /etc/ //链接配置文件位置,使访问方便
[root@localhost ~]# vi /etc/nginx.conf //设置配置文件(数字代表行号)
1
2 user nginx nginx; //更改用户和组
3 worker_processes 4;
...... //省略部分信息
12 events {
13 use epoll;
14 worker_connections 4096; //更改进程连接数量
15 }
16
17
18 http {
19 include mime.types;
20 default_type application/octet-stream;
21
22 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' //去掉开头的#号,开启定义日志格式
23 '$status $body_bytes_sent "$http_referer" '
24 '"$http_user_agent" "$http_x_forwarded_for"';
25
26 #access_log logs/access.log main;
......
36 server {
37 listen 80;
38 server_name localhost;
39
40 charset utf-8;
41
42 access_log logs/aa.com.access.log main;
43
44 location / {
45 root html;
46 index index.html index.htm;
47 }
48 location /status { //访问位置
49 stub_status on; //开启状态统计模块
50 access_log off; //关闭此位置的访问日志
51 }
- 检查nginx用法,并重启该服务
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# ulimit -n
1024
[root@localhost ~]# ulimit -n 65535 >> /etc/rc.d/rc.local
[root@localhost ~]# ulimit -n
65535
[root@localhost ~]# systemctl stop nginx
[root@localhost ~]# systemctl start nginx
- 初步测验,在浏览器访问该虚拟主机地址
1.3.2 访问统计测试
- 访问 IP/status 时,出现以下信息:
- 字段的含义如下
Active connections: 1 //活跃的连接数量为1
server accepts handled requests //共处理了5个连接。成功创建了5个连接,总共处理了5个请求
5 5 5
Reading: 0 writing: 1 waiting: 0 //读取客户端的连接数为0,响应数据到客户端的数量为1,等待下一次请求指令的驻留连接
2.Nginx访问控制
2.1 基于授权的访问控制
2.1.1 简介
- Nginx 与 Apache 一样,可以实现基于用户授权的访问控制,当客户想要访问相应的网站或者目录时,要求输入用户名和密码才能访问,配置与 Apache 基本一致。
- 网页认证实现步骤可以概括为:
1)生成用户密码认证文件;
2)修改主配置文件相应目录,添加认证配置项;
3)重启服务,访问测试。
2.1.2 实现步骤
- 生成用户密码认证文件,并添加认证配置
[root@localhost ~]# yum -y install httpd-tools //安装tools工具
[root@localhost ~]# htpasswd -c /usr/local/nginx/passwd.db jack //生成用户密码认证文件
New password:
Re-type new password:
Adding password for user jack
[root@localhost ~]# chown nginx /usr/local/nginx/passwd.db //将所有者改为nginx
[root@localhost ~]# vi /etc/nginx.conf //修改主配置文件,添加相应认证配置项
...
location / {
root html;
index index.html index.htm;
auth_basic "secret"; //添加认证配置
auth_basic_user_file /usr/local/nginx/passwd.db;
}
- 检测语法,重启服务
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# systemctl stop nginx
[root@localhost ~]# systemctl start nginx
- 访问测验
测验当在浏览器访问虚拟机IP地址时,出现安全登录窗口,输入
用户:jack
密码:生成密码认证文件设置的密码 可完成登录
2.2 基于客户端的访问控制
2.2.1 基于客户端的访问控制简介
- 基于客户端的访问控制是通过客户端IP地址,决定是否允许对页面访问。
- 访问规则如下:
1)deny IP/IP段:拒绝某个IP或IP段的客户端访问;
2)allow IP/IP段:允许某个IP或IP段的客户端访问;
3)规则从上往下执行、如匹配则停止,不再往下匹配。
2.2.2 基于客户端的访问控制步骤
1)当设置先允许所有IP访问,再拒绝某个IP访问时
[root@localhost ~]# vi /etc/nginx.conf
...
location / {
root html;
index index.html index.htm;
allow all; //允许所有IP访问
deny 192.168.140.15/32; //拒绝该IP访问
auth_basic "secret";
auth_basic_user_file /usr/local/nginx/passwd.db;
}
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# systemctl stop nginx
[root@localhost ~]# systemctl start nginx
- 访问测试
清除浏览数据后,发现仍然能登录
2)如果设置先拒绝某个IP发访问,再允许所有IP访问
[root@localhost ~]# vi /etc/nginx.conf
...
location / {
root html;
index index.html index.htm;
deny 192.168.140.15/32; //拒绝该IP访问
allow all; //允许所有IP访问
auth_basic "secret";
auth_basic_user_file /usr/local/nginx/passwd.db;
}
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# systemctl stop nginx
[root@localhost ~]# systemctl start nginx
- 访问测试
清除浏览器缓存,在浏览器访问地址,访问被距离
3.Nginx虚拟主机
3.1 虚拟主机的概述
- 虚拟主机提供了在同一台服务器、同一个Nginx进程上运行多个网站的功能。与Apache 一样,Nginx 也可以配置多种类型的虚拟主机,分别是:基于IP的虚拟主机、基于域名的虚拟主机、基于端口的虚拟主机。
- 注意:使用 Nginx 搭建虚拟主机服务器时,每个虚拟Web站点拥有独立的 "server{ }"配置段,各自监听的 IP 地址、端口号可以单独指定,当然网站名称也是不同的。
3.2 基于域名的虚拟主机
注意:若在之前的环境下操作,需要删除验证nginx模块(即访问权限相关设置),并将server模块的部分配置还原
[root@localhost ~]# vi /etc/nginx.conf
...
location / {
root html;
index index.html index.htm;
}
- 配置步骤
[root@localhost ~]# vi /etc/nginx.conf
...
http {
...
server {
listen 80;
server_name www.aa.com; //设置域名,添加站点
charset utf-8;
access_log logs/aa.com.access.log main;
location / {
root /var/www/aa; //设置域名的工作目录
index index.html index.htm;
}
}
...... //在末尾http的结尾标记前,添加一个server模块
server {
listen 80;
server_name www.ab.com;
charset utf-8;
access_log logs/ab.com.access.log;
location / {
root /var/www/ab;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html; //复制第一个server模块的错误日志
location = /50x.html {
root html;
}
}
}
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# systemctl stop nginx
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# mkdir -p /var/www/aa //创建www.aa.com的根目录
[root@localhost ~]# mkdir -p /var/www/ab //创建www.ab.com的根目录
[root@localhost ~]# echo "<h1>This is server aa site.</h1>" > /var/www/aa/index.html
//创建默认页,并设置网页内容为This is server aa site.
[root@localhost ~]# echo "<h1>This is server ab site.</h1>" > /var/www/ab/index.html
//创建默认页,并设置网页内容为This is server ab site.
[root@localhost ~]# vi /etc/hosts //添加映射
192.168.140.15 www.aa.com www.ab.com
[root@localhost ~]# curl http://www.aa.com
<h1>This is server aa site.</h1>
[root@localhost ~]# curl http://www.ab.com
<h1>This is server ab site.</h1>
3.3 基于IP的虚拟主机
- 注意:一台主机如果有多个IP[地址,可以设置每一个IP对应一个站点。主机安装多个网卡可以有多个IP,这里采用虚拟IP的方式使得主机有多个IP。
[root@localhost ~]# ifconfig ens33:1 192.168.1.11/24 //添加虚拟网卡
[root@localhost ~]# vi /etc/nginx.conf
...
http {
...
server {
listen 192.168.140.15:80; //在端口前添加地址
server_name www.aa.com;
...
}
...
server {
listen 192.168.1.11:80;
server_name www.ab.com;
...
}
}
......
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# systemctl stop nginx
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# netstat -anpt | grep nginx
tcp 0 0 192.168.140.15:80 0.0.0.0:* LISTEN 81424/nginx: master
[root@localhost ~]# curl http://192.168.140.15
<h1>This is server aa site.</h1>
[root@localhost ~]# curl http://192.168.1.11
<h1>This is server ab site.</h1>
3.4 基于端口的虚拟主机
[root@localhost ~]# vi /etc/nginx.conf
...
server {
listen 192.168.1.11:8080;
server_name www.ab.com;
...
}
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# systemctl stop nginx
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# netstat -anpt | grep nginx
tcp 0 0 192.168.140.15:80 0.0.0.0:* LISTEN 81424/nginx: master
tcp 0 0 192.168.140.15:8080 0.0.0.0:* LISTEN 81424/nginx: master
4.LNMP架构的部署
- 注意:若在之前的环境下操作,需要删除 nginx 主配置文件下的第二个server模块,然后重启nginx服务,再进行以下配置
4.1 安装 MySQL 数据库
4.1.1 安装MySQL环境依赖包
[root@localhost ~]# yum -y install ncurses ncurses-devel bison cmake
'//ncurses-devel是字符终端下屏幕控制的基本库'
'//bison 函数库'
'//cmake跨平台编译安装工具'
4.1.2 创建运行用户
[root@localhost ~]# useradd -s /sbin/nologin mysql '//添加用户,指定shell,禁止用户登录系统'
4.1.3 cmake配置与编译安装
[root@localhost ~]# tar zxvf mysql-boost-5.7.20.tar.gz
[root@localhost ~]# cd mysql-5.7.20/
[root@localhost mysql-5.7.20]# cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1
'//由于数据库较大,配置需要时间,请耐心等待'
[root@localhost mysql-5.7.20]# make && make install
-----注意:如果在CMAKE的过程中有报错---
当报错解决后,需要把源码目录中的CMakeCache.txt文件删除,然后再重新CMAKE,否则错误依旧
----注意: make: *** No targets specified and no makefile found. Stop.解决方法
1.wget http://ftp.gnu.org/pub/gnu/ncurses/ncurses- 5.6.tar.gz
2.tar zxvf ncurses-5.6.tar.gz
3./configure -prefix= /usr/local -with- shared-without- debug
4.make
5.make install
4.1.4 数据库优化调整
[root@localhost mysql-5.7.20]# chown -R mysql.mysql /usr/local/mysql
[root@localhost mysql-5.7.20]# vi /etc/my.cnf '//将内容全部删除,添加以下内容'
[client]
port = 3306
default-character-set = utf8
socket = /usr/local/mysql/mysql.sock
[mysql]
port = 3306
default-character-set = utf8
socket = /usr/local/mysql/mysql.sock
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server = utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1 //区分数据库,与UUID类似
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
[root@localhost mysql-5.7.20]# cd
[root@localhost ~]# chown mysql:mysql /etc/my.cnf
[root@localhost ~]# echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
//设置环境变量
[root@localhost ~]# echo 'export PATH' >> /etc/profile
[root@localhost ~]# source /etc/profile //让配置立即生效
4.1.5 初始化数据库
[root@localhost ~]# mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
4.1.6 启动mysql服务
[root@localhost ~]# cd /usr/local/mysql/
[root@localhost mysql]# cp usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system
//Linux下启动的第一个进程/usr/lib/systemd/system
[root@localhost mysql]# systemctl start mysqld
[root@localhost mysql]# systemctl enable mysqld
[root@localhost mysql]# systemctl status mysqld
[root@localhost mysql]# netstat -anpt | grep 3306
tcp6 0 0 :::3306 :::* LISTEN 75307/mysqld
[root@localhost mysql]# mysql
...
mysql>use mysql;
mysql>update user set authentication_string=password('abc123') where user='root'; //创建root用户,密码为abc123
mysql>flush privileges;
mysql>exit
4.2 安装PHP解析环境
4.2.1 安装依赖环境
[root@localhost ~]# yum -y install \
libjpeg \
libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 \
libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel
4.2.2 编译安装PHP
[root@localhost ~]# tar jxvf php-7.1.10.tar.bz2
[root@localhost ~]# cd php-7.1.10
[root@localhost php-7.1.10]# ./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-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip
[root@localhost php-7.1.10]# make && make install
4.2.3 复制模板文件作为PHP的主配置文件
[root@localhost php-7.1.10]# cp php.ini-development /usr/local/php/lib/php.ini
4.2.4 修改PHP配置文件
[root@localhost php-7.1.10]# vi /usr/local/php/lib/php.ini
...
mysqli.default_socket = /usr/local/mysql/mysql.sock //:1170查找到该行,添加配置
date.timezone = Asia/Shanghai //:939查找到该行,添加配置
[root@localhost php-7.1.10]# /usr/local/php/bin/php -m | wc -l //验证安装的模块
38
[root@localhost php-7.1.10]# cd
[root@localhost ~]# cd /usr/local/php/etc/
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]# cd /usr/local/php/etc/php-fpm.d/
[root@localhost php-fpm.d]# cp www.conf.default www.conf
[root@localhost php-fpm.d]# cd /usr/local/php/etc/
[root@localhost etc]# vi php-fpm.conf
...
pid = run/php-fpm.pid
[root@localhost etc]# cd
[root@localhost ~]# /usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini
[root@localhost ~]# netstat -anpt | grep 9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 80711/php-fpm: mast
[root@localhost ~]# ln -s /usr/local/php/bin/* /usr/bin/
[root@localhost ~]# vi /etc/nginx.conf
... //查找php模块(第二个)
location ~ \.php$ { //开启该模块,去掉该模块每一行开头的注释
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; //更改这行配置
include fastcgi_params;
}
...
- 检查语法,并重启nginx服务
[root@localhost ~]# nginx -t
[root@localhost ~]# systemctl stop nginx
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# vi /usr/local/nginx/html/index.php //设置PHP访问页面
<?php
phpinfo();
?>
4.2.5 测验PHP环境是否配置成功
- 在浏览器上测试,输入IP/index.php 会出现PHP网页界面
4.2.6 测试MySQL的页面访问
[root@localhost ~]# mysql -uroot -pabc123
...
mysql>create database bbs;
mysql>show databases;
mysql>grant all privileges on bbs.* to 'bbsuser'@'%' identified by 'admin123';
mysql>grant all privileges on bbs.* to 'bbsuser'@'localhost' identified by 'admin123';
mysql>flush privileges;
mysql>show grants for 'bbsuser'@'localhost';
mysql>exit
[root@localhost ~]# vi /usr/local/nginx/html/index.php
<?php
$link=mysqli_connect('192.168.140.15','bbsuser','admin123');
if($link) echo "<h1>Success!!</h1>";
else echo "Fail!!";
?>
5.搭建Discuz论坛
5.1 解压论坛安装包
[root@localhost ~]# unzip Discuz_X3.4_SC_UTF8.zip
[root@localhost ~]# cd dir_SC_UTF8/
[root@localhost dir_SC_UTF8]# cp -r upload/ /usr/local/nginx/html/bbs
[root@localhost dir_SC_UTF8]# cd /usr/local/nginx/html/bbs/
[root@localhost bbs]# ls -lh
5.2 设置论坛权限
[root@localhost bbs]# chown -R root:nginx ./config/
[root@localhost bbs]# chown -R root:nginx ./data/
[root@localhost bbs]# chown -R root:nginx ./uc_client/
[root@localhost bbs]# chown -R root:nginx ./uc_server/
[root@localhost bbs]# chmod -R 777 ./config/
[root@localhost bbs]# chmod -R 777 ./data/
[root@localhost bbs]# chmod -R 777 ./uc_client/
[root@localhost bbs]# chmod -R 777 ./uc_server/
5.3 访问论坛
http://192.168.140.15/bbs/install/index.php
数据库服务器:localhost
要填写IP地址和端口号
数据库名字:bbs
数据库用户名:bbsuser
数据库密码:admin123
管理员账号:root
管理员密码:abc123
http://IP/bbs/admin.php//管理后台
账户:admin
密码:admin123
访问论坛 http://192.168.140.15/bbs/index.php
- 安装向导
此刻表示论坛安装完成!
- 安装完成后,还可以确认是否安装
此刻表示论坛搭建完成!
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献1条内容
所有评论(0)