LAMP
L:linux
A:apache (httpd)
M:mysql, mariadb
M:memcached
P:php, perl, python
静态资源:原始形式与响应内容一致,在客户端浏览器执行
动态资源:原始形式通常为程序文件,需要在服务器端执行之后,将执行结果返回给客户端
基于php模块方式安装LAMP(一台主机)
[root@CentOS7 ~]# yum install httpd php php-mysql mariadb-server -y
[root@CentOS7 ~]# rpm -ql php
/etc/httpd/conf.d/php.conf
[root@CentOS7 ~]# cd /var/www/html/
[root@CentOS7 /var/www/html]# vim index.php 测试PHP
<?php
echo date("Y/m/d H:i:s");
phpinfo();
?>
[root@CentOS7 /var/www/html]# systemctl start httpd
使用PDO扩展连接数据库
[root@CentOS7 /var/www/html]# systemctl start mariadb
[root@CentOS7 /var/www/html]# cat pdo1.php 测试一
<?php
$dsn='mysql:host=localhost;dbname=test';
$username='root';
$passwd='';
$dbh=new PDO($dsn,$username,$passwd);
var_dump($dbh);
?>
[root@CentOS7 /var/www/html]# cat pdo2.php 测试二
<?php
try {
$user='root';
$pass='';
$dbh = new PDO('mysql:host=localhost;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();
}
?>
布署phpMyadmin
PhpMyAdmin是一个以PHP为基础,以Web-Base方式架构在网站主机上的MySQL的数据库管理工具,让管理者可用Web接口管理MySQL数据库
[root@CentOS7 /data]# tar xf phpMyAdmin-4.4.15.10-all-languages.tar.xz
[root@CentOS7 /data]# cd phpMyAdmin-4.4.15.10-all-languages/
[root@CentOS7 /data/phpMyAdmin-4.4.15.10-all-languages]# mv config.sample.inc.php config.inc.php
[root@CentOS7 /data/phpMyAdmin-4.4.15.10-all-languages]# vim config.inc.php
$cfg['blowfish_secret'] = 'centos'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */
[root@CentOS7 /data]# mv phpMyAdmin-4.4.15.10-all-languages /var/www/html/pma
[root@CentOS7 /data]# yum search mbstring
[root@CentOS7 /data]# yum install php-mbstring.x86_64
[root@CentOS7 /data]# systemctl restart httpd
[root@CentOS7 /data]# mysql_secure_installation
web登录创建数据库WordPress,创建用户wpuser及权限
[root@CentOS7 /data]# mysql -uwpuser -pcentos -h192.168.8.7
两台主机搭建博客架构
hostA httpd PHP wordpross
hostB mariadb-server
hostC 客户机
[root@hostA ~]# yum install php httpd php-mysql
[root@hostA /var/www/html]# vim /etc/php.ini
date.timezone = Asia/Shanghai
[root@hostA /var/www/html]# vim test.php
<?php
echo date("Y/m/d H:i:s");
phpinfo();
?>
[root@hostB ~]# yum install mariadb-server -y
[root@hostB ~]# systemctl start mariadb
MariaDB [(none)]> create database wpdb;
MariaDB [(none)]> grant all on wpdb.* to wpuser@'192.168.8.%' identified by 'centos';
方法一
[root@hostA /data/wordpress]# mv wp-config-sample.php wp-config.php
[root@hostA /data/wordpress]# vim wp-config.php
define('DB_NAME', 'wpdb');
define('DB_USER', 'wpuser');
define('DB_PASSWORD', 'centos');
define('DB_HOST', '192.168.8.17');
define('DB_CHARSET', 'utf8mb4');
[root@hostA /data]# mv wordpress /var/www/html/
方法二
[root@hostA /data]# tar xf wordpress-5.0.4-zh_CN.tar.gz -C /var/www/html/
[root@hostA /var/www/html]# setfacl -Rm u:apache:rwx wordpress
登录自动配置根据wp-config-sample.php自动生成wp-config.php
实现discuz! 实现论坛
[root@hostA /data]# unzip Discuz_X3.3_SC_UTF8.zip
[root@hostA /data]# mv upload /var/www/html/forum
[root@hostA /data]# cd /var/www/html/
[root@hostA /var/www/html]# setfacl -Rm u:apache:rwx forum/
[root@hostA /var/www/html]# systemctl start httpd
[root@hostB ~]# mysql
MariaDB [(none)]> create database dscdb;
MariaDB [(none)]> grant all on dscdb.* to dscuser@'192.168.8.%' identified by 'centos';
[root@hostA /var/www/html]# setfacl -Rb u:apache:rwx forum/
实现powerdns
PowerDNS 是一个跨平台的开源DNS服务组件,它是高性能的域名服务器,除了支持普通的BIND配置文件,PowerDNS还可从MySQL,Oracle,PostgreSQL等的数据库读取数据。PowerDNS安装了Poweradmin(基于php实现),能实现Web管理DNS记录,非常方便
[root@hostB ~]# mysql
MariaDB [(none)]> create database pdnsdb;
MariaDB [(none)]> grant all on pdnsdb.* to pduser@'192.168.8.%' identified by 'centos';
[root@hostA /data]# yum install -y pdns pdns-backend-mysql
[root@hostA /data]# vim /etc/pdns/pdns.conf
launch=gmysql
gmysql-host=192.168.8.17
gmysql-port=3306
gmysql-dbname=pdnsdb
gmysql-user=pduser
gmysql-password=centos
[root@hostA /data]# systemctl start pdns
[root@hostA /data]#yum -y install httpd php php-devel php-gd php-mcrypt php-imap php-ldap php-mysql php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-mhash gettext
[root@hostA /data]#systemctl start httpd
[root@hostA /data]#cd /var/www/html
[root@hostA /var/www/html]# wget http://downloads.sourceforge.net/project/poweradmin/poweradmin-2.1.7.tgz
[root@hostA /var/www/html]# tar xvf poweradmin-2.1.7.tgz
[root@hostA /var/www/html]# mv poweradmin-2.1.7 poweradmin
http://192.168.8.7/poweradmin/install/
PHP加速(编译安装xcache)
php的加速器
基于PHP的特殊扩展机制如opcode缓存扩展也可以将opcode缓存于php的共享内存中,从而可以让同一段代码的后续重复执行时跳过编译阶段以提高性能。这些加速器并非真正提高了opcode的运行速度,而仅是通过分析opcode后并将它们重新排列以达到快速执行的目的
常见的php加速器
- APC (Alternative PHP Cache)
- eAccelerator
- XCache
- NuSphere PhpExpress
不安装加速器压力测试
[root@CentOS7 ~]# ab -c10 -n 100 http://192.168.8.7/wordpress
Requests per second: 2589.06 [#/sec] (mean)
1、编译安装
[root@CentOS7 ~]# yum install gcc php-devel -y
[root@CentOS7 ~]# tar xf xcache-3.2.0.tar.gz
[root@CentOS7 ~]# cd xcache-3.2.0/
[root@CentOS7 /data/xcache-3.2.0]# phpize --clean && phpize
[root@CentOS7 /data/xcache-3.2.0]# ./configure --enable-xcache
[root@CentOS7 /data/xcache-3.2.0]# make && make install
[root@CentOS7 /data/xcache-3.2.0]# cp xcache.ini /etc/php.d/
[root@CentOS7 /data/xcache-3.2.0]# systemctl restart httpd
2、测试
[root@CentOS7 ~]# ab -c10 -n 100 http://192.168.8.7/wordpress
Requests per second: 2622.88 [#/sec] (mean)
PHP7以上版本性能更好,可以不使用加速器
基于fastcgi安装php-fpm
基于模块化安装PHP相当于装了一个插件,HTTPD处理相关php页面,基于fastcgi安装相当于装了一个独立的软件,专门处理php,性能更好
基于端口号php-fpm 的LAMP
1、删除xcache相关文件
[root@CentOS7 /data/xcache-3.2.0]# rm -f /etc/php.d/xcache.ini
[root@CentOS7 /data/xcache-3.2.0]# rm /usr/lib64/php/modules/xcache.so -f
2、安装php-fpm
[root@CentOS7 /data/xcache-3.2.0]# yum install php-fpm
[root@CentOS7 /data/xcache-3.2.0]# vim /etc/php-fpm.d/www.conf
listen.owner = apache
listen.group = apache
[root@CentOS7 /data/xcache-3.2.0]# vim /etc/httpd/conf.d/fcgi.conf
DirectoryIndex index.php
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/var/www/html/$1
[root@CentOS7 /data/xcache-3.2.0]# httpd -M |grep fcgi #查看是否开启fcgi模块
[root@CentOS7 /data/xcache-3.2.0]# systemctl start php-fpm.service
[root@CentOS7 /data/xcache-3.2.0]# systemctl restart httpd
基于socket安装php-fpm
- 支持httpd2.49以上版本
- 端口需要封装,socket性能更快
编译httpd2.4.43
[root@CentOS7 /data]# yum install gcc pcre-devel openssl-devel expat-devel -y
[root@CentOS7 /data]# ls
apr-1.7.0.tar.bz2 apr-util-1.6.1.tar.bz2 httpd-2.4.43.tar.bz2
[root@CentOS7 /data]# tar xvf apr-1.7.0.tar.bz2
[root@CentOS7 /data]# tar xvf apr-util-1.6.1.tar.bz2
[root@CentOS7 /data]# tar xvf httpd-2.4.43.tar.bz2
[root@CentOS7 /data]# mv apr-1.7.0 httpd-2.4.43/srclib/apr
[root@CentOS7 /data]# mv apr-util-1.6.1 httpd-2.4.43/srclib/apr-util
[root@CentOS7 /data]# cd httpd-2.4.43/
[root@CentOS7 /data/httpd-2.4.43]# ./configure \
> --prefix=/app/httpd24 \
> --enable-so \
> --enable-ssl \
> --enable-cgi \
> --enable-rewrite \
> --with-zlib \
> --with-pcre \
> --with-included-apr \
> --enable-modules=most \
> --enable-mpms-shared=all \
[root@CentOS7 /data/httpd-2.4.43]# make -j 4 && make install
2、配置环境
[root@CentOS7 /data/httpd-2.4.43]# echo 'PATH=/app/httpd24/bin:$PATH' >/etc/profile.d/http.sh
[root@CentOS7 /data/httpd-2.4.43]# source /etc/profile.d/http.sh
3、拷贝yum安装的http服务脚本
- 可以centos6上安装httpd拷贝过来service启动
- /app/httpd24/bin/apachectl start 在开机启动中配置(/etc/rc.d/rc.local)
[root@CentOS7 /data/httpd-2.4.43]# rpm -ql httpd |grep service
[root@CentOS7 /data/httpd-2.4.43]# cp /usr/lib/systemd/system/httpd.service /usr/lib/systemd/system/httpd64.service
[root@CentOS7 /data/httpd-2.4.43]# vim /usr/lib/systemd/system/httpd64.service
Type=forking
ExecStart=/app/httpd24/bin/httpd $OPTIONS -k start
#ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload=/app/httpd24/bin/httpd $OPTIONS -k graceful
4、配置httpd
[root@CentOS7 /data/httpd-2.4.43]# vim /app/httpd24/conf/httpd.conf
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
User apache
Group apache
LoadModule proxy_module modules/mod_proxy.so #注释掉
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
AddType application/x-httpd-php .php 找到AddType下面添加
AddType application/x-httpd-php-source .phps
ProxyRequests Off
ProxyPassMatch "^/(.*\.php(/.*)?)$" "unix:/var/run/php5-fpm.sock|fcgi://localhost/app/httpd24/htdocs/"
5、配置php-fpm
[root@CentOS7 /data/httpd-2.4.43]# vim /etc/php-fpm.d/www.conf
;listen = 127.0.0.1:9000 注释
listen=/var/run/php5-fpm.sock 添加
listen.owner = apache 解除注释
listen.group = apache
listen.mode = 0666
6、拷贝wordpress并启动服务
[root@CentOS7 /data/httpd-2.4.43]# cp /var/www/html/wordpress /app/httpd24/htdocs/ -av
[root@CentOS7 /data/httpd-2.4.43]# systemctl start httpd64
[root@CentOS7 /data/httpd-2.4.43]# systemctl enable httpd64
[root@CentOS7 /data/httpd-2.4.43]# systemctl start php-fpm
实现httpd php mysql独立成三台服务器
1、PHP主机安装配置PHP
[root@CentOS7 ~]# yum install php php-mysql
[root@CentOS7 ~]#vim /etc/php-fpm.d/www.conf
listen = 9000 注释
;listen-allowd-clients=127.0.0.1
2、httpd主机配置
[root@CentOS7 ~]#scp /var/www/html/wordpree 192.168.8.27/data
[root@CentOS7 /data/xcache-3.2.0]# vim /etc/httpd/conf.d/fcgi.conf
DirectoryIndex index.php
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.8.27:9000/data/$1
编译安装最新LAMP,实现多虚拟主机,一个虚拟主机blog.magedu.com 一个虚拟主机 forum.magedu.com (基于fcgi)
环境
- 一台apache+php-fpm
- 一台mariadb
软件版本
- apr-1.7.0.tar.bz2
- apr-util-1.6.1.tar.bz2
- httpd-2.4.43.tar.bz2
- php-7.4.8.tar.xz
- Discuz_X3.4_SC_UTF8.zip
- wordpress-5.4.2.tar.gz
- mariadb-10.2.25-linux-x86_64.tar.gz
1、二进制安装mariadb-10.2.25-linux-x86_64.tar.gz(mysql服务器)
[root@CentOS7 ~]# useradd -r -s /sbin/nologin -d /data/mysql mysql
[root@CentOS7 ~]# mkdir /data/mysql
[root@CentOS7 ~]# chown mysql.mysql /data/mysql/
[root@CentOS7 ~]# tar xf mariadb-10.2.25-linux-x86_64.tar.gz -C /usr/local/
[root@CentOS7 ~]# cd /usr/local/
[root@CentOS7 ~]# ln -s mariadb-10.2.25-linux-x86_64/ mysql
[root@CentOS7 /usr/local]# chown -R root.root mysql/
[root@CentOS7 /usr/local]# echo 'PATH=/usr/local/mysql/bin:$PATH' >/etc/profile.d/mysql.sh
[root@CentOS7 /usr/local]# . /etc/profile.d/mysql.sh
[root@CentOS7 /usr/local]# cd mysql/
[root@CentOS7 /usr/local/mysql]# scripts/mysql_install_db --datadir=/data/mysql --user=mysql
[root@CentOS7 /usr/local/mysql]# cp support-files/my-huge.cnf /etc/my.cnf -b
[root@CentOS7 /usr/local/mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@CentOS7 /usr/local/mysql]# chkconfig --add mysq
[root@CentOS7 /usr/local/mysql]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql #增加
[root@CentOS7 /usr/local/mysql]# systemctl start mysqld
[root@CentOS7 /usr/local/mysql]# mysql_secure_installation
创建环境
[root@CentOS7 /usr/local/mysql]# mysql -pcentos
MariaDB [(none)]> create database wordpress;
MariaDB [(none)]> create database discuz;
MariaDB [(none)]> grant all on wordpree.* to wordpress@'192.168.8.%' identified by 'centos';
MariaDB [(none)]> grant all on discuz.* to discuz@'192.168.8.%' identified by 'centos';
2编译安装httpd-2.4.43.tar.bz2
[root@CentOS7 /data]# yum install gcc pcre-devel openssl-devel expat-devel php-devel -y
[root@CentOS7 /data]# for p in *.bz2;do tar xf $p;done
[root@CentOS7 /data]# mv apr-1.7.0 httpd-2.4.43/srclib/apr
[root@CentOS7 /data]# mv apr-util-1.6.1 httpd-2.4.43/srclib/apr-util
[root@CentOS7 /data]# useradd -r -s /sbin/nologin apache
[root@CentOS7 /data]# cd httpd-2.4.43/
[root@CentOS7 /data/httpd-2.4.43]# ./configure \
--prefix=/app/httpd24 \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-included-apr \
--enable-modules=most \
--enable-mpms-shared=all
[root@CentOS7 /data/httpd-2.4.43]# make -j 4 && make install
[root@CentOS7 /data/httpd-2.4.43]# vim /app/httpd24/conf/httpd.conf
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
User apache
Group apache
LoadModule proxy_module modules/mod_proxy.so #注释掉
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
AddType application/x-httpd-php .php 找到AddType下面添加
AddType application/x-httpd-php-source .phps
ProxyRequests Off
<virtualhost *:80> 添加虚拟主机
servername blog.magedu.com
documentroot /data/wordpress
<directory /data/wordpress>
require all granted
</directory>
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/data/wordpress/$1
</virtualhost>
<virtualhost *:80>
servername forum.magedu.com
documentroot /data/discuz
<directory /data/discuz>
require all granted
</directory>
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/data/discuz/$1
</virtualhost>
[root@CentOS7 /data/httpd-2.4.43]# echo 'PATH=/app/httpd24/bin:$PATH' >/etc/profile.d/http.sh
[root@CentOS7 /data/httpd-2.4.43]# source /etc/profile.d/http.sh
[root@CentOS7 /data/httpd-2.4.43]# vim /etc/rc.d/rc.local #开机启动
[root@CentOS7 /data/httpd-2.4.43]# chmod +x /etc/rc.d/rc.local
[root@CentOS7 /data/httpd-2.4.43]# apachectl start
3、编译php-7.4.8.tar.xz
[root@CentOS7 /data]#yum install libxml2-devel bzip2-devel libmcrypt-devel libsqlite3x-devel oniguruma-devel(epel)
[root@CentOS7 /data]# cd php-7.4.8/
[root@CentOS7 /data/php-7.4.8]# ./configure --prefix=/app/php \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-openssl \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--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
[root@CentOS7 /data/php-7.4.8]# make && make install
[root@CentOS7 /data/php-7.4.8]# cp php.ini-production /etc/php.ini -b
[root@CentOS7 /data/php-7.4.8]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@CentOS7 /data/php-7.4.8]#chmod +x /etc/init.d/php-fpm
[root@CentOS7 /data/php-7.4.8]# chkconfig --add php-fpm
[root@CentOS7 /data/php-7.4.8]# chkconfig --list
[root@CentOS7 /data/php-7.4.8]# chkconfig php-fpm on
[root@CentOS7 /data/php-7.4.8]# cd /app/php/etc/
[root@CentOS7 /app/php/etc]# cp php-fpm.conf.default php-fpm.conf
[root@CentOS7 /app/php/etc]# cp php-fpm.d/www.conf.default php-fpm.d/www.conf
[root@CentOS7 /data]# vim /app/php/etc/php-fpm.d/www.conf
user = apache
group = apache
[root@CentOS7 /app/php/etc]# service php-fpm start
4、准备wordpress、discuz
[root@CentOS7 /data]# unzip Discuz_X3.4_SC_UTF8【20191201】.zip
[root@CentOS7 /data]# unzip wordpress-5.4.2.zip
[root@CentOS7 /data]# mv upload/* discuz/
[root@CentOS7 /data]# setfacl -Rm u:apache:rwx wordpress discuz/
- 以上PHP基于端口配置,还可以基于socket配置
- PHP版本php-7.4.8编译时部分配置不能识别,使用php-7.3.7.tar.xz 较好