lamp(源码部署)
lamp
1.lamp简介
所谓lamp,其实就是由Linux+Apache+Mysql/MariaDB+Php/Perl/Python的一组动态网站或者服务器的开源软件,除Linux外其它各部件本身都是各自独立的程序,但是因为经常被放在一起使用,拥有了越来越高的兼容度,共同组成了一个强大的Web应用程序平台。
LAMP指的是Linux(操作系统)、Apache(HTTP服务器)、MySQL(也指MariaDB,数据库软件)和PHP(有时也是指Perl或Python)的第一个字母,一般用来建立web应用平台。
2.web服务器工作流程
在说lamp架构平台的搭建前,我们先来了解下什么是CGI,什么是FastCGI,什么是…
web服务器的资源分为两种,静态资源和动态资源
静态资源就是指静态内容,客户端从服务器获得的资源的表现形式与原文件相同。可以简单的理解为就是直接存储于文件系统中的资源
动态资源则通常是程序文件,需要在服务器执行之后,将执行的结果返回给客户端
那么web服务器如何执行程序并将结果返回给客户端呢?下面通过一张图来说明一下web服务器如何处理客户端的请求
2.1 httpd与php结合的方式
httpd与php结合的方式有以下三种:
modules:php将以httpd的扩展模块形式存在,需要加载动态资源时,httpd可以直接通过php模块来加工资源并返回给客户端
httpd prefork:libphp5.so(多进程模型的php)
httpd event or worker:libphp5-zts.so(线程模型的php)
CGI:httpd需要加载动态资源时,通过CGI与php解释器联系,获得php执行的结果,此时httpd负责与php连接的建立和断开等
3. lamp平台构建
环境说明:
系统平台 | IP | 需要安装的服务 |
---|---|---|
centos7 redhat7 | 172.16.12.128 | httpd-2.4 mysql-5.7 php php-mysql |
lamp平台软件安装次序:
httpd --> mysql --> php
注意:php要求httpd使用prefork MPM
3.1 安装httpd
//安装开发软件包
[root@RHEL8 ~]# yum groups mark install 'Development Tools'
//创建apache服务的用户和组
[root@RHEL8 ~]# groupadd -r apache
[root@RHEL8 ~]# useradd -r -M -s /sbin/nologin -g apache apache
//安装依赖包
[root@RHEL8 ~]# yum -y install openssl-devel pcre-devel expat-devel libtool gc
下载和安装apr以及apr-util
//进入Apache官网([Apache Distribution Directory](https://downloads.apache.org/))下载安装包(需翻墙) 下载三个软件包
[root@RHEL8 src]# wget https://downloads.apache.org/apr/apr-1.7.4.tar.gz
[root@RHEL8 src]# wget https://downloads.apache.org/apr/apr-util-1.6.3.tar.gz
[root@RHEL8 src]# wget https://downloads.apache.org/httpd/httpd-2.4.57.tar.gz
[root@RHEL8 src]# ls
apr-1.7.4.tar.gz httpd-2.4.57.tar.gz
apr-util-1.6.3.tar.gz kernels
debug mysql57-community-release-el7-10.noarch.rpm
解压软件包
[root@RHEL8 src]# tar xf apr-1.7.4.tar.gz
[root@RHEL8 src]# tar xf apr-util-1.6.3.tar.gz
[root@RHEL8 src]# tar xf httpd-2.4.57.tar.gz
1
2
3
[root@RHEL8 src]# cd apr-1.7.4
[root@RHEL8 apr-1.7.4]# vim configure //进入之后搜索cfgfile快速定位
cfgfile="${ofile}T"
trap "$RM \"$cfgfile\"; exit 1" 1 2 15
# $RM "$cfgfile" //将此行加上注释,或者删除此行
[root@RHEL8 apr-1.7.4]# ./configure --prefix=/usr/local/apr
[root@RHEL8 apr-1.7.4]# yum -y install make
[root@RHEL8 apr-1.7.4]# make && make install
[root@RHEL8 apr-1.7.4]# cd /usr/src/apr-util-1.6.3
[root@RHEL8 apr-util-1.6.3]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@RHEL8 apr-util-1.6.3]# make && make install
//编译安装httpd
[root@RHEL8 httpd-2.4.57]# ./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@RHEL8 httpd-2.4.57]# make && make install
安装后进行配置
[root@RHEL8 ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
[root@RHEL8 ~]# source /etc/profile.d/httpd.sh
[root@RHEL8 ~]# ln -s /usr/local/apache/include/ /usr/include/httpd[root@RHEL8 ~]# echo 'MANPATH /usr/local/apache/man' >> /etc/man.config
//取消ServerName前面的注释
[root@RHEL8 ~]# sed -i '/#ServerName/s/#//g' /etc/httpd24/httpd.conf
[root@RHEL8 ~]# apachectl start //启动apache
[root@RHEL8 ~]# 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 80 *:3306 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
3.2 安装PHP
安装依赖包
[root@RHEL8 ~]# cd /usr/src/
[root@RHEL8 src]# wget https://www.php.net/distributions/php-8.2.9.tar.xz
[root@RHEL8 src]# tar xf php-8.2.9.tar.xz //解压
[root@RHEL8 src]# cd php-8.2.9
//后面有提醒,建议oniguruma-devel这个依赖包一开始就安装!!!!!!!!!!!!!!
[root@RHEL8 php-8.2.9]#yum -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
[root@RHEL8 php-8.2.9]# ./configure --prefix=/usr/local/php7 --with-config-file-path=/etc --enable-fpm --disable-debug --disable-rpath --enable-shared --enable-soap --with-openssl --enable-bcmath --with-iconv --with-bz2 --enable-calendar --with-curl --enable-exif --enable-ftp --enable-gd --with-jpeg --with-zlib-dir --with-freetype --with-gettext --enable-mbstring --enable-pdo --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-readline --enable-shmop --enable-simplexml --enable-sockets --with-zip --enable-mysqlnd-compression-support --with-pear --enable-pcntl --enable-posi
报错信息,缺少libxml2-devel安装包
configure: error: Package requirements (libxml-2.0 >= 2.9.0) were not met:
Package 'libxml-2.0', required by 'virtual:world', not found
Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.
Alternatively, you may set the environment variables LIBXML_CFLAGS
and LIBXML_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
继续重复编译
报错信息,缺少sqlite-devel安装包
configure: error: Package requirements (sqlite3 >= 3.7.7) were not met:
Package 'sqlite3', required by 'virtual:world', not found
Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.
Alternatively, you may set the environment variables SQLITE_CFLAGS
and SQLITE_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
[root@RHEL8 php-8.2.9]# yum -y install sqlite-devel
继续编译
报错信息,缺少bzip2 bzip2-devel安装包
configure: error: Please reinstall the BZip2 distribution
[root@RHEL8 php-8.2.9]# yum install bzip2 bzip2-devel
继续编译
报错信息,缺少libcurl-devel安装包
configure: error: Package requirements (libcurl >= 7.29.0) were not met:
Package 'libcurl', required by 'virtual:world', not found
Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.
Alternatively, you may set the environment variables CURL_CFLAGS
and CURL_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
[root@RHEL8 php-8.2.9]# yum -y install libcurl-devel
继续编译
报错信息,缺少libpng-devel安装包
configure: error: Package requirements (libpng) were not met:
Package 'libpng', required by 'virtual:world', not found
Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.
Alternatively, you may set the environment variables PNG_CFLAGS
and PNG_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
[root@RHEL8 php-8.2.9]# yum -y install libpng-devel
继续编译
报错信息,缺少libjpeg-devel安装包
configure: error: Package requirements (libjpeg) were not met:
Package 'libjpeg', required by 'virtual:world', not found
[root@RHEL8 php-8.2.9]# yum -y install libjpeg-devel
1
2
3
4
5
6
报错信息,缺少freetype-devel安装包
configure: error: Package requirements (freetype2) were not met:
Package 'freetype2', required by 'virtual:world', not found
[root@RHEL8 php-8.2.9]# yum -y install freetype-devel
最开始就安装这个包(不然可能装不上)
!!!!!!!!!!最开始安装
configure: error: Package requirements (oniguruma) were not met:
Package 'oniguruma', required by 'virtual:world', not found
yum -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
报错信息,缺少readline-devel安装包
configure: error: Please reinstall readline - I cannot find readline.h
[root@RHEL8 php-8.2.9]# yum -y install readline-devel
报错信息,缺少libzip-devel安装包
configure: error: Package requirements (libzip >= 0.11 libzip != 1.3.1 libzip != 1.7.0) were not met:
Package 'libzip', required by 'virtual:world', not found
Package 'libzip', required by 'virtual:world', not found
Package 'libzip', required by 'virtual:world', not found
[root@RHEL8 php-8.2.9]# yum -y install libzip-devel
[root@RHEL8 php-8.2.9]# make -j $(cat /proc/cpuinfo |grep processor|wc -l)
[root@RHEL8 php-8.2.9]# make install
安装后配置
[root@RHEL8 php-8.2.9]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[root@RHEL8 php-8.2.9]# source /etc/profile.d/php7.sh
[root@RHEL8 php-8.2.9]# which php
/usr/local/php7/bin/php
[root@RHEL8 php-8.2.9]# php -v
PHP 8.2.9 (cli) (built: Aug 31 2023 03:19:18) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.9, Copyright (c) Zend Technologies
配置php-fpm
[root@RHEL8 php-8.2.9]# cp php.ini-production /etc/php.ini
[root@RHEL8 php-8.2.9]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@RHEL8 php-8.2.9]# chmod +x /etc/rc.d/init.d/php-fpm
[root@RHEL8 php-8.2.9]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
[root@RHEL8 php-8.2.9]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
编辑查看
[root@RHEL8 php-8.2.9]# vim /usr/local/php7/etc/php-fpm.conf
[root@RHEL8 php-8.2.9]# tail /usr/local/php7/etc/php-fpm.conf
; files from a glob(3) pattern. This directive can be used everywhere in the
; file.
; Relative path can also be used. They will be prefixed by:
; - the global prefix if it's been set (-p argument)
; - /usr/local/php7 otherwise
include=/usr/local/php7/etc/php-fpm.d/*.conf
pm.max_children = 50 //最多同时提供50个进程提供50个并发服务
pm.start_servers = 5 //启动时启动5个进程
pm.min_spare_servers = 2 //最小空闲进程数
pm.max_spare_servers = 8 //最大空闲进程数
启动php-fpm
[root@RHEL8 php-8.2.9]# service php-fpm start
//默认情况下,fpm监听在127.0.0.1的9000端口,也可以使用如下命令验证其是否已经监听在相应的套接字
[root@RHEL8 php-8.2.9]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
3.3 配置apache
3.3.1 启用代理模块
在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
启用httpd
的相关模块
[root@RHEL8 ~]# sed -i '/proxy_module/s/#//g' /etc/httpd24/httpd.conf
[root@RHEL8 ~]# sed -i '/proxy_fcgi_module/s/#//g' /etc/httpd24/httpd.conf
3.3.2 配置虚拟主机
在需要使用fcgi的虚拟主机中添加类似如下两行:
ProxyRequests Off //关闭正向代理
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/PATH/TO/DOCUMENT_ROOT/$1
例如:
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/var/www/html/idfsoft.com/$1
以上设置表示把以.php结尾的文件请求发送到php-fpm
进程,php-fpm至少需要知道运行的目录和URI,所以这里直接在fcgi://127.0.0.1:9000
后指明了这两个参数,其它参数的传递已经被mod_proxy_fcgi.so
进行了封装,不需要手动指定。
注意:
这里写的/var/www/html/
是yum源安装方式生成的网页存放目录,这里必须改成你编译安装指定的网页存放路径,禁止直接复制我这里的路径
这里的idfsoft.com
是域名,你必须改成你所使用的域名,禁止直接复制此处的域名
这里的$1表示匹配所有以.php
结尾的http请求
[root@RHEL8 ~]# vim /usr/local/apache/htdocs/zhouwei.com/index.php
<?php
phpinfo();
?>
[root@RHEL8 ~]# chown -R apache.apache /usr/local/apache/htdocs/
[root@RHEL8 ~]# ll /usr/local/apache/htdocs/ -d
drwxr-xr-x 3 apache apache 43 Aug 31 04:49 /usr/local/apache/htdocs/
在配置文件的最后加入以下内容
~]# vim /etc/httpd24/httpd.conf
<VirtualHost *:80>
DocumentRoot "/usr/local/apache/htdocs/zhouwei.com"
ServerName www.zhouwei.com
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/zhouwei.com/$1
<Directory "/usr/local/apache/htdocs/zhouwei.com">
Options none
AllowOverride none
Require all granted
</Directory>
</VirtualHost>
[root@RHEL8 ~]# vim /etc/httpd24/httpd.conf
//搜索AddType,添加以下内容
# If the AddEncoding directives above are commented-out, then you
# 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 #添加此行
[root@RHEL8 ~]# sed -i '/ DirectoryIndex/s/index.html/index.php index.html/g' /etc/httpd24/httpd.conf
重启apache
服务
[root@RHEL8 ~]# apachectl start
[root@RHEL8 ~]# apachectl stop
httpd (no pid file) not running
[root@RHEL8 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
在真机C:\Windows\System32\drivers\etc\hosts
下面添加ip和域名
localhost name resolution is handled within DNS itself.
127.0.0.1 localhost
::1 localhost
192.168.198.130 www.zhouwei.com //添加ip和域名