1.LAMP架构介绍
LAMP是Linux+Apache(httpd)+MySQL+PHP的简写,即把Apache、MySQL以及PHP安装在linux系统上,组成一个运行环境来运行PHP脚本语言,通常是网站。比如Google、淘宝、百度、51cto博客、猿课论坛等就是用PHP语言写出来的。
2.web服务器工作流程
在说lamp架构平台的搭建前,我们先来了解下什么的CGI,什么是FastCGI,什么是 web服务器的资源分为两种,静态资源和动态资源。
静态资源就是指静态内容,客户端从服务器获得的资源的表现形式与原文件相同,可以简单的理解为就是直接存储于文件系统中的 资源。
动态资源则通常是程序文件,需要在服务器执行之后,将执行的结果返回给客户端。
那么web服务器如何执行程序并将结果返回给客户端呢?下面通过一张图来说明一下web服务器如何处理客户端的请求:
如上图所示
阶段①显示的是httpd服务器(即apache)和php服务器通过FastCGI协议进行通信,且php作为独立的服务器进程运行
阶段②显示的是php程序和mysql数据库间通过mysql协议进行通信。php与mysql本没有什么联系,但是由php语言写成的程序可以与mysql进行数据交互。同理perl和python写的程序也可以与mysql数据库进行交互
通过上面的图说明一下web的工作流程:
客户端通过http协议请求web服务器资源
web服务器收到请求后判断客户端请求的资源是静态资源还是动态资源
若是静态资源则直接从本地文件系统取之返回给客户端
否则若为动态资源则通过FastCGi协议与php服务器联系,通过CGI程序的master进程调度worker进程来执行程序以获得客户端请求的动态资源,并将执行的结果通过FastCGI协议返回给httpd服务器,httpd服务器收到的php的执行结果后将其封装为http响应报文响应给客户端。在执行程序获取动态资源时若需要获得数据库中的资源时,由php服务器通过mysql协议与Mysql/MariaDB服务器交互,取之而后返回给httpd,httpd将从php服务器收到的执行结果封装为http响应给客户端
3.lamp平台构建
环境说明:
系统平台 | IP | 需要安装的服务 |
---|---|---|
centos7 redhat7 | 192.168.209.12 | httpd-2.4 mysql-5.7 php php-mysql |
lamp平台软件安装次序:
http -- > mysql -- > php
注意:php要求httpd使用prowork MPM
4.实例演示:
//关闭防火墙firewalld
//关闭seLinux
1.安装httpd
//安装开发工具包
[root@lanzhiyong ~]# yum groups mark install 'Development Tools'
//创建apache服务的用户和组
[root@lanzhiyong ~]# groupadd -r apache
[root@lanzhiyong ~]# useradd -r -M -s /sbin/nologin -g apache apache
//安装依赖包
[root@lanzhiyong ~]# yum -y install openssl-devel pcre-devel expat-devel libtool
//下载和安装apar以及apr-util
[root@lanzhiyong ~]# cd /usr/src/
[root@lanzhiyong src]# wget http://mirrors.shu.edu.cn/apache//apr/apr-1.6.3.tar.bz2
[root@lanzhiyong src]# wget http://mirrors.shu.edu.cn/apache//apr/apr-util-1.6.1.tar.bz2
[root@lanzhiyong src]# tar xf apr-1.6.3.tar.bz2
[root@lanzhiyong src]# tar xf apr-util-1.6.1.tar.bz2
[root@lanzhiyong src]# cd apr-1.6.3
[root@lanzhiyong apr-1.6.3]# vim configure
/$cfgfile //将此行加上注释
[root@lanzhiyong apr-1.6.3]# ./configure --prefix=/usr/local/apr
[root@lanzhiyong apr-1.6.3]# make -j 8 && make install
[root@lanzhiyong src]# cd apr-util-1.6.1
[root@lanzhiyong apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@lanzhiyong apr-util-1.6.1]# make -j 8 && make install
//编译安装httpd
[root@lanzhiyong ~]# wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.4.34.tar.bz2
[root@lanzhiyong ~]# tar xf httpd-2.4.34.tar.bz2
[root@lanzhiyong ~]# cd httpd-2.4.34
[root@lanzhiyong httpd-2.4.34]# ./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@lanzhiyong httpd-2.4.34]# make -j 4 && make install
//安装后配置
[root@lanzhiyong ~]# echo 'PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
[root@lanzhiyong ~]# source /etc/profile.d/httpd.sh
[root@lanzhiyong ~]# ln -s /usr/local/apache/include/ /usr/include/httpd
[root@lanzhiyong ~]# echo 'MANPATH /usr/local/apache/man' >> /etc/man_db.conf
//取消ServerName前面的注释
[root@lanzhiyong ~]# sed -i '/#ServerName/s/#//g' /etc/httpd24/httpd.conf
//启动apache
[root@lanzhiyong ~]# apachectl start
[root@lanzhiyong ~]# ss -antl
2.安装mysql 数据库
//安装依赖 包
[root@lanzhiyong ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
//创建用户和组
[root@lanzhiyong ~]# cd /usr/src/
[root@lanzhiyong src]# groupadd -r -g 306 mysql
[root@lanzhiyong src]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql
//下载二进制格式的mysql软件包
[root@lanzhiyong src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
//解压软件至/usr/local
[root@lanzhiyong src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@lanzhiyong src]# cd /usr/local/
//创建软连接
[root@lanzhiyong local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql
//修改目录/usr/local/mysql的属主属组
[root@lanzhiyong local]# chown -R mysql.mysql /usr/local/mysql
[root@lanzhiyong local]# ll /usr/local/mysql
//添加环境变量
[root@lanzhiyong ~]# ls /usr/local/mysql
[root@lanzhiyong ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@lanzhiyong ~]# source /etc/profile.d/mysql.sh
[root@lanzhiyong ~]# echo $PATH
//建立数据存放目录
[root@lanzhiyong ~]# mkdir /opt/data
[root@lanzhiyong ~]# chown -R mysql.mysql /opt/data/
[root@lanzhiyong ~]# ll /opt/
总用量 0
drwxr-xr-x. 2 mysql mysql 6 8月 19 13:20 data
//初始化数据库 注意这个命令后会生成临时密码 要记住 jd?ajfrKY4pQ
[root@lanzhiyong ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
//配置mysql
//软连接
[root@lanzhiyong ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
[root@lanzhiyong ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@lanzhiyong ~]# ldconfig -v
//生成配置文件
[root@lanzhiyong ~]# 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@lanzhiyong ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@lanzhiyong ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@lanzhiyong ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
//启动mysql
[root@lanzhiyong ~]# service mysqld start
[root@lanzhiyong ~]# ps -ef |grep mysql
[root@lanzhiyong ~]# ss -antl
//修改密码 使用临时密码登入
[root@lanzhiyong ~]# mysql -uroot -p
jd?ajfrKY4pQ 这是以上步骤的临时密码
mysql> set password = password('lanzhiyong');
3.安装php
//配置yum源
[root@lanzhiyong ~]# cd /etc/yum.repos.d/
[root@lanzhiyong yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@lanzhiyong ~]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@lanzhiyong ~]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@lanzhiyong ~]# yum install -y epel-release
//安装依赖包
[root@lanzhiyong ~]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel
//下载php
[root@lanzhiyong ~]# cd /usr/src/
[root@lanzhiyong src]# wget http://cn.php.net/distributions/php-7.2.8.tar.xz
//编译安装php [root@lanzhiyong src]# tar xf php-7.2.8.tar.xz
[root@lanzhiyong src]# cd php-7.2.8
[root@lanzhiyong php-7.2.8]#
./configure --prefix=/usr/local/php7 \
--with-curl \
--with-freetype-dir \
--with-gd \
--with-gettext \
--with-iconv-dir \
--with-kerberos \
--with-libdir=lib64 \
--with-libxml-dir=/usr \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-openssl \
--with-pcre-regex \
--with-pdo-mysql \
--with-pdo-sqlite \
--with-pear \
--with-jpeg-dir \
--with-png-dir \
--with-xmlrpc \
--with-xsl \
--with-zlib \
--with-config-file-path=/etc \
--with-config-file-scan-dir=/etc/php.d \
--with-bz2 \
--enable-fpm \
--enable-bcmath \
--enable-libxml \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-opcache \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-xml \
--enable-zip
[root@lanzhiyong php-7.2.8]# make -j $(cat /proc/cpuinfo |grep processor |wc -l)
[root@lanzhiyong php-7.2.8]# make install
//安装后配置
[root@lanzhiyong ~]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[root@lanzhiyong ~]# source /etc/profile.d/php7.sh
[root@lanzhiyong ~]# cd /usr/src/php-7.2.8
[root@lanzhiyong php-7.2.8]# which php
/usr/local/php7/bin/php
[root@lanzhiyong php-7.2.8]# php -v
//配置php-fpm
[root@lanzhiyong php-7.2.8]# cp php.ini-production /etc/php.ini
[root@lanzhiyong php-7.2.8]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@lanzhiyong php-7.2.8]# chmod +x /etc/rc.d/init.d/php-fpm
[root@lanzhiyong php-7.2.8]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
[root@lanzhiyong php-7.2.8]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
//配置fpm的相关选项为你所需的值:
[root@lanzhiyong ~]# vim /usr/local/php7/etc/php-fpm.conf
*****在配置文件最后面添加一下内容
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
[root@lanzhiyong ~]# tail /usr/local/php7/etc/php-fpm.conf
//启动php-fpm
[root@lanzhiyong ~]# service php-fpm start
Starting php-fpm done
[root@lanzhiyong ~]# ss -antl
******php-fpm监听在9000端口
[root@lanzhiyong ~]# ps -ef |grep php
//////////////////配置apache
//启用httpd的相关模块
[root@lanzhiyong ~]# sed -i '/proxy_module/s/#//g' /etc/httpd24/httpd.conf
[root@lanzhiyong ~]# sed -i '/proxy_fcgi_module/s/#//g' /etc/httpd24/httpd.conf
///////////////////////配置虚拟主机
//创建虚拟主机目录并生成php测试页面
[root@lanzhiyong ~]# mkdir /usr/local/apache/htdocs/lanzhiyong.com
[root@lanzhiyong ~]# cat > /usr/local/apache/htdocs/lanzhiyong.com/index.php << EOF
> phpinfo();
> ?>
> EOF
[root@lanzhiyong ~]# chown -R apache.apache /usr/local/apache/htdocs/
[root@lanzhiyong ~]# ll -d /usr/local/apache/htdocs/
drwxr-xr-x. 3 apache apache 46 8月 19 15:35 /usr/local/apache/htdocs/
//在配置文件的最后加入一下内容
[root@lanzhiyong ~]# vim /etc/httpd24/httpd.conf
ServerName www.lanzhiyong.com
DocumentRoot "/usr/local/apache/htdocs/lanzhiyong.com"
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/lanzhiyong.com/$1
Options none
AllowOverride none
Require all granted
[root@lanzhiyong ~]# vim /etc/httpd24/httpd.conf
//搜索AddType 添加以下两行
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@lanzhiyong ~]# sed -i '/ DirectoryIndex/s/index.html/index.php index.html/g' /etc/httpd24/httpd.conf
//重启apache服务
[root@lanzhiyong ~]# apachectl stop
[root@lanzhiyong ~]# ss -antl
[root@lanzhiyong ~]# apachectl start
[root@lanzhiyong ~]# ss -antl
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 127.0.0.1:9000 *:*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306 :::*
5.验证:
1.修改/etc/hosts文件,添加域名与IP的映射
2.在浏览器上使用域名访问,若看到以下界面则表示lamp架构搭建成功,否则请检查你的操作