一、LNMP架构及应用部署
众所周知,LAMP平台时目前应用最为广泛的网站服务器架构,其中的“A”对应着web服务软件的Apache HTTP Server ,随着Nginx在工作环境中的使用越来越多,LNMP(或LEMP)架构也受到越来越多的Linux运维工程师的青睐。
就像构建LAMP平台一样,构建LNMP平台也需要Linux服务器、MySQL数据库、PHP解析环境,区别主义在于Nginx与PHP的协作配置上。
准备工作
Centos 7操作系统一台;
Windows 客户端一台;
案例所需镜像及软件包请访问:https://pan.baidu.com/s/10wFG1YQaY2FTJKgMp1x0kw
提取码:rl3i
二、构建LNMP网站平台
部署前准备
①挂载Linux光盘,拷贝nginx依赖程序到/usr/src/目录
[[email protected] ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[[email protected] ~]# cp /mnt/nginx-1.6.0.tar.gz /usr/src/
②切换LAMP光盘,将mnt目录下所有数据拷贝到/usr/src/目录[[email protected] ~]# umount /mnt/
[[email protected] ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[[email protected] ~]# cp /mnt/* /usr/src/
③切换到操作系统光盘[[email protected] ~]# umount /mnt/
[[email protected] ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
1、部署Nginx静态网站
Nginx具体配置及概述请访问博客:Centos 7部署Nginx网站服务
[[email protected] ~]# rm -rf /etc/yum.repos.d/CentOS-*
<!--清除系统自带yum源 -->
[[email protected] ~]# yum -y install pcre-devel zlib-devel
<!--安装Nginx的依赖程序-->
[[email protected] ~]# useradd -M -s /sbin/nologin nginx
<!--创建管理Nginx的用户-->
[[email protected] ~]# tar zxvf /usr/src/nginx-1.6.0.tar.gz -C /usr/src/
<!--解压缩Nginx软件包-->
[[email protected] ~]# cd /usr/src/nginx-1.6.0/
[[email protected] nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
<!--配置Nginx-->
[[email protected] nginx-1.6.0]# make && make install <!--编译安装Nginx-->
[[email protected] ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ <!--优化Nginx执行命令-->
[[email protected] ~]# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
<!--备份主配置文件-->
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf<!--编辑主配置文件-->
3 user nginx;
4 worker_processes 1;
6 error_log logs/error.log;
12 pid logs/nginx.pid;
16 use epoll;
17 worker_connections 1024;
29 #access_log logs/access.log main;
31 sendfile on;
35 keepalive_timeout 65;
39 server {
40 listen 80;
41 server_name localhost;
44 charset utf-8;
48 location / {
49 root html;
50 index index.html index.htm;
51 }
84 }
[[email protected] ~]# nginx <!--启动Nginx服务-->
[[email protected] ~]# netstat -anptu | grep nginx <!--监听Nginx服务-->
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4663/nginx: master
[[email protected] ~]# vim /etc/init.d/nginx <!--编写Nginx服务管理脚本-->
#!/bin/bash
#chkconfig: 35 90 30
#description:nginx 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)
;;
restart)
$0 stop
$0 start
;;
reload)
kill 0s HUP $(cat $PIDF)
;;
*)
echo "Usage:$0 (start|stop|restart|reload)"
exit 1
esac
exit 0
[[email protected] ~]# chmod +x /etc/init.d/nginx<!--添加脚本执行权限-->
[[email protected] ~]# chkconfig --add nginx <!--添加系统服务-->
[[email protected] ~]# chkconfig --level 35 nginx on <!--设置开机自动启动-->
[[email protected] ~]# /etc/init.d/nginx stop<!--脚本停止Nginx服务-->
[[email protected] ~]# /etc/init.d/nginx start<!--脚本启动Nginx服务-->
[[email protected] ~]# /etc/init.d/nginx restart <!--脚本重启Nginx服务-->
至此,可以配置客户端配置与服务器同网卡、同网段的IP地址及网关,访问Nginx网站
2、部署基于域名的虚拟主机
[[email protected] ~]# yum -y install bind bind-chroot bind-utils <!--安装DNS-->
[[email protected] ~]# echo "" > /etc/named.conf <!--清空主配置文件-->
[[email protected] ~]# vim /etc/named.conf <!--修改主配置文件-->
options {
listen-on port 53 { 192.168.100.20; };
directory "/var/named";
}
zone "benet.com" IN {
type master;
file "benet.com.zone";
}
zone "accp.com" IN {
type master;
file "accp.com.zone";
}
[[email protected] ~]# named-checkconf -z /etc/named.conf
<!--检查主配置文件是否配置错误-->
[[email protected] ~]# vim /var/named/benet.com.zone
<!--配置benet.com的正向解析区域-->
$TTL 86400
@ SOA benet.com. root.benet.com(
2019112801
1H
15M
1W
1D
)
@ NS centos02.benet.com.
centos02 A 192.168.100.20
www A 192.168.100.20
[[email protected] ~]# chmod +x /var/named/benet.com.zone
<!--正向解析区域配置文件添加执行权限-->
[[email protected] ~]# chown named:named /var/named/benet.com.zone
<!--修改属主属组-->
[[email protected] ~]# named-checkzone benet.com /var/named/benet.com.zone
<!--检查benet.com正向解析区域配置文件是否错误-->
zone benet.com/IN: loaded serial 2019112801
OK
[[email protected] ~]# cp /var/named/benet.com.zone /var/named/accp.com.zone
<!--复制benet.com正向解析区域到accp.com正向解析区域-->
[[email protected] ~]# vim /var/named/accp.com.zone
<!--修改accp.com正向解析区域配置文件-->
$TTL 86400
@ SOA accp.com. root.accp.com(
2019112801
1H
15M
1W
1D
)
@ NS centos02.accp.com.
centos02 A 192.168.100.20
www A 192.168.100.20
[[email protected] ~]# named-checkzone accp.com /var/named/accp.com.zone
<!--检查accp.com正向解析区域配置文件是否错误-->
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32<!--编辑网卡添加主DNS-->
DNS1=192.168.100.20 <!--添加DNS-->
[[email protected] ~]# systemctl restart network <!--重启网卡服务-->
[[email protected] ~]# systemctl start named <!--启动DNS服务器-->
[[email protected] ~]# systemctl enable named <!--设置开机自动启动-->
[[email protected] ~]# nslookup www.benet.com <!--解析域名测试是否正常-->
Server: 192.168.100.20
Address: 192.168.100.20#53
Name: www.benet.com
Address: 192.168.100.20
[[email protected] ~]# nslookup www.accp.com <!--解析域名测试是否正常-->
Server: 192.168.100.20
Address: 192.168.100.20#53
Name: www.accp.com
Address: 192.168.100.20
[[email protected] ~]# mkdir -p /var/www/benetcom <!--创建虚拟主机网站根目录-->
[[email protected] ~]# mkdir -p /var/www/accpcom <!--创建虚拟主机网站根目录-->
[[email protected] ~]# echo "www.benet.com" > /var/www/benetcom/index.html
<!--创建虚拟主机的网站主页-->
[[email protected] ~]# echo "www.accp.com" > /var/www/accpcom/index.html
<!--创建虚拟主机的网站主页-->
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf <!--修改Nginx主配置文件支持虚拟主机-->
server {
listen www.benet.com:80;
server_name www.benet.com;
charset utf-8;
access_log logs/www.benet.com.access.log;
error_log logs/www.benet.com.error.log;
location / {
root /var/www/benetcom/;
index index.html;
}
}
server {
listen www.accp.com:80;
server_name www.accp.com;
charset utf-8;
access_log logs/www.accp.com.access.log;
error_log logs/www.accp.com.error.log;
location / {
root /var/www/accpcom/;
index index.html;
}
}
[[email protected] ~]# nginx -t <!--检查Nginx是否配置错误-->
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
[[email protected] ~]# systemctl restart named <!--重新启动DNS服务-->
[[email protected] ~]# /etc/init.d/nginx restart <!--重新启动Nginx服务-->
至此客户端添加DNS地址,访问域名即可
3、部署MySQL数据库
MySQL数据库具体配置及概述请访问博文:Centos安装MySQL数据库
[[email protected] ~]# groupadd mysql <!--创建管理组-->
[[email protected] ~]# useradd -M -s /sbin/nologin -g mysql mysql
<!--创建管理Mysql的用户-->
[[email protected] ~]# yum -y install ncurses-devel <!--安装MySQL依赖程序-->
[[email protected] ~]# tar zxvf /usr/src/cmake-2.8.6.tar.gz -C
/usr/src/ <!--解压缩cmake软件包-->
[[email protected] ~]# cd /usr/src/cmake-2.8.6/
[[email protected] cmake-2.8.6]# ./configure && gmake && gmake install <!--编译配置及安装gmake-->
[[email protected] ~]# tar zxvf /usr/src/mysql-5.5.22.tar.gz -C
/usr/src/ <!--解压缩MySQL软件包-->
[[email protected] mysql-5.5.22]# cmake -
DCMAKE_INSTALL_PREFIX=/usr/local/mysql
-DDEFAULT_CHARSET=utf8
-DDEFAULT_COLLATION=utf8_general_ci
-DWITH_EXTRA_CHARSETS=all -DSYSCONFDIR=/etc
<!--配置MySQL-->
[[email protected] mysql-5.5.22]# make && make install <!--编译安装MySQL-->
[[email protected] mysql-5.5.22]# cp support-files/my-medium.cnf /etc/my.cnf
<!--生成MySQL配置文件-->
cp:是否覆盖"/etc/my.cnf"? y <!--输入y确定覆盖-->
[[email protected] mysql-5.5.22]# cp support-files/mysql.server
/etc/init.d/mysqld <!--生成MySQL服务配置文件-->
[[email protected] ~]# chmod +x /etc/init.d/mysqld <!--添加执行权限-->
[[email protected] ~]# chkconfig --add mysqld <!--添加系统服务-->
[[email protected] ~]# chkconfig --level 35 mysqld on <!--设置开机自动启动-->
[[email protected] ~]# vim /etc/profile <!--优化MySQL程序命令-->
PATH=$PATH:/usr/local/mysql/bin/
[[email protected] ~]# source /etc/profile <!--执行优化-->
[[email protected] ~]# /usr/local/mysql/scripts/mysql_install_db
--user=mysql --basedir=/usr/local/mysql
--datadir=/usr/local/mysql/data <!--初始化MySQL数据库-->
[[email protected] ~]# systemctl start mysqld <!--启动MySQL服务-->
[[email protected] ~]# systemctl enable mysqld <!--设置开机自动启动-->
[[email protected] ~]# mysqladmin -u root password
<!--设置MySQL数据密码-->
New password: <!--输入密码-->
Confirm new password: <!--确定密码-->
[[email protected] ~]# mysql -uroot [email protected]
<!--测试是否可以正常登录数据库-->
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
4、部署PHP解析环境
1)配置php依赖程序
[[email protected] ~]# yum -y install gd libxml2-devel libjpeg-devel libpng-devel
<!--安装php依赖程序-->
[[email protected] ~]# tar zxvf /usr/src/php-5.3.28.tar.gz -C /usr/src/
<!--解压缩php软件包-->
[[email protected] ~]# cd /usr/src/php-5.3.28/
[[email protected] php-5.3.28]# ./configure --prefix=/usr/local/php
--with-config-file-path=/usr/local/php --with-mysql=/usr/local/mysql/
--with-mysqli=/usr/local/mysql/bin/mysql_config --with-gd
--with-zlib --with-jpeg-dir=/usr/lib --enable-mbstring
--enable-fpm <!--配置php-->
[[email protected] php-5.3.28]# make && make install <!--编译安装php-->
[[email protected] php-5.3.28]# ls -ld /usr/local/php/ <!--查看是否安装成功-->
drwxr-xr-x 10 root root 101 11月 28 20:04 /usr/local/php/
2)生成PHP配置文件优化程序执行位置
[[email protected] php-5.3.28]# cp php.ini-production
/usr/local/php/php.ini <!--生成服务配置文件-->
[[email protected] ~]# ln -s /usr/local/php/sbin/* /usr/local/sbin/
<!--优化程序执行位置-->
[[email protected] ~]# ln -s /usr/local/php/bin/* /usr/local/bin/
<!--优化程序执行位置-->
[[email protected] ~]# tar zxvf /usr/src/zendguardloader-php-5.3-linux-glibc23-i386.tar.gz
-C /usr/src/ <!--解压缩zend模块软件包-->
[[email protected] ~]# mv /usr/src/ZendGuardLoader-php-5.3-linux-glibc23-i386
/php-5.3.x/ZendGuardLoader.so /usr/local/php/lib/php/
<!--php加载zend模块加速访问,修改zend模块位置-->
[[email protected] ~]# vim /usr/local/php/php.ini <!--修改php主配置文件加载zend-->
zend_extension=/usr/local/php/lib/php/ZendGuardLoader.so
zend_loader.enable=1
3)配置php-fpm支持加载动态网站
[[email protected] ~]# cp /usr/local/php/etc/php-fpm.conf.default
/usr/local/php/etc/php-fpm.conf <!--生成php-fpm配置文件-->
[[email protected] ~]# vim /usr/local/php/etc/php-fpm.conf
<!--修改php-fpm主配置文件-->
26 pid = run/php-fpm.pid
141 user = nginx
142 group = nginx
[[email protected] ~]# /usr/local/sbin/php-fpm <!--启动php-fpm服务-->
[[email protected] ~]# netstat -anptu | grep 9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 127332/php-fpm: mas
4)配置nginx主配置文件支持php,配置php到www.benet.com的虚拟主机
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
<!--修改主配置文件支持php-fpm-->
location ~ \.php$ {
root /var/www/benetcom;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
[[email protected] ~]# 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
[[email protected] ~]# vim /var/www/benetcom/index.php
<!--编写php测试文件-->
<?php
phpinfo();
?>
[[email protected] ~]# /etc/init.d/nginx restart <!--重新启动Nginx服务-->
至此,客户端可以访问PHP测试页
三、基于LNMP架构搭建开源论坛
1、修改Nginx主配置文件,配置php到www.accp.com虚拟主机支持php-fpm
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
104 server {
105 listen www.accp.com:80;
106 server_name www.accp.com;
107 charset utf-8;
108 access_log logs/www.accp.com.access.log;
109 error_log logs/www.accp.com.error.log;
110 location / {
111 root /var/www/accpcom/;
112 index index.html index.php;
113 }
114 location ~ \.php$ {
115 root /var/www/accpcom;
116 fastcgi_pass 127.0.0.1:9000;
117 fastcgi_index index.php;
118 include fastcgi.conf;
119 }
2、上传论坛的源代码包到服务器
3、配置论坛
[[email protected] ~]# unzip Discuz_X3.2_SC_UTF8.zip
<!--解压缩论坛-->
[[email protected] ~]# ls
anaconda-ks.cfg Discuz_X3.2_SC_UTF8.zip readme utility
Discuz_X3.2_SC_UTF8 initial-setup-ks.cfg upload
[[email protected] ~]# mv ./upload/* /var/www/accpcom/<!--移动论坛到网站根目录-->
[[email protected] ~]# chmod -R +x /var/www/accpcom/
<!--设置执行权限-->
[[email protected] ~]# chown -R nginx:nginx /var/www/accpcom/
<!--设置网站的所有者-->
[[email protected] ~]# mysql -uroot [email protected]
mysql> create database bbs;
<!--创建论坛的数据库-->
mysql> grant all on bbs.* to [email protected] identified by ‘[email protected]‘;
<!--授权bbs账号管理bbs数据库-->
[[email protected] ~]# /etc/init.d/nginx restart <!--重新启动Nginx服务-->
4、客户端访问论坛网站
1)客户端浏览器输入www.accp.com/install访问,单击我同意开始安装
2)检查安装环境
3)选择全新安装,单击下一步
4)设置数据库的用户及密码
5)等待安装完成即可
6)删除accp.com网站的默认首页[[email protected] ~]# rm -rf /var/www/accpcom/index.html
7)客户端访问
8)登录后台管理网站
至此论坛就已经部署完成。
—————— 本文至此结束,感谢阅读 ——————
原文地址:https://blog.51cto.com/14156658/2457346