LAMP——详解及分离部署

LAMP架构

LINUX

Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户、多任务、支持多线程和多CPU的操作系统。它能运行主要的UNIX工具软件、应用程序和网络协议。它支持32位和64位硬件。Linux继承了Unix以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统

Apache

Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源码的网页服务器,可以在大多数计算机操作系统中运行,由于其多平台和安全性被广泛使用,是最流行的Web服务器端软件之一。它快速、可靠并且可通过简单的API扩展,将Perl/Python等解释器编译到服务器中。

Mysql

MySQL是一个关系型数据库管理系统.MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQL是最好的 RDBMS (Relational Database Management System,关系数据库管理系统) 应用软件之一。

PHP

 PHP是一种通用开源脚本语言。它可以比CGI或者Perl更快速地执行动态网页。用PHP做出的动态页面与其他的编程语言相比,PHP是将程序嵌入到HTML(标准通用标记语言下的一个应用)文档中去执行,执行效率比完全生成HTML标记的CGI要高许多;PHP还可以执行编译后代码,编译可以达到加密和优化代码运行,使代码运行更快。

Apache与PHP联动方式

  • 第一种

把php编译时直接编译成apache的模块、module模块化的方式进行工作(apahce默认的这种方式)

  • 第二种

CGI(通用网关接口):CGI是外部应用程序(CGI程序)与WEB服务器之间的接口标准,是在CGI程序和Web服务器之间传递信息的过程,apache基于CGI跟php通信

  • 第三种

FastCGI:CGI的改良版,FastCGI则是通过master-worker形式来处理每个请求,即启动一个master主进程,然后根据配置启动几个worker进程,当请求进来时,master会从worker进程中选择一个去处理请求。这样调度CPU会更加的迅速。

Apache与php通讯

  • 客户端通过HTTP协议请求web服务器资源
  • httpd服务器判断请求的资源为静态资源还是动态资源。
    • 静态资源:web直接把资源返回至客户端
    • 动态资源:通过指定的通讯方式与PHP服务器联系,交由php服务器来进行资源运算。从而将动态资源运算的结果交由httpd服务,最后返回给客户端。如果运算期间需要mysql数据库,则通过Php_mysql去连接mysql,从而进行运算。

lamp架构搭建

httpd服务配置

  1. httpd环境以及依赖包的安装
[root@localhost ~]# yum groups mark install 'Development Tools'
......

[root@localhost ~]# useradd -r -M -s /sbin/nologin -g apache apache 



[root@localhost ~]# yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++
......

Complete!


[root@localhost src]# tar -xf apr-1.7.0.tar.bz2 /usr/local/

[root@localhost src]# tar -xf apr-util-1.6.1.tar.bz2 /usr/local/

[root@localhost src]# tar -xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz /usr/local/
  1. 编译apr,aprutil,httpd
[root@localhost local]# 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"    # 此行注释掉
......

# 开始编译apr
[root@localhost apr-1.7.0]# ./configure --prefix=/usr/local/apr
......
[root@localhost apr-1.7.0]# make && make install
......

# 开始编译apr-util
[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-arp=/usr/local/apr
......
[root@localhost apr-util-1.6.1]# make && make install
......

# 编译httpd
[root@localhost apr-util-1.6.1]# cd ../httpd-2.4.46/

[root@localhost httpd-2.4.46]# ./configure --prefix=/usr/local/httpd \
> --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.46]# make && make install
......

3.安装后的配置

# 配置httpd服务环境变量
[root@localhost httpd-2.4.46]# echo 'export PATH=/usr/local/httpd/bin:$PATH' > /etc/profile.d/httpd.sh
[root@localhost httpd-2.4.46]# source /etc/profile.d/httpd.sh 

# 创建include文件软连接
[root@localhost httpd-2.4.46]# ln -s /usr/local/httpd/include/ /usr/include/httpd
[root@localhost httpd-2.4.46]# ll /usr/include/
......
lrwxrwxrwx   1 root root     25 Oct 30 16:00 httpd -> /usr/local/httpd/include/
......

# 修改服务器主机名
[root@localhost httpd-2.4.46]# vim /etc/httpd24/httpd.conf
......
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#
ServerName localhost:80    # 修改服务名
......

# 开启服务
[root@localhost httpd-2.4.46]# apachectl start
[root@localhost httpd-2.4.46]# ss -antl
State        Recv-Q        Send-Q                  Local Address:Port                 Peer Address:Port        
LISTEN       0             128                           0.0.0.0:22                        0.0.0.0:*           
LISTEN       0             128                                 *:80                              *:*           
LISTEN       0             128                              [::]:22                           [::]:*   

mysql服务配置

1.安装依赖包

[root@192 ~]# yum install -y ncurses-devel openssl-devel openssl cmake mariadb-devel
.....
Complete!

2.创建mysql账号

useradd -r -M -s /sbin/nologin mysql
[root@192 ~]# id mysql
uid=995(mysql) gid=992(mysql) groups=992(mysql)

3.解压mysql安装包创建软连接

tar -xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/

[root@192 local]# ln -s mysql-5.7.22-linux-glibc2.12-x86_64/ mysql;chown -R mysql.mysql mysql*

[root@192 local]# ll
......
lrwxrwxrwx  1 mysql mysql  36 Oct 31 15:33 mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/
drwxr-xr-x  9 mysql mysql 172 Oct 31 15:34 mysql-5.7.22-linux-glibc2.12-x86_64
......

# 创建环境变量
[root@192 local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh

[root@192 local]# source /etc/profile.d/mysql.sh 
[root@192 local]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

4.创建数据库数据存放目录

[root@192 local]# mkdir /var/mysql_data;chown mysql.mysql /var/mysql_data
[root@192 local]# ll /var/mysql_data/ -d
drwxr-xr-x 2 mysql mysql 6 Oct 31 15:41 /var/mysql_data/

5.初始化数据库

/usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/var/mysql_data/

[root@192 local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/var/mysql_data/
......
2020-10-31T07:45:23.026677Z 1 [Note] A temporary password is generated for root@localhost: 1Wy_!7khx+aL

[root@192 local]# echo '1Wy_!7khx+aL' > ~/.password
[root@192 local]# cat ~/.password 
1Wy_!7khx+aL

6.创建include软链接文件

[root@192 local]# ln -s /usr/local/mysql/include/ /usr/local/include/mysql

[root@192 include]# ll
total 0
lrwxrwxrwx 1 root root 25 Oct 31 15:48 mysql -> /usr/local/mysql/include/

[root@192 include]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@192 include]# ldconfig

7.配置mysql主配置文件

[root@192 include]# vim /etc/my.cnf

[mysqld]
basedir = /usr/local/mysql
datadir = /var/mysql_data
socket = /tmp/mysql.sock
port = 3306
pid-file = /var/mysql_data/mysql.pid
user = mysql
skip-name-resolve

8.配置服务启动脚本

[root@192 include]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld


# 补全服务脚本中的路径
[root@192 include]# vim /etc/init.d/mysqld
......
# If you change base dir, you must also change datadir. These may get
# overwritten by settings in the MySQL configuration files.

basedir=/usr/local/mysql
datadir=/var/mysql_data

9.启动服务

[root@192 include]# service mysqld start
Starting MySQL.Logging to '/var/mysql_data/192.168.197.139.err'.
 SUCCESS! 
[root@192 include]# ss -antl
State       Recv-Q       Send-Q                Local Address:Port               Peer Address:Port       
LISTEN      0            128                         0.0.0.0:22                      0.0.0.0:*          
LISTEN      0            128                            [::]:22                         [::]:*          
LISTEN      0            80                                *:3306                          *:*

10.设置数据库密码

[root@192 include]# mysql -uroot -p'1Wy_!7khx+aL'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

PHP服务配置

1.安装关联依赖包

root@192 ~]# 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 php-mysqlnd.x86_64
......

Complete!

2.编译安装Php

[root@192 src]# tar -xf php-7.2.8.tar.xz -C /usr/local/

[root@192 src]# cd /usr/local
[root@192 local]# ls
bin  etc  games  include  lib  lib64  libexec  php-7.2.8  sbin  share  src
[root@192 local]# cd php-7.2.8/

./configure --prefix=/usr/local/php --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@192 php-7.2.8]# make && make install

3.安装后的配置

[root@192 php-7.2.8]# echo 'export PATH=$PATH:/usr/local/php/bin' > /etc/profile.d/php.sh[root@192 php-7.2.8]# source /etc/profile.d/php.sh 
[root@192 php-7.2.8]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/php/bin

4.配置php-fpm

[root@192 php-7.2.8]# cp php.ini-production /etc/php.ini 
cp: overwrite '/etc/php.ini'? yes
[root@192 php-7.2.8]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@192 php-7.2.8]# chmod +x /etc/rc.d/init.d/php-fpm 
[root@192 php-7.2.8]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

vim /usr/local/php/etc/php-fpm.conf

......
pm.max_children = 50   # 最多同时提供50个进程提供50个并发服务
pm.start_servers = 5 # 启动时启动5个进程
pm.min_spare_servers = 2  # 最小空闲进程数
pm.max_spare_servers = 8  # 最大空闲进程数

5.启动php

[root@192 php-fpm.d]# service php-fpm start
Starting php-fpm  done
[root@192 php-fpm.d]# ss -antl
State     Recv-Q     Send-Q           Local Address:Port           Peer Address:Port     
LISTEN    0          128                    0.0.0.0:22                  0.0.0.0:*        
LISTEN    0          128                  127.0.0.1:9000                0.0.0.0:*        
LISTEN    0          128                       [::]:22                     [::]:*

6.配置apache

#将配置文件中的两行取消注释
[root@192 ~]# vim /etc/httpd24/httpd.conf 
......
LoadModule proxy_module modules/mod_proxy.so   
......
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so


# php服务器上将欲执行的Php文件编辑好保存
[root@192 /]# mkdir -p /www/html/
[root@192 html]# touch index.php
[root@192 html]# vim index.php
<?php
   phpinfo();
?>
[root@192 ~]# chown -R apache.apache /www/html/

# http服务器上编辑虚拟主机文件

[root@localhost local]# vim /etc/httpd24/extra/httpd-vhosts.conf 
<VirtualHost *:80>
    DocumentRoot "/usr/local/httpd/htdocs"
    ServerName test.example.com
    ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.197.140:9000/www/html/$1    #ip设置为php服务器的IP
    <Directory "/usr/local/httpd/htdocs">
        Options none
        AllowOverride none
        Require all granted
    </Directory>
</VirtualHost>


# 
[root@192 ~]# vim /etc/httpd24/httpd.conf
......
# probably should define those extensions to indicate media types:
    #
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType application/x-httpd-php .php
    AddType application/x-httpd-php-source .phps    搜索AddType,并增加最后两行
......

7.修改php服务的监听ip

[root@192 php-fpm.d]# vim www.conf
......
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = 0.0.0.0:9000    # 将默认的主机ip改成0.0.0.0
......

# 重启服务
[root@192 php-fpm.d]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done
[root@192 php-fpm.d]# ss -antl
State     Recv-Q     Send-Q           Local Address:Port           Peer Address:Port     
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                     [::]:*

8.查看效果

posted @ 2020-11-02 10:47  阿不思布丁  阅读(369)  评论(0编辑  收藏  举报