xinlugang 2019-12-11
众所周知,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
部署前准备
①挂载Linux光盘,拷贝nginx依赖程序到/usr/src/目录
[ ~]# mount /dev/cdrom /mnt/ mount: /dev/sr0 写保护,将以只读方式挂载 [ ~]# cp /mnt/nginx-1.6.0.tar.gz /usr/src/
②切换LAMP光盘,将mnt目录下所有数据拷贝到/usr/src/目录[ ~]# umount /mnt/
[ ~]# mount /dev/cdrom /mnt/ mount: /dev/sr0 写保护,将以只读方式挂载 [ ~]# cp /mnt/* /usr/src/
③切换到操作系统光盘[ ~]# umount /mnt/
[ ~]# mount /dev/cdrom /mnt/ mount: /dev/sr0 写保护,将以只读方式挂载
Nginx具体配置及概述请访问博客:Centos 7部署Nginx网站服务
[ ~]# rm -rf /etc/yum.repos.d/CentOS-* <!--清除系统自带yum源 --> [ ~]# yum -y install pcre-devel zlib-devel <!--安装Nginx的依赖程序--> [ ~]# useradd -M -s /sbin/nologin nginx <!--创建管理Nginx的用户--> [ ~]# tar zxvf /usr/src/nginx-1.6.0.tar.gz -C /usr/src/ <!--解压缩Nginx软件包--> [ ~]# cd /usr/src/nginx-1.6.0/ [ nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module <!--配置Nginx--> [ nginx-1.6.0]# make && make install <!--编译安装Nginx--> [ ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ <!--优化Nginx执行命令--> [ ~]# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak <!--备份主配置文件--> [ ~]# 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 } [ ~]# nginx <!--启动Nginx服务--> [ ~]# netstat -anptu | grep nginx <!--监听Nginx服务--> tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4663/nginx: master [ ~]# 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 [ ~]# chmod +x /etc/init.d/nginx<!--添加脚本执行权限--> [ ~]# chkconfig --add nginx <!--添加系统服务--> [ ~]# chkconfig --level 35 nginx on <!--设置开机自动启动--> [ ~]# /etc/init.d/nginx stop<!--脚本停止Nginx服务--> [ ~]# /etc/init.d/nginx start<!--脚本启动Nginx服务--> [ ~]# /etc/init.d/nginx restart <!--脚本重启Nginx服务-->
至此,可以配置客户端配置与服务器同网卡、同网段的IP地址及网关,访问Nginx网站
[ ~]# yum -y install bind bind-chroot bind-utils <!--安装DNS--> [ ~]# echo "" > /etc/named.conf <!--清空主配置文件--> [ ~]# 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"; } [ ~]# named-checkconf -z /etc/named.conf <!--检查主配置文件是否配置错误--> [ ~]# 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 [ ~]# chmod +x /var/named/benet.com.zone <!--正向解析区域配置文件添加执行权限--> [ ~]# chown named:named /var/named/benet.com.zone <!--修改属主属组--> [ ~]# named-checkzone benet.com /var/named/benet.com.zone <!--检查benet.com正向解析区域配置文件是否错误--> zone benet.com/IN: loaded serial 2019112801 OK [ ~]# cp /var/named/benet.com.zone /var/named/accp.com.zone <!--复制benet.com正向解析区域到accp.com正向解析区域--> [ ~]# 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 [ ~]# named-checkzone accp.com /var/named/accp.com.zone <!--检查accp.com正向解析区域配置文件是否错误--> [ ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32<!--编辑网卡添加主DNS--> DNS1=192.168.100.20 <!--添加DNS--> [ ~]# systemctl restart network <!--重启网卡服务--> [ ~]# systemctl start named <!--启动DNS服务器--> [ ~]# systemctl enable named <!--设置开机自动启动--> [ ~]# nslookup www.benet.com <!--解析域名测试是否正常--> Server: 192.168.100.20 Address: 192.168.100.20#53 Name: www.benet.com Address: 192.168.100.20 [ ~]# nslookup www.accp.com <!--解析域名测试是否正常--> Server: 192.168.100.20 Address: 192.168.100.20#53 Name: www.accp.com Address: 192.168.100.20 [ ~]# mkdir -p /var/www/benetcom <!--创建虚拟主机网站根目录--> [ ~]# mkdir -p /var/www/accpcom <!--创建虚拟主机网站根目录--> [ ~]# echo "www.benet.com" > /var/www/benetcom/index.html <!--创建虚拟主机的网站主页--> [ ~]# echo "www.accp.com" > /var/www/accpcom/index.html <!--创建虚拟主机的网站主页--> [ ~]# 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; } } [ ~]# 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 [ ~]# systemctl restart named <!--重新启动DNS服务--> [ ~]# /etc/init.d/nginx restart <!--重新启动Nginx服务-->
至此客户端添加DNS地址,访问域名即可
MySQL数据库具体配置及概述请访问博文:Centos安装MySQL数据库
[ ~]# groupadd mysql <!--创建管理组--> [ ~]# useradd -M -s /sbin/nologin -g mysql mysql <!--创建管理Mysql的用户--> [ ~]# yum -y install ncurses-devel <!--安装MySQL依赖程序--> [ ~]# tar zxvf /usr/src/cmake-2.8.6.tar.gz -C /usr/src/ <!--解压缩cmake软件包--> [ ~]# cd /usr/src/cmake-2.8.6/ [ cmake-2.8.6]# ./configure && gmake && gmake install <!--编译配置及安装gmake--> [ ~]# tar zxvf /usr/src/mysql-5.5.22.tar.gz -C /usr/src/ <!--解压缩MySQL软件包--> [ 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--> [ mysql-5.5.22]# make && make install <!--编译安装MySQL--> [ mysql-5.5.22]# cp support-files/my-medium.cnf /etc/my.cnf <!--生成MySQL配置文件--> cp:是否覆盖"/etc/my.cnf"? y <!--输入y确定覆盖--> [ mysql-5.5.22]# cp support-files/mysql.server /etc/init.d/mysqld <!--生成MySQL服务配置文件--> [ ~]# chmod +x /etc/init.d/mysqld <!--添加执行权限--> [ ~]# chkconfig --add mysqld <!--添加系统服务--> [ ~]# chkconfig --level 35 mysqld on <!--设置开机自动启动--> [ ~]# vim /etc/profile <!--优化MySQL程序命令--> PATH=$PATH:/usr/local/mysql/bin/ [ ~]# source /etc/profile <!--执行优化--> [ ~]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data <!--初始化MySQL数据库--> [ ~]# systemctl start mysqld <!--启动MySQL服务--> [ ~]# systemctl enable mysqld <!--设置开机自动启动--> [ ~]# mysqladmin -u root password <!--设置MySQL数据密码--> New password: <!--输入密码--> Confirm new password: <!--确定密码--> [ ~]# mysql -uroot <!--测试是否可以正常登录数据库--> mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec)
[ ~]# yum -y install gd libxml2-devel libjpeg-devel libpng-devel <!--安装php依赖程序--> [ ~]# tar zxvf /usr/src/php-5.3.28.tar.gz -C /usr/src/ <!--解压缩php软件包--> [ ~]# cd /usr/src/php-5.3.28/ [ 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--> [ php-5.3.28]# make && make install <!--编译安装php--> [ php-5.3.28]# ls -ld /usr/local/php/ <!--查看是否安装成功--> drwxr-xr-x 10 root root 101 11月 28 20:04 /usr/local/php/
[ php-5.3.28]# cp php.ini-production /usr/local/php/php.ini <!--生成服务配置文件--> [ ~]# ln -s /usr/local/php/sbin/* /usr/local/sbin/ <!--优化程序执行位置--> [ ~]# ln -s /usr/local/php/bin/* /usr/local/bin/ <!--优化程序执行位置--> [ ~]# tar zxvf /usr/src/zendguardloader-php-5.3-linux-glibc23-i386.tar.gz -C /usr/src/ <!--解压缩zend模块软件包--> [ ~]# mv /usr/src/ZendGuardLoader-php-5.3-linux-glibc23-i386 /php-5.3.x/ZendGuardLoader.so /usr/local/php/lib/php/ <!--php加载zend模块加速访问,修改zend模块位置--> [ ~]# vim /usr/local/php/php.ini <!--修改php主配置文件加载zend--> zend_extension=/usr/local/php/lib/php/ZendGuardLoader.so zend_loader.enable=1
[ ~]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf <!--生成php-fpm配置文件--> [ ~]# vim /usr/local/php/etc/php-fpm.conf <!--修改php-fpm主配置文件--> 26 pid = run/php-fpm.pid 141 user = nginx 142 group = nginx [ ~]# /usr/local/sbin/php-fpm <!--启动php-fpm服务--> [ ~]# netstat -anptu | grep 9000 tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 127332/php-fpm: mas
[ ~]# 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; } [ ~]# 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 [ ~]# vim /var/www/benetcom/index.php <!--编写php测试文件--> <?php phpinfo(); ?> [ ~]# /etc/init.d/nginx restart <!--重新启动Nginx服务-->
至此,客户端可以访问PHP测试页
[ ~]# vim /usr/local/nginx/conf/nginx.conf 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 index.php; } location ~ \.php$ { root /var/www/accpcom; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; }
[ ~]# unzip Discuz_X3.2_SC_UTF8.zip <!--解压缩论坛--> [ ~]# ls anaconda-ks.cfg Discuz_X3.2_SC_UTF8.zip readme utility Discuz_X3.2_SC_UTF8 initial-setup-ks.cfg upload [ ~]# mv ./upload/* /var/www/accpcom/<!--移动论坛到网站根目录--> [ ~]# chmod -R +x /var/www/accpcom/ <!--设置执行权限--> [ ~]# chown -R nginx:nginx /var/www/accpcom/ <!--设置网站的所有者--> [ ~]# mysql -uroot mysql> create database bbs; <!--创建论坛的数据库--> mysql> grant all on bbs.* to identified by ‘‘; <!--授权bbs账号管理bbs数据库--> [ ~]# /etc/init.d/nginx restart <!--重新启动Nginx服务-->
1)客户端浏览器输入www.accp.com/install访问,单击我同意开始安装
2)检查安装环境
3)选择全新安装,单击下一步
4)设置数据库的用户及密码
5)等待安装完成即可
6)删除accp.com网站的默认首页[ ~]# rm -rf /var/www/accpcom/index.html
7)客户端访问
8)登录后台管理网站
至此论坛就已经部署完成。
—————— 本文至此结束,感谢阅读 ——————