lamp分离部署
1.环境部署说明
主机ip | 主机名 | 运行服务 |
---|---|---|
192.168.30.100 | vm1 | httpd |
192.168.30.130 | vm2 | mysql |
192.168.30.150 | vm3 | php |
1.1在vm1上安装httpd
应装的是centos 已有网络源 不需在下载
1.安装epel-release,开发工具包,以及wget bzip2
[root@vm1 ~]# yum -y install epel-release
[root@vm1 ~]# yum groups mark install 'Development Tools'
[root@vm1 ~]# yum -y install wget
[root@vm1 ~]# yum -y install bzip2
2.创建apache服务的用户和组
[root@vm1 ~]# useradd -r -M -s /sbin/nologin apache
[root@vm1 ~]# id apache
uid=997(apache) gid=995(apache) groups=995(apache)
3.安装依赖包
[root@vm1 ~]# yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++
4.下载并解压apr apr-util 以及httpd
[root@vm1 ~]# wget https://mirror.bit.edu.cn/apache/apr/apr-1.7.0.tar.bz2
[root@vm1 ~]# wget https://mirror.bit.edu.cn/apache/apr/apr-util-1.6.1.tar.bz2
[root@vm1 ~]# wget https://mirror.bit.edu.cn/apache/httpd/httpd-2.4.43.tar.bz2
[root@vm1 ~]# ls
anaconda-ks.cfg apr-util-1.6.1.tar.bz2
apr-1.7.0.tar.bz2 httpd-2.4.43.tar.bz2
[root@vm1 ~]# tar -xf apr-1.7.0.tar.bz2
[root@vm1 ~]# tar -xf apr-util-1.6.1.tar.bz2
[root@vm1 ~]# tar -xf httpd-2.4.43.tar.bz2
[root@vm1 ~]# ls
anaconda-ks.cfg apr-util-1.6.1.tar.bz2
apr-1.7.0 httpd-2.4.43
apr-1.7.0.tar.bz2 httpd-2.4.43.tar.bz2
apr-util-1.6.1
5.配置并安装apr apr-util httpd
[root@vm1 ~]# cd apr-1.7.0
[root@vm1 apr-1.7.0]# vim configure
cfgfile=${ofile}T
trap "$RM \"$cfgfile\"; exit 1" 1 2 15
$RM "$cfgfile" 删掉此行
[root@vm1 apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@vm1 apr-1.7.0]# make && make install
[root@vm1 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@vm1 apr-util-1.6.1]# make && make install
[root@vm1 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@vm1 httpd-2.4.43]# make && make install
[root@vm1 httpd-2.4.43]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
[root@vm1 httpd-2.4.43]# source /etc/profile.d/httpd.sh
[root@vm1 httpd-2.4.43]# echo $PATH
/usr/local/apache/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@vm1 httpd-2.4.43]# ln -s /usr/local/apache/include/ /usr/include/apache
6.启动apache
[root@vm1 ~]# apachectl start
[root@vm1 ~]# ss -tanl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
验证
1.2在vm2上安装mysql
1.安装依赖包
[root@vm2 ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
2.创建用户
[root@vm2 ~]# useradd -r -M -s /sbin/nologin -u 306 mysql[root@vm2 ~]# id mysql
uid=306(mysql) gid=306(mysql) groups=306(mysql)
3.解压软件包mysql
[root@vm2 ~]# ls
anaconda-ks.cfg
mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
[root@vm2 ~]# tar xf mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@vm2 ~]# cd /usr/local/
[root@vm2 local]# ls
bin lib sbin
etc lib64 share
games libexec src
include mysql-5.7.30-linux-glibc2.12-x86_64
[root@vm2 local]# ln -s mysql-5.7.30-linux-glibc2.12-x86_64/ mysql
[root@vm2 local]# ls
bin lib mysql-5.7.30-linux-glibc2.12-x86_64
etc lib64 sbin
games libexec share
include mysql src
[root@vm2 local]# ll
total 0
drwxr-xr-x. 2 root root 6 Apr 11 2018 bin
drwxr-xr-x. 2 root root 6 Apr 11 2018 etc
drwxr-xr-x. 2 root root 6 Apr 11 2018 games
drwxr-xr-x. 2 root root 6 Apr 11 2018 include
drwxr-xr-x. 2 root root 6 Apr 11 2018 lib
drwxr-xr-x. 2 root root 6 Apr 11 2018 lib64
drwxr-xr-x. 2 root root 6 Apr 11 2018 libexec
lrwxrwxrwx. 1 root root 36 Jul 11 22:29 mysql -> mysql-5.7.30-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root root 129 Jul 11 22:26 mysql-5.7.30-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 Apr 11 2018 sbin
drwxr-xr-x. 5 root root 49 Jul 3 15:09 share
drwxr-xr-x. 2 root root 6 Apr 11 2018 src
4.修改属组
[root@vm2 local]# chown -R mysql.mysql mysql*
[root@vm2 local]# ll -d mysql*
lrwxrwxrwx. 1 mysql mysql 36 Jul 11 22:29 mysql -> mysql-5.7.30-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 mysql mysql 129 Jul 11 22:26 mysql-5.7.30-linux-glibc2.12-x86_64
5.添加环境变量
[root@vm2 local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@vm2 local]# source /etc/profile.d/mysql.sh
[root@vm2 local]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
6.配置mysql
[root@vm2 local]# ls /usr/local/mysql
bin include LICENSE README support-files
docs lib man share
[root@vm2 local]# ln -s /usr/local/mysql/include/ /usr/local/include/mysql
[root@vm2 local]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@vm2 local]# ldconfig
7.建立数据存放目录以及初始化数据库
[root@vm2 local]# mkdir /opt/data
[root@vm2 local]# chown -R mysql.mysql /opt/data/
[root@vm2 local]# ll /opt
total 0
drwxr-xr-x. 2 mysql mysql 6 Jul 11 22:40 data
[root@vm2 local]# mysqld --initialize-insecure --user=mysql --datadir=/opt/data/
8.生成配置文件
[root@vm2 local]# 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@vm2 local]# 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@vm2 local]#
9.配置服务启动脚本
[root@vm2 mysql]# cd support-files/
[root@vm2 support-files]# ls
magic mysql-log-rotate
mysqld_multi.server mysql.server
[root@vm2 support-files]# cp mysql.server /etc/init.d/mysqld
[root@vm2 support-files]# vim /etc/init.d/mysqld
# overwritten by settings in the MySQL configuration files.
basedir=/usr/local/mysql
datadir=/opt/data
10.启动mysql
[root@vm2 support-files]# service mysqld start
Starting MySQL.Logging to '/opt/data/vm2.err'.
SUCCESS!
[root@vm2 support-files]# ss -tanl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 80 :::3306 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
11.修改密码
mysql> set password = password('123456!');
Query OK, 0 rows affected, 1 warning (0.00 sec)
1.3在vm3上安装php
1.安装需要的包
[root@vm3 ~]# wget http://rpms.remirepo.net/enterprise/remi-release-7.rpm
[root@vm3 ~]# rpm -Uvh remi-release-7.rpm
[root@vm3 ~]# yum makecache --enablerepo=remi-php74
[root@vm3 ~]# yum -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 php72-php-mysqlnd
2解压php并编译安装
[root@vm3 ~]# ls
anaconda-ks.cfg remi-release-7.rpm
php-7.4.7.tar.xz
[root@vm3 ~]# tar xf php-7.4.7.tar.xz
[root@vm3 ~]# ls
anaconda-ks.cfg php-7.4.7.tar.xz
php-7.4.7 remi-release-7.rpm
[root@vm3 php-7.4.7]# yum -y install libsqlite3x.x86_64 libsqlite3x-devel.x86_64
[root@vm3 php-7.4.7]# yum -y install oniguruma-devel
[root@vm3 php-7.4.7]# ./configure --prefix=/usr/local/php7 --with-config-file-path=/etc --enable-fpm --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-soap --with-openssl --enable-bcmath --with-iconv --with-bz2 --enable-calendar --with-curl --enable-exif --enable-ftp --with-gd --with-jpeg-dir --with-png-dir --with-zlib-dir --with-freetype-dir --with-gettext --enable-json --enable-mbstring --enable-pdo --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-readline --enable-shmop --enable-simplexml --enable-sockets --enable-zip --enable-mysqlnd-compression-support --with-pear --enable-pcntl --enable-posix
[root@vm3 php-7.4.7]# make && make install
3.安装后配置
[root@vm3 php-7.4.7]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[root@vm3 php-7.4.7]# source /etc/profile.d/php7.sh
[root@vm3 php-7.4.7]# which php
/usr/local/php7/bin/php
[root@vm3 php-7.4.7]# php -v
PHP 7.4.7 (cli) (built: Jul 11 2020 23:48:31) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
4.配置php-fpm
[root@vm3 php-7.4.7]# cp php.ini-production /etc/php.ini
[root@vm3 php-7.4.7]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@vm3 php-7.4.7]# chmod +x /etc/rc.d/init.d/php-fpm
[root@vm3 php-7.4.7]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
[root@vm3 php-7.4.7]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
5.启动php-fpm
[root@vm3 php-7.4.7]# service php-fpm start
Starting php-fpm done
[root@vm3 php-7.4.7]# ss -tanl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:9000 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
[root@vm3 php-7.4.7]# cd
[root@vm3 ~]# cd /usr/local/php7/etc/php-fpm.d
[root@vm3 php-fpm.d]# ls
www.conf www.conf.default
[root@vm3 php-fpm.d]# vim www.conf
et.
; Note: This value is mandatory.
listen = 0.0.0.0:9000
; Set listen(2) backlog.
[root@vm3 php-fpm.d]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
[root@vm3 php-fpm.d]# ss -tanl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:9000 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
[root@vm3 php-fpm.d]#
2.配置apache
2.1在vm1上配置apache
1.启用代理模块
[root@vm1 ~]# vim /etc/httpd24/httpd.conf
LoadModule proxy_module modules/mod_proxy.so 取消注释
#LoadModule proxy_connect_module modules/mod_proxy_connect.so
#LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
#LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so 取消注释
#LoadModule proxy_scgi_module modules/mod_proxy_scgi.
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 动态页面优先
</IfModule>
<VirtualHost *:80> DocumentRoot "/usr/local/apache/htdocs"
ServerName www.yy.com
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.30.150:9000/xunxian/$1 <Directory "/usr/local/apache/htdocs">
Options none
AllowOverride none
Require all granted
</Directory>
</VirtualHost>
2.在vm3上生成动态页面存在目录并创建测试页面
[root@vm3 ~]# mkdir /xunxian
[root@vm3 xunxian]# cat index.php
<?php
phpinfo();
?>
3.验证