• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

20.LANMP架构

开发技术 开发技术 5小时前 2次浏览

1 rpm实现LAMP

安装环境

10.0.0.2	HTTP
10.0.0.5 数据库服务器
[root@centos8 ~]# cat /etc/redhat-release 
CentOS Linux release 8.2.2004 (Core) 

1.1 RPM包的安装部署

范例:CentOS 8 默认使用factcgi模式,可以按下面步骤修改为httpd的模块方式

10.0.0.5安装数据库
#安装数据库
[root@centos8 ~]# yum -y install mariadb-server
[root@centos8 ~]# systemctl start mariadb
#授权远程连接账户
MariaDB [(none)]> grant all on *.* to 'test'@'10.0.0.%' identified by'123321';

在10.0.0.2上安装HTTP和PHP
#安装HTTP、PHP和数据库连接驱动
[root@centos8 ~]# yum -y install httpd php php-mysqlnd

[root@centos8 ~]# cat /etc/httpd/conf.modules.d/15-php.conf
<IfModule !mod_php5.c>
  <IfModule prefork.c>
    LoadModule php7_module modules/libphp7.so
  </IfModule>
</IfModule>
#从配置文件中看出模块支持prefork模式,所以将模块修改为支持httpd 模块prefork模式
[root@centos8 ~]# vim /etc/httpd/conf.modules.d/00-mpm.conf
LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
#LoadModule mpm_worker_module modules/mod_mpm_worker.so
#LoadModule mpm_event_module modules/mod_mpm_event.so

#修改为httpd模块方式,非必改项目
[root@centos8 html]#vim /etc/httpd/conf.d/php.conf
#<IfModule !mod_php5.c>
#  <IfModule !mod_php7.c>
#    # Enable http authorization headers
#    SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1
#
#    <FilesMatch .(php|phar)$>
#        SetHandler "proxy:unix:/run/php-fpm/www.sock|fcgi://localhost"
#    </FilesMatch>
#  </IfModule>
#</IfModule>

#添加测试页面
[root@centos8 ~]#cat /var/www/html/lamp.php
<?php
try {
        $user='test';		#数据库用户
        $pass='123321';		#数据库用户密码
        $dbh = new PDO('mysql:host=10.0.0.5;port=3306;dbname=mysql', $user, $pass);
        foreach($dbh->query('SELECT user,host from user') as $row) {
                print_r($row);
        }
        $dbh = null;
} catch (PDOException $e) {
        print "Error!: " . $e->getMessage() . "<br/>";
        die();
}
?>

#配置HTTP能识别.php文件
[root@centos8 ~]#vim /etc/httpd24/conf/httpd.conf
AddType application/x-httpd-php .php

#启动HTTP服务
[root@centos8 ~]#systemctl start httpd

#测试页面是否能访问
[root@centos8 ~]# curl http://10.0.0.2/lamp.php
Array
(
    [user] => test
    [0] => test
    [host] => 10.0.0.%
    [1] => 10.0.0.%
)

2 编译安装基于 FASTCGI 模式的wordpress

2.1 实验环境准备

10.0.0.2	HTTP+php(fastcgi模式)
10.0.0.5 mariadb数据库服务器

#软件版本
CentOS 8.2
mariadb 10.3.28

2.2 二进制安装Mariadb

在官方网站下载二进制安装包:https://downloads.mariadb.org/mariadb/10.3.30/

这里下载的是mariadb-10.3.30-linux-systemd-x86_64.tar.gz (for systems with systemd)版本,要求systemd和 GLIBC 2.19或者更高版本

#解压软件包并创建软链接
[root@centos8 src]# pwd
/usr/local/src
[root@centos8 src]# tar xf mariadb-10.3.30-linux-systemd-x86_64.tar.gz

#创建软链接方便之后版本升级,建议链接到/usr/local目录,这是mariadb.service的默认查找目录
[root@centos8 local]# ln -sv /usr/local/src/mariadb-10.3.30-linux-systemd-x86_64 mysql
'mysql' -> '/usr/local/src/mariadb-10.3.30-linux-systemd-x86_64'

#创建mariadb用户
[root@centos8 src]# useradd -r -s /sbin/nologin mysql
[root@centos8 src]# cd mysql/

#创建相应文件夹,赋予对应权限
[root@centos8 mysql]# chown -R root.root ./*
[root@centos8 mysql]# mkdir /data/mysql -p
[root@centos8 mysql]# chown -R mysql.mysql /data/mysql/
[root@centos8 mysql]# mkdir /etc/mysql
cat /etc/mysql/my.cnf
[mysqld]
datadir =/data/mysql
skip_name_resolve = ON

#安装数据库
[root@centos8 mysql]# ./scripts/mysql_install_db  --user=mysql --datadir=/data/mysql
Installing MariaDB/MySQL system tables in '/data/mysql' ...
OK

#配置环境变量
[root@centos8 mysql]# vim /etc/profile.d/lamp.sh
PATH=/usr/local/src/mysql/bin/:$PATH
[root@centos8 mysql]# . /etc/profile.d/lamp.sh

#拷贝服务启动配置
[root@centos8 local]# cp support-files/systemd/mariadb.service /usr/lib/systemd/system/mariadb.service
[root@centos8 local]# systemctl start mariadb

#连接数据库时报错
[root@centos8 mysql]# mysql -uroot
mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such file or directory
#安装对应库重新进入即可
[root@centos8 mysql]# yum install libncurses*

#创建wordpress数据库和账户
[root@centos8 mysql]# mysql -uroot
MariaDB [mysql]> create database wordpress;
Query OK, 1 row affected (0.004 sec)
MariaDB [mysql]> grant all on wordpress.* to wpuser@'10.0.0.%' identified by "database";
Query OK, 0 rows affected (0.007 sec)

2.3 编译安装httpd

官网下载安装包:https://apr.apache.org/download.cgi

apr-1.7.0.tar.gz 、apr-util-1.6.1.tar.gz、httpd-2.4.48.tar.gz

#安装依赖包
[root@centos8 data]# yum install gcc pcre-devel openssl-devel expat-devel -y

#解压安装包
[root@centos8 data]# tar xvf httpd-2.4.48.tar.gz
[root@centos8 data]# tar xvf apr-1.7.0.tar.gz
[root@centos8 data]# tar xvf apr-util-1.6.1.tar.gz

#将三者一并编译并安装
[root@centos8 data]# mv apr-1.7.0/ httpd-2.4.48/srclib/apr
[root@centos8 data]# mv apr-util-1.6.1 httpd-2.4.48/srclib/apr-util
[root@centos8 data]# cd httpd-2.4.48
./configure 
--prefix=/apps/httpd24 
--enable-so 
--enable-ssl 
--enable-cgi 
--enable-rewrite 
--with-zlib 
--with-pcre 
--with-included-apr 
--enable-modules=most 
--enable-mpms-shared=all 
--with-mpm=event
[root@centos8 httpd-2.4.48]# make -j 4 && make install

#配置环境变量
[root@centos8 httpd-2.4.48]# vim /etc/profile.d/lamp.sh
PATH=/apps/httpd24/bin:$PATH
[root@centos8 httpd-2.4.48]# . /etc/profile.d/lamp.sh 

#创建用户组
[root@centos8 data]# useradd -s /sbin/nologin -r apache
[root@centos8 data]# vim /apps/httpd24/conf/httpd.conf
user apache
group apache

#添加到开机自动启动服务
[root@centos8 data]# vim /usr/lib/systemd/system/httpd.service
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)
[Service]
Type=forking
#EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/apps/httpd24/bin/apachectl start
#ExecStart=/apps/httpd24/bin/httpd $OPTIONS -k start
ExecReload=/apps/httpd24/bin/apachectl graceful
#ExecReload=/apps/httpd24/bin/httpd $OPTIONS -k graceful
ExecStop=/apps/httpd24/bin/apachectl stop
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target

#启动
[root@centos8 data]# systemctl enable --now apache

2.4 编译安装 fastcgi 方式的 php 7.4.7

官方站点下载安装包:https://www.php.net/downloads

#安装相关依赖包
[root@centos8 data]# yum -y install gcc libxml2-devel bzip2-devel libmcrypt-devel sqlite-devel oniguruma

#编译安装PHP
[root@centos8 php-7.4.7]# cd php-7.4.7
./configure 
--prefix=/apps/php74 
--enable-mysqlnd 
--with-mysqli=mysqlnd 
--with-pdo-mysql=mysqlnd 
--with-openssl   
--with-zlib 
--with-config-file-path=/etc 
--with-config-file-scan-dir=/etc/php.d 
--enable-mbstring 
--enable-xml 
--enable-sockets 
--enable-fpm 
--enable-maintainer-zts 
--disable-fileinfo
#环境检查时报错
Package 'oniguruma', required by 'virtual:world', not found
#解决方式,安装后即可解决问题
[root@centos8 php-7.4.7]# dnf --enablerepo=PowerTools install oniguruma-devel
#正式开始编译安装
[root@centos8 php-7.4.7]# make -j 4 && make install

#配置环境变量
[root@centos8 php-7.4.7]# vim /etc/profile.d/lamp.sh
PATH=/apps/php74/bin:/apps/httpd24/bin:$PATH
 . /etc/profile.d/lamp.sh

#准备PHP相关文件
[root@centos8 php-7.4.7]# cp php.ini-production /etc/php.ini	拷贝配置文件
[root@centos8 php-7.4.7]# cp sapi/fpm/php-fpm.service /usr/lib/systemd/system	服务启动配置
[root@centos8 php74]# cd /apps/php74/etc/
[root@centos8 etc]# cp php-fpm.conf.default php-fpm.conf
[root@centos8 etc]# cd php-fpm.d/
[root@centos8 php-fpm.d]# cp www.conf.default www.conf
[root@centos8 php-fpm.d]# vim ./www.conf
user apache
group apache
ping.path = /ping		打开ping界面

#支持opcache加速
[root@centos8 etc]# vim /etc/php.ini
[opcache]
; Determines if Zend OPCache is enabled
opcache.enable=1
zend_extension=opcache.so

#启动服务php-fpm
[root@centos8 etc]# systemctl daemon-reload
[root@centos8 etc]# systemctl enable --now php-fpm.service

2.5 配置php-fpm

[root@centos8 etc]# vim /apps/httpd24/conf/httpd.conf
#取消下面两行的注释
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
#修改下面行
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
#加下面三行
AddType application/x-httpd-php .php
#AddType application/x-httpd-php-source .phps
ProxyRequests Off

#使用虚拟主机方式部署博客
<virtualhost *:80>
servername blog.wordpress.org
documentroot /data/wordpress
<directory /data/wordpress>
require all granted
</directory>
ProxyPassMatch ^/(.*.php)$ fcgi://127.0.0.1:9000/data/wordpress/$1
#实现status和ping页面
ProxyPassMatch ^/(fpm_status|ping)$ fcgi://127.0.0.1:9000/$1
CustomLog "logs/access_wordpress_log" common
</virtualhost>

2.6 部署wordpress

官网下载wordpress:https://cn.wordpress.org/download/#download-install

#解压
[root@centos8 src]# tar xvf wordpress-5.7.2-zh_CN.tar.gz
[root@centos8 src]# mv wordpress/ /data
[root@centos8 src]# setfacl -R -m u:apache:rwx /data/wordpress/

#测试访问是否成功
浏览器打开http://10.0.0.2/
出现wordpress引导页面表示安装成功

程序员灯塔
转载请注明原文链接:20.LANMP架构
喜欢 (0)