• 基于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) 准备安装包 

    [root@centos7-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用户与数据目录

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

     (3) 准备二进制程序

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

     (4) 准备配置文件

    [root@centos7-27 local]# cd /usr/local/mysql/
    [root@centos7-27 mysql]# mkdir /etc/mysql
    [root@centos7-27 mysql]# cp support-files/my-huge.cnf /etc/mysql/my.cnf
    [root@centos7-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) 初始化数据库,生成数据库文件

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

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

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

     (7)  配置PATH变量

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

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

    [root@centos7-27 mysql]# mysql_secure_installation 

    2、编译安装httpd (192.168.214.17)

     (1) 准备安装包

    [root@centos7-17 data]# cd /data
    [root@centos7-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 解压包中一起进行编译,当然也可以单独编译

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

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

    [root@centos7-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
    
    [root@centos7-17 data]# yum install -y gcc pcre-devel openssl-devel expat-devel

     (4) 开始编译安装

    [root@centos7-17 data]# cd httpd-2.4.39/
    [root@centos7-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
    [root@centos7-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

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

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

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

      附:httpd 通过 systemctl 命令的启动脚本制作,根据光盘 yum 源安装的 httpd-2.4.6 包中的 httpd.service 文件修改的

    [root@centos7-17 data]# vim /usr/lib/systemd/system/httpd.service
    [Unit]
    Description=The Apache HTTP Server
    After=network.target remote-fs.target nss-lookup.target
    Documentation=man:httpd(8)
    Documentation=man:apachectl(8)
    
    [Service]
    #Type=notify
    Type=forking  #修改此处
    #EnvironmentFile=/etc/sysconfig/httpd    #注释此项
    #ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
    ExecStart=/app/httpd24/bin/httpd $OPTIONS -k start    #修改此处
    ExecReload=/app/http24/bin/httpd $OPTIONS -k graceful
    #ExecStop=/bin/kill -WINCH ${MAINPID}
    ExecStop=/app/httpd24/bin/httpd $OPTIONS -k stop    #修改此处
    # We want systemd to give httpd some time to finish gracefully, but still want
    # it to kill httpd after TimeoutStopSec if something went wrong during the
    # graceful stop. Normally, Systemd sends SIGTERM signal right after the
    # ExecStop, which would kill httpd. We are sending useless SIGCONT here to give
    # httpd time to finish.
    KillSignal=SIGCONT
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target
    
    [root@centos7-17 data]# systemctl start httpd
    [root@centos7-17 data]# systemctl stop httpd
    [root@centos7-17 data]# systemctl restart httpd
    View Code

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

     (1) 准备安装包

    [root@centos7-17 ~]# cd /data
    [root@centos7-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源)

    [root@centos7-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
    
    [root@centos7-17 data]# yum install libxml2-devel bzip2-devel libmcrypt-devel
    #libmcrypt相关包依赖epel源

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

    [root@centos7-17 data]# tar -jxvf php-7.3.12.tar.bz2
    [root@centos7-17 data]# cd php-7.3.12/
    [root@centos7-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
    [root@centos7-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

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

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

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

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

     (1) 修改 http.conf 文件,

    [root@centos7-17 ~]# 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/ 创建多虚拟主机所需文件夹

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

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

    [root@centos7-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 服务

    [root@centos7-17 ~]# apachectl restart

     (4) 准备网站源码,并在源码目录给 apache 用户设置访问控制权限

    [root@centos7-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
    [root@centos7-17 data]# tar -xvf wordpress-5.2.2.tar.gz
    [root@centos7-17 data]# unzip Discuz_X3.3_SC_UTF8.zip
    [root@centos7-17 data]# cp -r wordpress/* /app/httpd24/htdocs/wordpress/ 
    [root@centos7-17 data]# cp -r upload/* /app/httpd24/htdocs/discuz/
    #设置apache用户的ACL权限,以便在网站安装时有权限读写文件
    [root@centos7-17 data]# setfacl -Rm u:apache:rwx /app/httpd24/htdocs/wordpress
    [root@centos7-17 data]# setfacl -Rm u:apache:rwx /app/httpd24/htdocs/discuz 

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

    [root@centos7-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:WindowsSystem32driversetc )

       192.168.214.17 blog.abcdef.com forum.abcdef.com

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

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

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

  • 相关阅读:
    Spring标签@Aspect-实现面向方向编程(@Aspect的多数据源自动加载)——SKY
    easyUI参数传递Long型时,前台解析出错的问题——SKY
    javax.servlet.ServletException: Could not resolve view with name‘ XXXX’in servlet with name 'spring'的解决方案-----SKY
    Netty实现java多线程Post请求解析(Map参数类型)—SKY
    java并发编程基础---Sky
    创建100个1k的随机文件到FSxL
    RAID磁盘阵列与LVM逻辑卷管理
    The beginners’ guide to farming Chia Coin on Windows.
    有赞移动Crash平台建设
    Comparisons Serverless
  • 原文地址:https://www.cnblogs.com/hovin/p/12037052.html
Copyright © 2020-2023  润新知