LAMP分离部署

Posted on 2020-06-17 10:54  留不住的时间  阅读(243)  评论(0编辑  收藏  举报

 一、实验名称

LAMP分离部署

二、 实验目的: 

 很多人在搭建的时候都是使用的一台机器来部署LAMP环境,但是我们在实际的工作中一般都是分离部署的。也就是说MySQL是MySQL;它是一台单机,分离部署自己跑自己的服务,提高效率!

三、 实验环境: 

OS:Centos7.5x86_64

Apache2.4 IP地址:192.168.100.11

PHP-5.5 IP地址:192.168.100.12

MySQL5.6IP地址:192.168.100.13

四:实验思路;

一:先在三台服务器上分别部署APACHE  PHP  MYSQL

二:调试Apache和PHP的协调

三:调试PHP和MySQL的协调

四:验证

 

具体操作步骤:

一:先把三台服务器IP调试同一个网段并关闭防火墙和内核安全机制

二:安装Apache

[root@localhost ~]# tar zxf httpd-2.4.29.tar.gz -C /usr/src/

[root@localhost ~]# cd /usr/src/httpd-2.4.29/

[root@localhost httpd-2.4.29]# yum -y insatll gcc* apr* pcre-devel

[root@localhost httpd-2.4.29]# ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-proxy --enable-proxy-http --enable-charset-lite

[root@localhost httpd-2.4.29]# make && make install

[root@localhost httpd-2.4.29]# cp /usr/local/httpd/bin/* /usr/local/bin/

[root@localhost httpd-2.4.29]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd

[root@localhost httpd-2.4.29]# vim /etc/init.d/httpd

#chkconfig: 35 80 80    #在第二行添加

[root@localhost httpd-2.4.29]# chmod a+x /etc/init.d/httpd

[root@localhost httpd-2.4.29]# chkconfig --add httpd

[root@localhost httpd-2.4.29]# chkconfig httpd on

[root@localhost httpd-2.4.29]# systemctl start httpd

[root@localhost httpd-2.4.29]# netstat -anpt | grep httpd

tcp6  0   0      :::80     :::*             LISTEN      30571/httpd 

[root@localhost httpd-2.4.29]# mkdir /var/www

[root@localhost httpd-2.4.29]# echo "apache.html" >> /var/www/index.html

[root@localhost ~]# vim /usr/local/httpd/conf/httpd.conf

LoadModule proxy_module modules/mod_proxy.so    #取消注释

LoadModule proxy_connect_module modules/mod_proxy_connect.so     #取消注释

LoadModule proxy_http_module modules/mod_proxy_http.so      #取消注释

LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so        #取消注释

LoadModule lbmethod_bybusyness_module modules/mod_lbmethod_bybusyness.so #取消注释

ServerName www.aaa.com:80   #取消注释并修改主机名

DocumentRoot "/var/www"   #修改网站根目录

<Directory "/var/www">      #修改网站根目录

Include conf/extra/httpd-vhosts.conf   #取消注释

修改虚拟目录配置

[root@localhost ~]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf

<Directory "/var/www ">

Order allow,deny

allow from all

</Directory>

<VirtualHost 192.168.100.11:80>

    ServerAdmin webmaster@dummy-host.example.com

    DocumentRoot "/var/www"

    ServerName www.aaa.com

    ServerAlias www.dummy-host.example.com

    ErrorLog "logs/www.aaa.com-error_log"

    CustomLog "logs/www.aaa.com-access_log" common

    ProxyRequests off

    ProxyPreserveHost on

    ProxyVia on

ProxyPassMatch /*.html$ !   ##html结尾的不转发

ProxyPassMatch /*.gif$ !

ProxyPassMatch /*.jpg$ !

ProxyPassMatch /*.png$ !

ProxyPassMatch /*.css$ !

ProxyPassMatch /*.js$ !

ProxyPassMatch /*.htm$ !

ProxyPass / fcgi://192.168.100.12:9000/var/www/

</VirtualHost>

测试Apache

 

三:配置PHP

[root@localhost ~]# tar zxf php-5.5.38.tar.gz -C /usr/src/

[root@localhost ~]# cd /usr/src/php-5.5.38/

[root@localhost php-5.5.38]# ./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --enable-fpm --enable-sockets --enable-sysvshm --enable-mbstring --with-libxml-dir=/usr --enable-xml --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d  --enable-maintainer-zts

[root@localhost php-5.5.38]# yum -y install libmcrypt* gcc* libxml2*

[root@localhost php-5.5.38]# make && make install

[root@php php-5.5.27]# useradd php

[root@localhost php-5.5.38]# cp php.ini-production /etc/php.ini

[root@localhost php-5.5.38]# vi /etc/php.ini

short_open_tag = On

default_charset = "UTF-8"

[root@php php-5.6.27]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm   

[root@localhost php-5.5.38]# chmod +x /etc/init.d/php-fpm

[root@localhost php-5.5.38]# chkconfig --add php-fpm

[root@localhost php-5.5.38]# chkconfig php-fpm on

修改php主配置,并编辑如下:

[root@localhost php-5.5.38]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

[root@localhost php-5.5.38]# vi /usr/local/php/etc/php-fpm.conf

修改内容如下:

pid = run/php-fpm.pid

user = nginx

group = nginx

listen = 192.168.100.12:9000    //PHP主机的IP地址

pm.max_children = 50

pm.start_servers = 5

pm.min_spare_servers = 5

pm.max_spare_servers = 35

修改完成后启动php服务

[root@localhost fpm]# systemctl start php-fpm

[root@localhost fpm]# netstat -anp | grep 9000

tcp   0  0 192.168.100.12:9000  0.0.0.0:*   LISTEN      127576/php-fpm: mas

[root@localhost fpm]# mkdir /var/www

[root@localhost fpm]# echo "index.php" >> /var/www/index.php

[root@localhost ~]# yum -y install mysql 

 

四:配置MySQL

1)编译安装mysql

[root@localhost ~]# yum -y install ncurses-devel

[root@localhost ~]# tar zxvf cmake-2.8.6.tar.gz

[root@localhost ~]# cd cmake-2.8.6/

[root@localhost cmake-2.8.6]# ./configure

[root@localhost cmake-2.8.6]# gmake && gmake install

[root@localhost ~]# tar zxvf mysql-5.6.36.tar.gz

[root@localhost ~]# cd mysql-5.6.36/

[root@localhost mysql-5.6.36]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DSYSCONFDIR=/etc

[root@localhost mysql-5.6.36]# make && make install

2)优化调整

[root@localhost mysql-5.6.36]# cp support-files/my-default.cnf /etc/my.cnf

cp:是否覆盖"/etc/my.cnf"? y

[root@localhost mysql-5.6.36]# cp support-files/mysql.server /etc/init.d/mysqld

[root@localhost mysql-5.6.36]# chmod +x /etc/init.d/mysqld

[root@localhost mysql-5.6.36]# chkconfig --add mysqld

[root@localhost mysql-5.6.36]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile

[root@localhost mysql-5.6.36]# source /etc/profile

3)初始化数据库

[root@localhost mysql-5.6.36]# groupadd mysql

[root@localhost mysql-5.6.36]# useradd -M -s /sbin/nologin mysql -g mysql

[root@localhost mysql-5.6.36]# chown -R mysql:mysql /usr/local/mysql

[root@localhost mysql-5.6.36]# yum -y install autoconf

[root@localhost mysql-5.6.36]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/

4)启动mysql服务

[root@localhost mysql-5.6.36]# systemctl start mysqld

[root@localhost mysql-5.6.36]# mysqladmin -u root password 'pwd123'

[root@localhost mysql-5.6.36]# mysql -u root -p

Enter password:

mysql> create database aaa;  ##创建aaa数据库

Query OK, 1 row affected (0.01 sec)

mysql> grant all on *.* to root@'192.168.100.12' identified by 'pwd123';  ##授权并指定密码

Query OK, 0 rows affected (0.00 sec)

 

五:PHP上创建调用MySQL的测试页面

[root@localhost www]# vim mysql.php

<?php

$link=mysqli_connect('192.168.100.13','root','pwd123');

if($link)

echo "恭喜你,数据库连接成功!";

mysqli_close($link);

?>

 访问测试

在客户端上访问:http://192.168.10.12