LAMP动态网站架构
①静态网页
②动态网页
1、当客户端请求的是动态资源时,apache(httpd程序)会调用libphpX.so模块进行相应的解析
2、如果解析处理需要用到后台数据库相关数据,此时php程序也会连接后台数据库
3、最终php程序将解析后的结果返回给apache(httpd程序),让apache返回给客户
环境说明:
系统平台 | IP | 安装的服务 |
---|---|---|
CentOS8 | 192.168.92.131 | httpd-2.4 mysql-5.7 php php-mysql |
服务安装次序:
注意:php要求httpd使用prefork MPM
用到的网站链接:
//关闭防火墙与SELinux
[root@LAMP ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
[root@LAMP ~]# getenforce
Disabled
//配置YUM源
[root@LAMP ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@LAMP ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@LAMP ~]# dnf -y install epel-release
//创建apache服务的用户与组
[root@LAMP ~]# useradd -Mrs /sbin/nologin apache
[root@LAMP ~]# id apache
uid=995(apache) gid=992(apache) groups=992(apache)
//安装开发工具包及依赖包
[root@LAMP ~]# yum groups mark install 'Development Tools'
[root@LAMP ~]# yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++
//下载和安装apr和apr-util以及httpd
[root@LAMP ~]# wget https://mirrors.aliyun.com/apache/apr/apr-1.7.0.tar.gz
[root@LAMP ~]# wget https://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.gz
[root@LAMP ~]# wget https://mirrors.aliyun.com/apache/httpd/httpd-2.4.54.tar.gz
//把源码包移至/usr/local/src位置下并解压
[root@LAMP ~]# mv apr* /usr/local/src/
[root@LAMP ~]# mv httpd-2.4.54.tar.gz /usr/local/src/
[root@LAMP ~]# cd /usr/local/src/
[root@LAMP src]# tar -xf apr-1.7.0.tar.gz
[root@LAMP src]# tar -xf apr-util-1.6.1.tar.gz
[root@LAMP src]# tar -xf httpd-2.4.54.tar.gz
//编译安装apr与apr-util
[root@LAMP src]# cd apr-1.7.0/
[root@LAMP apr-1.7.0]# vim configure
cfgfile="${ofile}T"
trap "$RM \"$cfgfile\"; exit 1" 1 2 15
# $RM "$cfgfile" //将此行加上注释,或者删除此行
[root@LAMP apr-1.7.0]# ./configure --prefix=/usr/local/apr
........编译过程略..........
[root@LAMP apr-1.7.0]# make && make install
.........编译安装过程略........
[root@LAMP apr-1.7.0]# cd /usr/local/src/apr-util-1.6.1/
[root@LAMP apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
.........编译过程略............
[root@LAMP apr-util-1.6.1]# make && make install
.........编译安装过程略............
//编译安装httpd
[root@LAMP apr-util-1.6.1]# cd /usr/local/src/httpd-2.4.54
[root@LAMP httpd-2.4.54]# ./configure --prefix=/usr/local/apache --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@LAMP httpd-2.4.54]# make && make install
...........编译安装过程略...........
//安装后配置。映射头文件,库文件,man手册文件
[root@LAMP ~]# echo 'export PATH=$PATH:/usr/local/apache/bin/' > /etc/profile.d/httpd.sh
[root@LAMP ~]# source /etc/profile.d/httpd.sh
[root@LAMP ~]# ln -s /usr/local/apache/include/ /usr/include/httpd
[root@LAMP ~]# echo 'MANPATH /usr/local/apache/man' >> /etc/man.config
//取消ServerName前面的注释
[root@LAMP ~]# sed -i '/#ServerName/s/#//g' /usr/local/apache/conf/httpd.conf
//配置httpd开机自启的脚本
[root@LAMP ~]# vim /usr/lib/systemd/system/httpd.service
[root@LAMP ~]# cat /usr/lib/systemd/system/httpd.service
[Unit]
Description=httpd 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
[root@LAMP system]# systemctl daemon-reload //重载生效配置
[root@LAMP system]# systemctl restart httpd.service //开启httpd服务
[root@LAMP system]# systemctl enable httpd.service //httpd设为开机自启
//查看httpd服务的默认端口80是否开启
[root@LAMP system]# ss -anlt
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 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
//安装依赖包
[root@LAMP ~]# dnf -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
//创建mysql服务的用户与组
[root@LAMP ~]# useradd -Mrs /sbin/nologin mysql
[root@LAMP ~]# id mysql
uid=994(mysql) gid=991(mysql) groups=991(mysql)
//下载mysql二进制包,解压该包
[root@LAMP ~]# cd /usr/local/src/
[root@LAMP src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
[root@LAMP src]# tar -xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@LAMP src]# cd /usr/local/
[root@LAMP local]# mv mysql-5.7.38-linux-glibc2.12-x86_64/ mysql
//修改目录的属主属组为mysql
[root@LAMP local]# chown -R mysql.mysql /usr/local/mysql
[root@LAMP local]# ll -d mysql/
drwxr-xr-x 9 mysql mysql 129 Aug 2 18:48 mysql/
//添加环境变量,映射头文件、库文件、man手册
[root@LAMP mysql]# echo 'export PATH=$PATH:/usr/local/mysql/bin' > /etc/profile.d/mysql.sh
[root@LAMP mysql]# source /etc/profile.d/mysql.sh
[root@LAMP mysql]# ln -s /usr/local/mysql/include/ /usr/local/include/mysql
[root@LAMP mysql]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@LAMP mysql]# ldconfig
[root@LAMP mysql]# echo 'MANPATH /usr/local/mysql/man' >> /etc/man.config
//创建数据存放目录,并修改属主属组为mysql
[root@LAMP mysql]# mkdir /opt/data
[root@LAMP mysql]# chown -R mysql.mysql /opt/data/
[root@LAMP mysql]# ll -d /opt/data/
drwxr-xr-x 2 mysql mysql 6 Aug 2 18:54 /opt/data/
//初始化数据库
[root@LAMP mysql]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2022-08-02T10:54:50.460373Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-08-02T10:54:50.625962Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-08-02T10:54:50.654697Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-08-02T10:54:50.711891Z 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: 885298ff-1251-11ed-85c7-000c299ee3c1.
2022-08-02T10:54:50.713328Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-08-02T10:54:50.815484Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-08-02T10:54:50.815524Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-08-02T10:54:50.815955Z 0 [Warning] CA certificate ca.pem is self signed.
2022-08-02T10:54:50.888670Z 1 [Note] A temporary password is generated for root@localhost: H6e9f,Jt3<me
//把初始化后的临时密码保存,后面登录要用到
[root@LAMP mysql]# echo 'H6e9f,Jt3 /root/mysql.passwd
//添加配置文件
[root@LAMP mysql]# vim /etc/my.cnf
[root@LAMP mysql]# cat /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
//配置服务启动脚本
[root@LAMP mysql]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@LAMP mysql]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@LAMP mysql]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
//启动mysql
[root@LAMP mysql]# ss -anlt
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@LAMP mysql]# mysql -uroot -p'H6e9f,Jt3
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.38
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('123456');
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
//测试新密码是否能登录
[root@LAMP mysql]# mysql -uroot -p123456
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 3
Server version: 5.7.38 MySQL Community Server (GPL)
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> exit
Bye
[root@LAMP mysql]#
//首先登录遭遇的报错问题
[root@LAMP mysql]# mysql -uroot -p'H6e9f,Jt3
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
经过分析,原因是缺少’ libncurses.so.5 '这个库文件
解决该问题的方式:
//首先查询该库文件所属哪个包
[root@LAMP mysql]# dnf provides libncurses.so.5
Last metadata expiration check: 2:36:23 ago on Tue 02 Aug 2022 04:22:58 PM CST.
ncurses-compat-libs-6.1-9.20180224.el8.i686 : Ncurses compatibility libraries
Repo : base
Matched from:
Provide : libncurses.so.5
//找到该包后下载,安装完成后再尝试登录即可
[root@LAMP mysql]# dnf -y install ncurses-compat-libs
...........下载安装过程略............
//从php官网下载php7的源码包,下载后校验一下该包是否完整
[root@LAMP ~]# wget https://www.php.net/distributions/php-7.4.30.tar.gz
[root@LAMP ~]# sha256sum php-7.4.30.tar.gz
e37ea37e0f79109351ac615da85eb7c2c336101fc5bc802ee79a124a4310dc10 php-7.4.30.tar.gz
//把上面这一大串值跟php官网的值做对比
//安装依赖包
[root@LAMP ~]# dnf -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
.............下载安装过程略................
//解压源码包
[root@LAMP ~]# mv php-7.4.30.tar.gz /usr/local/src/
[root@LAMP ~]# cd /usr/local/src/
[root@LAMP src]# tar -xf php-7.4.30.tar.gz
//编译安装php
[root@LAMP 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@LAMP php-7.4.30]# make -j $(cat /proc/cpuinfo |grep processor|wc -l) //此方式编译速度会加快
.............编译过程略...........
[root@LAMP php-7.4.30]# make install
..............安装过程略...........
//配置环境变量
[root@LAMP ~]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[root@LAMP ~]# source /etc/profile.d/php7.sh
[root@LAMP ~]# which php //验证php的环境变量是否配置成功
/usr/local/php7/bin/php
[root@LAMP ~]# php -v //查看php的版本信息
PHP 7.4.30 (cli) (built: Aug 2 2022 19:38:49) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
//配置php-fpm
[root@LAMP ~]# cd /usr/local/src/php-7.4.30/
[root@LAMP php-7.4.30]# cp php.ini-production /etc/php.ini
[root@LAMP php-7.4.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@LAMP php-7.4.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@LAMP php-7.4.30]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
[root@LAMP php-7.4.30]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
//启动php
[root@LAMP php-fpm.d]# service php-fpm start
Starting php-fpm done
[root@LAMP php-fpm.d]# service php-fpm status
php-fpm (pid 209351) is running...
[root@LAMP php-fpm.d]# 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 128 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
1、sqlite3
//报错信息
configure: error: Package requirements (sqlite3 > 3.7.4) were not met:
Package 'sqlite3', required by 'virtual:world', not found
解决方式:
[root@LAMP ~]# dnf -y install libsqlite3x-devel
2、oniguruma
//报错信息
configure: error: Package requirements (oniguruma) were not met:
Package 'oniguruma', required by 'virtual:world', not found
解决方式:
[root@LAMP ~]# dnf -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
在apache httpd 2.4以后已经专门有一个模块针对FastCGI的实现,此模块为mod_proxy_fcgi.so,它其实是作为mod_proxy.so模块的扩展,因此,这两个模块都要加载,编辑httpd.conf文件,取消以下两行内容的注释:
//启用httpd的相关模块
[root@LAMP ~]# sed -i '/proxy_module/s/#//g' /usr/local/apache/conf/httpd.conf
[root@LAMP ~]# sed -i '/proxy_fcgi_module/s/#//g' /usr/local/apache/conf/httpd.conf
//这里的$1表示匹配所有以.php结尾的http请求
在需要使用fcgi的虚拟主机中添加类似如下两行:
ProxyRequests Off //关闭正向代理
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/PATH/TO/DOCUMENT_ROOT/$1
实战演示:
[root@LAMP extra]# pwd
/usr/local/apache/conf/extra
[root@LAMP extra]# vim httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/usr/local/apache/htdocs/yf"
ServerName yf.example.com
ErrorLog "yf.example.com-error_log"
CustomLog "yf.com-access_log" common
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/yf/$1
<Directory "/usr/local/apache/htdocs/yf">
Options none
AllowOverride none
Require all granted
</Directory>
</VirtualHost>
[root@LAMP htdocs]# pwd
/usr/local/apache/htdocs
[root@LAMP htdocs]# mkdir yf
[root@LAMP htdocs]# vim yf/index.php
[root@LAMP htdocs]# cat yf/index.php
<?php
phpinfo();
?>
[root@LAMP conf]# pwd
/usr/local/apache/conf
//搜索AddType,添加以下内容
[root@LAMP conf]# vim httpd.conf
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
[root@LAMP conf]# sed -i '/ DirectoryIndex/s/index.html/index.php index.html/g' /usr/local/apache/conf/httpd.conf
[root@LAMP conf]# systemctl restart httpd.service
phpmyadmin下载地址
//下载phpmyadmin包
[root@LAMP ~]# wget https://files.phpmyadmin.net/phpMyAdmin/5.2.0/phpMyAdmin-5.2.0-all-languages.zip
//把该解压在网站的根目录
[root@LAMP ~]# mv phpMyAdmin-5.2.0-all-languages.zip /usr/local/apache/htdocs/yf/
[root@LAMP ~]# cd /usr/local/apache/htdocs/yf/
[root@LAMP yf]# unzip phpMyAdmin-5.2.0-all-languages.zip
//把目录的名字改为phpmyadmin
[root@LAMP yf]# mv phpMyAdmin-5.2.0-all-languages/ phpmyadmin
[root@LAMP yf]# cd phpmyadmin/
//复制样本配置文件到config.inc.php文件
[root@LAMP phpmyadmin]# cp config.sample.inc.php config.inc.php
[root@LAMP phpmyadmin]# systemctl restart httpd.service
浏览器访问:
该用户名和密码填写数据库的root用户的密码。