lamp分离部署
1.配置环境
//关闭所有环境的防火墙和selinux
systemctl stop firewalld
setenforce 0
//或者直接设置永久关闭
systemctl disable firewalld
vim /etc/selinux/config
SELINUX=disabled
系统 | 主机名 | IP | 服务 |
---|---|---|---|
Centos 8 | vm1 | 192.168.249.141 | httpd-2.4 |
Centos 8 | vm2 | 192.168.249.145 | mysql-5.7 |
Centos 8 | vm3 | 192.168.249.146 | php |
2.配置步骤
2.1在vm1上安装配置httpd
//安装epel
[root@localhost ~]# dnf -y install epel-release
//安装开发工具“Development Tools”
[root@localhost ~]# dnf -y groups mark install "Development Tools"
//创建apache服务的用户和组
[root@localhost ~]# useradd -r -M -s /sbin/nologin apache
[root@localhost ~]# id apache
uid=994(apache) gid=991(apache) 组=991(apache)
//安装依赖包
[root@localhost ~]# dnf -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make
//下载和安装apr以及apr-util、httpd,这里我们下载好了直接xftp上传,源码安装
[root@localhost ~]# ls
anaconda-ks.cfg apr-1.7.0.tar.bz2 apr-util-1.6.1.tar.bz2 httpd-2.4.43.tar.bz2
//安装一个bzip2,不然不能解压
[root@localhost ~]# dnf -y install bzip2
//分别解压
[root@localhost ~]# ls
anaconda-ks.cfg apr-1.7.0.tar.bz2 apr-util-1.6.1.tar.bz2 httpd-2.4.43.tar.bz2
[root@localhost ~]# tar xf apr-1.7.0.tar.bz2
[root@localhost ~]# tar xf apr-util-1.6.1.tar.bz2
[root@localhost ~]# tar xf httpd-2.4.43.tar.bz2
[root@localhost ~]# ls
anaconda-ks.cfg apr-1.7.0.tar.bz2 apr-util-1.6.1.tar.bz2 httpd-2.4.43.tar.bz2
apr-1.7.0 apr-util-1.6.1 httpd-2.4.43
[root@localhost ~]# cd apr-1.7.0
[root@localhost apr-1.7.0]# vim configure
cfgfile="${ofile}T"
trap "$RM \"$cfgfile\"; exit 1" 1 2 15
# $RM "$cfgfile" //将此行加上注释,或者删除此行
//配置编译安装
[root@localhost apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@localhost apr-1.7.0]# make && make install
[root@localhost apr-1.7.0]# cd ../apr-util-1.6.1
[root@localhost apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@localhost apr-util-1.6.1]# make && make install
[root@localhost httpd-2.4.43]# ./configure --prefix=/usr/local/apache \
> --sysconfdir=/etc/httpd24 \
> --enable-so \
> --enable-ssl \
> --enable-cgi \
> --enable-rewrite \
> --with-zlib \
> --with-pcre \
> --with-apr=/usr/local/apr \
> --with-apr-util=/usr/local/apr-util/ \
> --enable-modules=most \
> --enable-mpms-shared=all \
> --with-mpm=prefork
[root@localhost httpd-2.4.43]# make && make install
//安装后配置环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
[root@localhost ~]# source /etc/profile.d/httpd.sh
//创建软链接
[root@localhost ~]# ln -s /usr/local/apache/include/ /usr/include/httpd
//配置帮助文档
[root@localhost ~]# vim /etc/man_db.conf
MANDATORY_MANPATH /usr/local/share/man
//加上这个路径
MANDATORY_MANPATH /usr/local/apache/man
//取消ServerName前面的注释
[root@localhost ~]# sed -i '/#ServerName/s/#//g' /etc/httpd24/httpd.conf
//启动apache
[root@localhost ~]# apachectl start
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 128 *:80 *:*
2.2在vm2上安装配置mysql
//安装依赖包
[root@localhost ~]# dnf -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
//创建用户和组
[root@localhost ~]# useradd -r -M -s /sbin/nologin mysql
[root@localhost ~]# id mysql
uid=993(mysql) gid=990(mysql) 组=990(mysql)
//下载二进制格式的mysql安装包
[root@localhost ~]# ls
anaconda-ks.cfg apr-1.7.0.tar.bz2 apr-util-1.6.1.tar.bz2 httpd-2.4.43.tar.bz2
apr-1.7.0 apr-util-1.6.1 httpd-2.4.43 mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
//解压软件至/usr/local/
[root@localhost ~]# tar xf mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
//名字太长可以改个名字
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ls
apache apr-util etc include lib64 mysql-5.7.33-linux-glibc2.12-x86_64 share
apr bin games lib libexec sbin src
[root@localhost local]# mv mysql-5.7.33-linux-glibc2.12-x86_64 mysql
[root@localhost local]# ls
apache apr apr-util bin etc games include lib lib64 libexec mysql sbin share src
//修改属主属组
[root@localhost local]# chown -R mysql.mysql /usr/local/mysql
[root@localhost local]# ll
drwxr-xr-x. 9 mysql mysql 129 5月 12 23:38 mysql
//添加环境变量
[root@localhost local]# cd
[root@localhost ~]# ls /usr/local/mysql/
bin docs include lib LICENSE man README share support-files
[root@localhost ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost ~]# source /etc/profile.d/mysql.sh
[root@localhost ~]# ln -s /usr/local/mysql/include /usr/include/mysql
[root@localhost ~]# vim /etc/ld.so.conf.d/mysql.conf
/usr/local/lib
[root@localhost ~]# ldconfig
[root@localhost ~]# vim /etc/man_db.conf
MANDATORY_MANPATH /usr/man
MANDATORY_MANPATH /usr/share/man
MANDATORY_MANPATH /usr/local/mysql/man
//建立数据存放目录
[root@localhost ~]# mkdir /opt/data
[root@localhost ~]# chown -R mysql.mysql /opt/data
//初始化数据库
[root@localhost ~]# mysqld --initialize --user=mysql --datadir=/opt/data
2021-05-13T03:11:35.935105Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-05-13T03:11:37.507514Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-05-13T03:11:37.780304Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-05-13T03:11:37.842035Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: ee3eb51f-b398-11eb-9418-000c29e3d6a4.
2021-05-13T03:11:37.845672Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-05-13T03:11:40.248832Z 0 [Warning] CA certificate ca.pem is self signed.
2021-05-13T03:11:40.730747Z 1 [Note] A temporary password is generated for root@localhost: ie?goIl8yRLk //这个是密码
//生成配置文件,没有实质性的东西,直接覆盖,要是之前有,先备份
[root@localhost ~]# cat > /etc/my.cnf <<EOF
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
EOF
[root@localhost ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
//配置服务启动脚本
[root@localhost ~]# ls /usr/local/mysql/
bin docs include lib LICENSE man README share support-files
[root@localhost ~]# ls /usr/local/mysql/support-files/
magic mysqld_multi.server mysql-log-rotate mysql.server
[root@localhost ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# vim /etc/init.d/mysqld
basedir=/usr/local/mysql //添加它的安装目录
datadir=/opt/data //添加数据存放目录
//启动mysql
[root@localhost ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'.
SUCCESS!
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 *:80 *:*
//修改密码
//使用临时密码登录
root@localhost ~]# mysql -uroot -p'FqYzKscpN9+O'
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
[root@localhost ~]# dnf whatprovides libncurses.so.5
上次元数据过期检查:1:55:39 前,执行于 2021年05月12日 星期三 22时17分29秒。
ncurses-compat-libs-6.1-7.20180224.el8.i686 : Ncurses compatibility libraries
仓库 :baseos
匹配来源:
提供 : libncurses.so.5
//报错,安装这个包
[root@localhost ~]# dnf -y install ncurses-compat-libs
//再次登录就可以了
[root@localhost ~]# mysql -uroot -p'ie?goIl8yRLk'
mysql: [Warning] Using a password on the command line interface can be insecure.
//设置密码
mysql> set password = password('lixirong123!');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
Bye
//新密码登录
[root@localhost ~]# mysql -uroot -p'lixirong123!'
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql>
//设置开机自启
[root@localhost ~]# chkconfig --add mysqld
2.3在vm3上安装配置php
//yum安装php
[root@localhost ~]# dnf -y install php*
//安装依赖包
[root@localhost ~]# dnf -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel
//启动php-fpm
[root@localhost ~]# systemctl start php-fpm
//设置开机自启
[root@localhost ~]# systemctl enable php-fpm
[root@localhost ~]# ls /etc/php-fpm.d/
www.conf
[root@localhost ~]# vim /etc/php-fpm.d/www.conf
;listen = /run/php-fpm/www.sock //找到这个位置在下面添加如下端口号
listen = 0.0.0.0:9000
//重新启动,就可以看到端口号了
[root@localhost ~]# systemctl restart php-fpm
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:9000 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 *:80 *:*
3.配置apache
3.1在vm1上启用代理模块
在apache httpd 2.4以后已经专门有一个模块针对FastCGI的实现,此模块为mod_proxy_fcgi.so,它其实是作为mod_proxy.so模块的扩展,因此,这两个模块都要加载,编辑httpd.conf文件,取消以下两行内容的注释:
- LoadModule proxy_module modules/mod_proxy.so
- LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
//找到这两行取消注释
[root@localhost ~]# vim /etc/httpd24/httpd.conf
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
3.2配置虚拟主机
//在vm1主机创建虚拟主机的根目录并设置属主和属组为apache
[root@localhost ~]# mkdir /usr/local/php
[root@localhost ~]# mkdir /usr/local/php/linux
[root@localhost php]# chown -R apache.apache linux
//修改主配置文件
[root@localhost httpd24]# vim httpd.conf
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php //插入此行
AddType application/x-httpd-php-source .phps //插入此行
<IfModule dir_module>
DirectoryIndex index.php index.html //插入index.php
#Include /etc/httpd24/extra/httpd-default.conf
Include /etc/httpd24/extra/vhosts.conf //插入此行,取消注释
//配置虚拟机
[root@localhost ~]# cd /etc/httpd24/
[root@localhost httpd24]# ls
extra httpd.conf magic mime.types original
[root@localhost httpd24]# cd extra/
[root@localhost extra]# ls
httpd-autoindex.conf httpd-info.conf httpd-mpm.conf httpd-userdir.conf
httpd-dav.conf httpd-languages.conf httpd-multilang-errordoc.conf httpd-vhosts.conf
httpd-default.conf httpd-manual.conf httpd-ssl.conf proxy-html.conf
[root@localhost extra]# vim vhosts.conf
<VirtualHost *:80>
DocumentRoot "/usr/local/php/linux"
DirectoryIndex index.php
ServerName www.linux.com
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.249.146:9000/usr/local/php/linux/$1
<Directory "/usr/local/php/linux/">
Options none
AllowOverride none
Require all granted
</Directory>
</VirtualHost>
//重启apache服务
[root@localhost extra]# /usr/local/apache/bin/apachectl restart
[root@localhost extra]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
//在vm3主机上创建/usr/local/php/linux/inde.php的测试页面,并且设置属主属组为apache
[root@localhost ~]# mkdir /usr/local/php
[root@localhost ~]# mkdir /usr/local/php/linux
[root@localhost php]# vim /usr/local/php/linux/index.php
<?php
phpinfo()
?>
//重启php-fpm
[root@localhost php]# service php-fpm restart
Redirecting to /bin/systemctl restart php-fpm.service
[root@localhost php]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:9000 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
4.验证
1.在电脑主机修改C:\Windows\System32\drivers\etc\hosts文件,添加域名与IP的映射。(注意将文件hosts拖到桌面修改之后再拖回)
192.168.249.141 www.linux.com
2.在浏览器上进行访问,若看到以下界面则表示lamp架构搭建成功,否则请检查你的操作。
域名访问“www.linux.com”
IP访问