Linux学习-基于CentOS7的LAMP环境实现多虚拟主机

一、实验环境

 系统:CentOS7.6

 主机:两台(一台也可以),一台实现apache+php-fpm (192.168.214.17),一台实现mysql服务器 (192.168.214.27)

 软件包:

Apache:httpd-2.4.39.tar.bz2,apr-1.7.0.tar.bz2,apr-util-1.6.1.tar.bz2

PHP:php-7.3.7.tar.xz 

MySQL:mariadb-10.2.25-linux-x86_64.tar.gz (二进制包)

web网站:Discuz_X3.3_SC_UTF8.zip,wordpress-5.2.2.tar.gz

虚拟主机配置:forum.abcdef.com (论坛域名 对应Discuz),blog.abcdef.com (博客域名 对应wordpress)

二、实验步骤

1、实现MySQL服务器(192.168.214.27)

 (1) 准备安装包 

[27 ~]# ll /data
total 447224
-rw-r--r-- 1 root root 457955398 Dec 13 17:33 mariadb-10.2.25-linux-x86_64.tar.gz

 (2) 新建mysql用户与数据目录

[27 ~]# useradd -r -s /sbin/nologin -d /data/mysql mysql
[-27 ~]# mkdir /data/mysql
[-27 ~]# chown mysql:mysql /data/mysql/

 (3) 准备二进制程序

[27 data]# cd /data/
[-27 data]# tar -zxvf mariadb-10.2.25-linux-x86_64.tar.gz -C /usr/local/
[-27 data]# cd /usr/local/
[-27 local]# ln -s mariadb-10.2.25-linux-x86_64/ mysql
[-27 local]# chown -R root:mysql /usr/local/mysql

 (4) 准备配置文件

[27 local]# cd /usr/local/mysql/
[-27 mysql]# mkdir /etc/mysql
[-27 mysql]# cp support-files/my-huge.cnf /etc/mysql/my.cnf
[-27 mysql]# vim /etc/mysql/my.cnf 
#在[mysqld]下添加以下三项
datadir         = /data/mysql
innodb_file_per_table = on
skip_name_resolve = on#以下项非必需,建议添加,设置服务器端字符集character_set_server = utf8mb4

 (5) 初始化数据库,生成数据库文件

[27 mysql]# ./scripts/mysql_install_db --datadir=/data/mysql --user=mysql

 (6) 准备服务脚本,并启动服务

[27 mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[-27 mysql]# chkconfig --add mysqld
[-27 mysql]# service mysqld start
Starting mysqld (via systemctl):                           [  OK  ]

 (7)  配置PATH变量

[27 mysql]# echo ‘PATH=/usr/local/mysql/bin:$PATH‘ > /etc/profile.d/mysql.sh
[-27 mysql]# . /etc/profile.d/mysql.sh

 (8) 安全加固,以上安装后数据库是没有密码的,可以使用安全初始化脚本修改密码

[27 mysql]# mysql_secure_installation 

2、编译安装httpd (192.168.214.17)

 (1) 准备安装包

[17 data]# cd /data
[-17 data]# ls -lh *.bz2
-rw-r--r-- 1 root root 852K Dec 13 18:02 apr-1.7.0.tar.bz2
-rw-r--r-- 1 root root 419K Dec 13 18:02 apr-util-1.6.1.tar.bz2
-rw-r--r-- 1 root root 6.8M Dec 13 18:02 httpd-2.4.39.tar.bz2

  (2) 解压源码包,并将 apr 和 apr-util 复制到 httpd 解压包中一起进行编译,当然也可以单独编译

[17 data]# tar -jxvf apr-1.7.0.tar.bz2 
[-17 data]# tar -jxvf apr-util-1.6.1.tar.bz2
[-17 data]# tar -jxvf httpd-2.4.39.tar.bz2
[-17 data]# mv apr-1.7.0 httpd-2.4.39/srclib/apr
[-17 data]# mv apr-util-1.6.1 httpd-2.4.39/srclib/apr-util

 (3) 安装相关所需包(光盘yum源)

[17 data]# cat /etc/yum.repos.d/base.repo 
[development]
name=dvdbase repo
baseurl=file:///mnt/cdrom/
enabled=1
gpgcheck=1
gpgkey=file:///mnt/cdrom/RPM-GPG-KEY-CentOS-7

[-17 data]# yum install -y gcc pcre-devel openssl-devel expat-devel

 (4) 开始编译安装

[17 data]# cd httpd-2.4.39/
[-17 httpd-2.4.39]# ./configure > --prefix=/app/httpd24 > --enable-so > --enable-ssl > --enable-cgi > --enable-rewrite > --with-zlib > --with-pcre > --with-included-apr > --enable-modules=most > --enable-mpms-shared=all > --with-mpm=prefork
[-17 httpd-2.4.39]# make && make install
./configure --prefix=/app/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork

编译代码

 (5) 创建apache用户并修改配置文件中的启动时所用的用户和组,配置文件中默认的用户和组都为 daemon

[17 httpd-2.4.39]# useradd -r -s /sbin/nologin apache
[-17 httpd-2.4.39]# vim /app/httpd24/conf/httpd.conf#找到以下两项,修改用户和组为apache
user apache
group apache

 (6) 配置PATH变量并启动测试

[17 httpd-2.4.39]# echo ‘PATH=/app/httpd24/bin:$PATH‘ > /etc/profile.d/http24.sh
[-17 httpd-2.4.39]# . /etc/profile.d/http24.sh 
[-17 httpd-2.4.39]# apachectl  start
[-17 httpd-2.4.39]# curl http://192.168.214.17  
#测试访问,可以看到可以了
<html><body><h1>It works!</h1></body></html>

3、编译安装php-fpm (192.168.214.17)

 (1) 准备安装包

[17 ~]# cd /data
[-17 data]# ls -l php-7.3.12.tar.bz2 
-rw-r--r-- 1 root root 15006275 Dec 13 19:54 php-7.3.12.tar.bz2

 (2) 安装相关所需包(光盘yum源与EPEL源)

[17 data]# cat /etc/yum.repos.d/base.repo 
[development] #光盘源也可用阿里云的yum源
name=dvdbase repo
baseurl=file:///mnt/cdrom/
enabled=1
gpgcheck=1
gpgkey=file:///mnt/cdrom/RPM-GPG-KEY-CentOS-7

[aliyunEpel] #epel源
name=aliyun epel
baseurl=https://mirrors.aliyun.com/epel/$releasever/$basearch
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-$releasever

[-17 data]# yum install libxml2-devel bzip2-devel libmcrypt-devel
#libmcrypt相关包依赖epel源

 (3) 解压源码包,并进行编译安装

[17 data]# tar -jxvf php-7.3.12.tar.bz2
[-17 data]# cd php-7.3.12/
[-17 php-7.3.12]# ./configure --prefix=/app/php > --enable-mysqlnd > --with-mysqli=mysqlnd > --with-pdo-mysql=mysqlnd > --with-openssl > --with-freetype-dir > --with-jpeg-dir > --with-png-dir > --with-zlib > --with-libxml-dir=/usr > --with-config-file-path=/etc > --with-config-file-scan-dir=/etc/php.d > --enable-mbstring > --enable-xml > --enable-sockets > --enable-fpm > --enable-maintainer-zts > --disable-fileinfo
[-17 php-7.3.12]# make && make install
./configure --prefix=/app/php --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --enable-mbstring --enable-xml --enable-sockets --enable-fpm --enable-maintainer-zts --disable-fileinfo

编译代码

 (4) 准备配置文件,并修改 www.conf 配置文件中启动用户与组为 apache,默认用户与组为 nobody

[17 php-7.3.12]# cp php.ini-production /etc/php.ini
[-17 php-7.3.12]# cd /app/php/etc/
[-17 etc]# cp php-fpm.conf.default php-fpm.conf
[-17 etc]# cd php-fpm.d/
[-17 php-fpm.d]# cp www.conf.default www.conf
[-17 php-fpm.d]# vim www.conf
#找到以下两项,将nobody改为apache
user = apache
group = apache

 (5) 准备服务启动脚本,并启动 php-fpm 服务

[17 php-fpm.d]# cd /data/php-7.3.12/
[-17 php-7.3.12]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[-17 php-7.3.12]# chmod +x /etc/init.d/php-fpm 
[-17 php-7.3.12]# chkconfig --add php-fpm 
[-17 php-7.3.12]# service php-fpm start
Starting php-fpm  done
[-17 php-7.3.12]# ss -nlt|grep 9000  #可以看到9000端口已经启动
LISTEN     0      128    127.0.0.1:9000                     *:*

4、配置 httpd 配置文件,让其支持 php

 (1) 修改 http.conf 文件,

[ ~]# vim /app/httpd24/conf/httpd.conf#取消下面两个模块的注释
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

#修改下面行,添加 index.php
<IfModule dir_module>
    DirectoryIndex index.php index.html
</IfModule>

#添加以下行
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
ProxyRequests Off
#以下这行如果是单主机是必需加的,但我们要什么多虚拟主机,到时到虚拟主机配置中加即可
#ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/app/httpd24/htdocs/$1 

5、配置网站 

 (1) 在/app/httpd24/htdocs/ 创建多虚拟主机所需文件夹,并给 apache 用户设置访问控制权限

[17 ~]# mkdir -v /app/httpd24/htdocs/{wordpress,discuz}
mkdir: created directory ‘/app/httpd24/htdocs/wordpress’
mkdir: created directory ‘/app/httpd24/htdocs/discuz’
[-17 ~]# setfacl
[-17 ~]# setfacl

 (2) 继续在 http.conf 文件中添加多虚拟主机设置 

[17 ~]# vim /app/httpd24/conf/httpd.conf

<VirtualHost *:80>
ServerName blog.abcdef.com    
DocumentRoot /app/httpd24/htdocs/wordpress
CustomLog logs/blog.access_log combined  #访问日志
ErrorLog logs/blog.error_log    #错误日志
LogLevel warn   #日志级别 
<Directory /app/httpd24/htdocs/wordpress>
Options none
Require all granted
</Directory>
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/app/httpd24/htdocs/wordpress/$1
</VirtualHost>

<VirtualHost *:80>
ServerName forum.abcdef.com
DocumentRoot /app/httpd24/htdocs/discuz
CustomLog logs/forum.access_log combined
ErrorLog logs/forum.error_log
LogLevel warn
<Directory /app/httpd24/htdocs/discuz>
Options none
Require all granted
</Directory>
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/app/httpd24/htdocs/discuz/$1
</VirtualHost>

  (3) 重新启动 httpd 服务

[17 ~]# apachectl restart

 (4) 准备网站源码

[17 data]# ll Discuz_X3.3_SC_UTF8.zip wordpress-5.2.2.tar.gz 
-rw-r--r-- 1 root root 10922155 Dec 13 18:02 Discuz_X3.3_SC_UTF8.zip
-rw-r--r-- 1 root root 11133665 Dec 13 18:02 wordpress-5.2.2.tar.gz
[-17 data]# tar -xvf wordpress-5.2.2.tar.gz
[-17 data]# unzip Discuz_X3.3_SC_UTF8.zip
[-17 data]# cp -r wordpress/* /app/httpd24/htdocs/wordpress/ 
[ data]# cp -r upload/* /app/httpd24/htdocs/discuz/
#设置apache用户的ACL权限,以便在网站安装时有权限读写文件
[ data]# setfacl -Rm u:apache:rwx /app/httpd24/htdocs/wordpress
[ data]# setfacl -Rm u:apache:rwx /app/httpd24/htdocs/discuz

 (5) 在数据库服务器上 (192.168.214.27),创建数据库及连接数据库的用户

[ mysql]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.2.25-MariaDB-log MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
MariaDB [(none)]> create database wordpress;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> create database discuz;
Query OK, 1 row affected (0.01 sec)
MariaDB [(none)]> grant all on wordpress.* to ‘wpuser‘@‘192.168.214.%‘ identified by ‘centos‘;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> grant all on discuz.* to ‘dsuser‘@‘192.168.214.%‘ identified by ‘centos7‘ ;
Query OK, 0 rows affected (0.01 sec)

  (6) 设置 hosts 文件,使域名可以解析到主机,在 hosts 文件中添加一行(如果利用Linux图形桌面上的浏览器进行网站安装,hosts文件为 /etc/hosts,如果是用windows安装,则host文件 路径为 C:\Windows\System32\drivers\etc )

 192.168.214.17 blog.abcdef.com forum.abcdef.com

 (7) 安装网站即可,利用浏览器安装,输出相应域名,根据安装向导安装即可

blog.abcdef.com域名访问如图,一步步安装即可

Linux学习-基于CentOS7的LAMP环境实现多虚拟主机Linux学习-基于CentOS7的LAMP环境实现多虚拟主机

forum.abcdef.com域名访问如下图,一步步安装即可

Linux学习-基于CentOS7的LAMP环境实现多虚拟主机

相关推荐