web服务器工作流程
在说lamp架构平台的搭建前,我们先来了解下什么是CGI,什么是FastCGI,什么是…
web服务器的资源分为两种,静态资源和动态资源
- 静态资源就是指静态内容,客户端从服务器获得的资源的表现形式与原文件相同。可以简单的理解为就是直接存储于文件系统中的资源
- 动态资源则通常是程序文件,需要在服务器执行之后,将执行的结果返回给客户端
那么web服务器如何执行程序并将结果返回给客户端呢?下面通过一张图来说明一下web服务器如何处理客户端的请求
如上图所示
阶段①显示的是httpd服务器(即apache)和php服务器通过FastCGI协议进行通信,且php作为独立的服务进程运行
阶段②显示的是php程序和mysql数据库间通过mysql协议进行通信。php与mysql本没有什么联系,但是由Php语言写成的程序可以与mysql进行数据交互。同理perl和python写的程序也可以与mysql数据库进行交互
cgi与fastcgi
上图阶段①中提到了FastCGI,下面我们来了解下CGI与FastCGI。
CGI(Common Gateway Interface,通用网关接口),CGI是外部应用程序(CGI程序)与WEB服务器之间的接口标准,是在CGI程序和Web服务器之间传递信息的过程。CGI规范允许Web服务器执行外部程序,并将它们的输出发送给Web浏览器,CGI将web的一组简单的静态超媒体文档变成一个完整的新的交互式媒体。
FastCGI(Fast Common Gateway Interface)是CGI的改良版,CGI是通过启用一个解释器进程来处理每个请求,耗时且耗资源,而FastCGI则是通过master-worker形式来处理每个请求,即启动一个master主进程,然后根据配置启动几个worker进程,当请求进来时,master会从worker进程中选择一个去处理请求,这样就避免了重复的生成和杀死进程带来的频繁cpu上下文切换而导致耗时
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连接的建立和断开等
- FastCGI:利用php-fpm机制,启动为服务进程,php自行运行为一个服务,https通过socket与php通信
较于CGI方式,FastCGI更为常用,很少有人使用CGI方式来加载动态资源
web工作流程
通过上面的图说明一下web的工作流程:
- 客户端通过http协议请求web服务器资源
- web服务器收到请求后判断客户端请求的资源是静态资源或是动态资源
- 若是静态资源则直接从本地文件系统取之返回给客户端。
- 否则若为动态资源则通过FastCGI协议与php服务器联系,通过CGI程序的master进程调度worker进程来执行程序以获得客户端请求的动态资源,并将执行的结果通过FastCGI协议返回给httpd服务器,httpd服务器收到php的执行结果后将其封装为http响应报文响应给客户端。在执行程序获取动态资源时若需要获得数据库中的资源时,由Php服务器通过mysql协议与MySQL/MariaDB服务器交互,取之而后返回给httpd,httpd将从php服务器收到的执行结果封装成http响应报文响应给客户端。
lamp平台构建
环境说明:
系统平台 | IP | 需要安装的服务 |
---|---|---|
centos7 redhat7 | 172.16.12.128 | httpd-2.4 mysql-5.7 php php-mysql |
lamp平台软件安装次序:
httpd --> mysql --> php
安装httpd
//YUM源配置
[root@localhost yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@localhost yum.repos.d]# cd
[root@localhost ~]# yum install -y wget vim
CentOS-8.5.2111 - Base - mirrors.aliyun.com 17 kB/s | 3.9 kB 00:00
CentOS-8.5.2111 - Extras - mirrors.aliyun.com 10 kB/s | 1.5 kB 00:00
CentOS-8.5.2111 - AppStream - mirrors.aliyun.com 25 kB/s | 4.3 kB 00:00
Package wget-1.19.5-10.el8.x86_64 is already installed.
Package vim-enhanced-2:8.0.1763-19.el8.4.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
[root@localhost ~]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
Last metadata expiration check: 0:01:59 ago on Tue 02 Aug 2022 04:04:49 PM CST.
epel-release-latest-8.noarch.rpm 66 kB/s | 24 kB 00:00
Package epel-release-8-16.el8.noarch is already installed.
Dependencies resolved.
Nothing to do.
Complete!
[root@localhost yum.repos.d]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@localhost yum.repos.d]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
//安装开发工具包
[root@localhost ~]# yum groups mark install 'Development Tools'
//创建apache服务的用户和组
[root@localhost ~]# useradd -r -M -s /sbin/nologin apache
The memcache was not invalidated by NSS responder.
[root@localhost ~]# id apache
uid=994(apache) gid=991(apache) groups=991(apache)
//安装依赖包
[root@localhost ~]# yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++
//下载和安装apr以及apr-util
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://downloads.apache.org/apr/apr-1.6.5.tar.bz2
[root@localhost src]# wget https://downloads.apache.org/apr/apr-util-1.6.1.tar.bz2
[root@localhost src]# wget https://downloads.apache.org/httpd/httpd-2.4.54.tar.bz2
[root@localhost src]# tar xf apr-1.6.5.tar.bz2
[root@localhost src]# tar xf apr-util-1.6.1.tar.bz2
[root@localhost src]# tar xf httpd-2.4.54.tar.bz2
ls[root@localhost src]# ls
apr-1.6.5 apr-util-1.6.1 debug httpd-2.4.54.tar.bz2
apr-1.6.5.tar.bz2 apr-util-1.6.1.tar.bz2 httpd-2.4.54 kernels
[root@localhost apr-1.6.5]# vim configure
cfgfile=${ofile}T
trap "$RM \"$cfgfile\"; exit 1" 1 2 15
# $RM "$cfgfile" //将此行加上注释
[root@localhost apr-1.6.5]# ./configure --prefix=/usr/local/apr
[root@localhost apr-1.6.5]# make && make install
//安装apr-util
[root@localhost apr-1.6.5]# cd ../apr-util-1.6.1/
[root@localhost apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@localhost apr-util-1.6.1]# make && make install
//编译安装httpd
[root@localhost httpd-2.4.54]# ./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@localhost httpd-2.4.54]# make && make install
//安装后配置环境变量
[root@localhost httpd-2.4.54]# echo "export PATH=$PATH:/usr/local/apache/bin" > /etc/profile.d/httpd.sh
[root@localhost httpd-2.4.54]# ln -s /usr/local/apache/include/ /usr/include/httpd
[root@localhost httpd-2.4.54]# vim /etc/man_db.conf
MANDATORY_MANPATH /usr/local/apache/man
[root@localhost httpd-2.4.54]# source /etc/profile.d/httpd.sh
[root@localhost system]# cat httpd.service
[Unit]
Description=apache server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start
ExecStop=/usr/local/apache/bin/apachectl stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
测试:
 gid=990(mysql) groups=990(mysql)
//下载二进制格式的mysql软件包
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# tar xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost src]# cd /usr/local/
[root@localhost local]# ln -s mysql-5.7.38-linux-glibc2.12-x86_64/ mysql
[root@localhost local]# chown -R mysql.mysql mysql*
[root@localhost local]# ll -d mysql*
lrwxrwxrwx. 1 mysql mysql 36 Aug 2 16:28 mysql -> mysql-5.7.38-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 mysql mysql 129 Aug 2 16:27 mysql-5.7.38-linux-glibc2.12-x86_64
//解压软件至/usr/local/
[root@localhost ~]# tar xf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
//修改目录/usr/local/mysql的属主属组
[root@localhost local]# chown -R mysql.mysql mysql
[root@localhost local]# chown -R mysql.mysql mysql-5.7.37-linux-glibc2.12-x86_64/
[root@localhost local]# ll
total 0
drwxr-xr-x. 2 root root 6 May 19 2020 bin
drwxr-xr-x. 2 root root 6 May 19 2020 etc
drwxr-xr-x. 2 root root 6 May 19 2020 games
drwxr-xr-x. 2 root root 6 May 19 2020 include
drwxr-xr-x. 2 root root 6 May 19 2020 lib
drwxr-xr-x. 3 root root 17 Jun 27 21:48 lib64
drwxr-xr-x. 2 root root 6 May 19 2020 libexec
lrwxrwxrwx. 1 mysql mysql 36 Jul 26 17:57 mysql -> mysql-5.7.37-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 mysql mysql 129 Jul 26 17:56 mysql-5.7.37-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 May 19 2020 sbin
drwxr-xr-x. 5 root root 49 Jun 27 21:48 share
drwxr-xr-x. 2 root root 6 May 19 2020 src
//添加环境变量
[root@localhost local]# ls mysql
bin docs include lib LICENSE man README share support-files
[root@localhost local]# echo 'export PATH=$PATH:/usr/local/mysql/bin/' > /etc/profile.d/mysql.sh
[root@localhost local]# source /etc/profile.d/mysql.sh
[root@localhost local]# ln -s /usr/local/mysql/include /usr/include/mysql
[root@localhost local]# ll /usr/include/ | grep mysql
lrwxrwxrwx. 1 root root 24 Jul 26 18:00 mysql -> /usr/local/mysql/include
[root@localhost local]# vim /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
[root@localhost local]# ldconfig
[root@localhost local]# vi /etc/man_db.conf
......
MANDATORY_MANPATH /usr/local/mysql/man
......
//建立数据存放目录
[root@localhost local]# cd
[root@localhost ~]# mkdir /opt/data
[root@localhost ~]# chown -R mysql.mysql /opt/data/
//初始化数据库
[root@localhost ~]# mysqld --initialize --user=mysql --datadir=/opt/data/
2022-07-26T10:06:15.217572Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-26T10:06:15.366362Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-26T10:06:15.388023Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-26T10:06:15.442314Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 95cb821b-0cca-11ed-8584-000c295e7eb4.
2022-07-26T10:06:15.443312Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-26T10:06:16.926673Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-26T10:06:16.926689Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-26T10:06:16.927065Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-26T10:06:17.043394Z 1 [Note] A temporary password is generated for root@localhost: lQTaUm,T<7WA
//注意:这个命令的最后会生成一个临时的随机密码,此处密码是 “lQTaUm,T<7WA”。
[root@localhost ~]# echo 'lQTaUm,T<7WA' > password
[root@localhost ~]# ls
anaconda-ks.cfg mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz password
//生成配置文件
[root@localhost ~]# vim /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
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
//配置服务启动脚本
[root@localhost ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@localhost ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
//启动mysql
[root@localhost ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'.
SUCCESS!
[root@localhost ~]# 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 [::]:22 [::]:*
[root@localhost ~]# ps -ef | grep mysqld
root 11023 1 0 18:12 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql 11223 11023 1 18:12 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=localhost.localdomain.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root 11258 1581 0 18:13 pts/0 00:00:00 grep --color=auto mysql
//使用临时密码登录
[root@localhost ~]# mysql -p
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
[root@localhost ~]# yum whatprovides libncurses.so.5
Last metadata expiration check: 0:19:41 ago on Tue 26 Jul 2022 05:55:09 PM CST.
ncurses-compat-libs-6.1-7.20180224.el8.i686 : Ncurses compatibility
: libraries
Repo : baseos
Matched from:
Provide : libncurses.so.5
ncurses-compat-libs-6.1-9.20180224.el8.i686 : Ncurses compatibility
: libraries
Repo : baseos
Matched from:
Provide : libncurses.so.5
[root@localhost ~]# yum install -y ncurses-compat-libs
//修改密码,设置新密码
[root@localhost ~]# cat password
lQTaUm,T<7WA
[root@localhost ~]# mysql -p'lQTaUm,T<7WA'
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.37
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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('skye123!');
Query OK, 0 rows affected, 1 warning (0.00 sec)
安装php
//安装依赖包
[root@localhost system]# 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
//下载php
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://www.php.net/distributions/php-7.4.30.tar.xz
[root@localhost src]# tar xf php-7.4.30.tar.xz
//编译安装php
[root@localhost src]# cd php-7.4.30
[root@localhost php-7.4.30]# ./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 \
--enable-gd \
--with-jpeg \
--with-zlib-dir \
--with-freetype \
--with-gettext \
--enable-json \
--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-posix
//安装后配置
[root@localhost php-7.4.30]# cd /usr/local/php7/
[root@localhost php7]# ls
bin etc include lib php sbin var
[root@localhost php7]# echo 'export PATH=$PATH:/usr/local/php7/bin' > /etc/profile.d/php7.sh
[root@localhost php7]# source /etc/profile.d/php7.sh
[root@localhost php7]# ln -s /usr/local/php7/include/ /usr/include/php7
[root@localhost php7]# echo '/usr/local/php7/lib/' > /etc/ld.so.conf.d/php7.conf
[root@localhost php7]# ldconfig
//配置php-fpm
[root@localhost php7]# cd /usr/src/php-7.4.30
[root@localhost php-7.4.30]# cp php.ini-production /etc/php.ini
cp: overwrite '/etc/php.ini'? y
[root@localhost php-7.4.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-7.4.30]# ll -d /etc/init.d/php-fpm
-rw-r--r--. 1 root root 2402 Aug 2 23:38 /etc/init.d/php-fpm
[root@localhost php-7.4.30]# chmod +x /etc/init.d/php-fpm
[root@localhost php-7.4.30]# cd /usr/local/php7/etc/
[root@localhost etc]# ls
pear.conf php-fpm.conf.default php-fpm.d
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]# cd php-fpm.d/
[root@localhost php-fpm.d]# ls
www.conf.default
[root@localhost php-fpm.d]# cp www.conf.default www.conf
w-r–r–. 1 root root 2402 Aug 2 23:38 /etc/init.d/php-fpm
[root@localhost php-7.4.30]# chmod +x /etc/init.d/php-fpm
[root@localhost php-7.4.30]# cd /usr/local/php7/etc/
[root@localhost etc]# ls
pear.conf php-fpm.conf.default php-fpm.d
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]# cd php-fpm.d/
[root@localhost php-fpm.d]# ls
www.conf.default
[root@localhost php-fpm.d]# cp www.conf.default www.conf
##