构建lamp(php-fpm方式)
一、准备一台centos7虚拟机,ip为192.168.10.30。同时作为httpd服务器,fpm服务器,mariadb服务器。192.168.10.20测试机
使用yum仓库下载mariadbhttpd版本:2.4.6,php-fpm版本:5.4.16,mariadb版本:5.5.64,php-mysql版本:5.4.16,php-mbstring版本:5.4.16
(1)安装并配置MariaDB服务
# yum -y install mariadb-server编辑mariadb的配置文件添加常用选项
# vim /etc/my.cnf.d/server.cnfskip_name_resolve=ON 跳过名称解析
innodb_file_per_table=ON 每表使用单独的表空间文件
文章图片
启动mariadb并开机自启动
# systemctl start mariadb本地连接测试:
# systemctl enable mariadb
# mysql安全加固
Welcome to the MariaDB monitor.Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.64-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help; ' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>exit
Bye
# mysql_secure_installation加固之后,再使用用户名密码登陆
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE!PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user.If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):为空,直接回车
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] Y设置root密码
New password:输入新密码
Re-enter new password: 确认新密码
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.This is intended only for testing, and to make the installation
go a bit smoother.You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] Y删除默认的匿名用户
... Success!
Normally, root should only be allowed to connect from 'localhost'.This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] Y禁止root管理员远程登陆,建议禁止
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access.This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] n是否删除名为test的测试库
... skipping.
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] Y重载特权表
... Success!
Cleaning up...
All done!If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
# mysql -uroot -h127.0.0.1 -plhp@ssw0rd授权一个普通用户做后面的测试,用户名 myuser密码mypass
Welcome to the MariaDB monitor.Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.64-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help; ' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> GRANT ALL ON testdb.* TO 'myuser'@'192.168.10.%' IDENTIFIED BY 'mypass';刷新授权表
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> FLUSH PRIVILEGES;退出sql用新建的用户测试连接
Query OK, 0 rows affected (0.00 sec)
# mysql -umyuser -h192.168.10.30 -pmypass【构建lamp(php-fpm方式)】创建数据库testdb并指定默认字符集为utf8
Welcome to the MariaDB monitor.Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 5.5.64-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help; ' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> CREATE DATABASE testdb CHARACTER SET 'utf8';
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> exit
Bye
(2)安装并配置pmp-fpm服务 确保没有和php同时安装
# yum info php还在仓库中
文章图片
# yum info php-fpm还在仓库中
文章图片
安装php-fpm和连接数据库的php-mysql和支持多字符的php-mbstring和加解密的php-mcrypt
# yum -y install php-fpm php-mysql php-mbstring php-mcrypt服务配置文件:/etc/php-fpm.conf , /etc/php-fpm.d/*.conf
/etc/php-fpm.d/www.conf中的关键参数:
listen = 127.0.0.1:9000监听的主机和端口,跨主机部署需要修改php环境配置文件:/etc/php.ini, /etc/php.d/*.ini
; listen.backlog = -1后援队列,等待队列,请求等待,-1表示无限制
listen.allowed_clients = 127.0.0.1 允许哪些主机有权限连接请求,跨主机部署amp时需要修改
user = apache运行进程的用户
group = apache运行进程的组
pm = dynamic连接池运行为动态
pm.max_children = 50运行的最大子进程数
pm.start_servers = 5服务刚启动是运行的子进程个数
pm.min_spare_servers = 5最少空闲子进程个数
pm.max_spare_servers = 35最大空闲子进程数
; pm.max_requests = 500每个子进程响应500个请求后重新起一个子进程
; pm.status_path = /status内置状态页
; ping.path = /ping服务远程健康状态测试
; ping.response = pong服务远程健康状态测试
php_value[session.save_path] = /var/lib/php/session会话持久保持在这个目录
创建session保存的目录,默认没创建,在/etc/php-fpm.d/www.conf中定义
文章图片
# mkdir -pv /var/lib/php/session设置运行用户apache,组apache,和php-fpm一致
mkdir: created directory ‘/var/lib/php/session’
# chown apache:apache /var/lib/php/session/启动php-fpm服务
# systemctl start php-fpm(3)安装并配置httpd服务 安装启动httpd
# ss -tnl
# yum -y install httpd配置一个虚拟主机做测试
# systemctl start httpd
# systemctl enable httpd
# vim /etc/httpd/conf.d/vhosts.conf创建虚拟主机对应的网页目录及文件
ServerName www.b.net
DocumentRoot "/apps/vhosts/b.net"
Options None
AllowOverride None
Require all granted
# mkdir -pv /apps/vhosts/b.net语法检查
# touch /apps/vhosts/b.net/index.html
# vim /apps/vhosts/b.net/index.html
test page
# httpd -t重启httpd服务及关闭防火墙和SELinux
# systemctl restart httpd关闭SELinux
# systemctl stop firewalld
# setenforce 0测试
配置httpd通过fpm访问动态资源
增加/etc/httpd/conf.d/vhost.conf的参数
DirectoryIndex index.php 主页支持index.php
ProxyRequests Off 关闭正向代理
ProxyPassMatch^/(.*\.php)$fcgi://127.0.0.1:9000/apps/vhosts/b.net/$1正则表达式模式匹配,如果用户请求的URL是以任意字符开头但以.php结尾,那么我们就把他反代到 fcgi://127.0.0.1:9000端口 ,指定动态网页存放路径为/apps/vhosts/b.net/$1,$1为后向引用,引用第一个括号中的内容,在正则表达式外用$引用,在正则表达式中用\引用
文章图片
进入虚拟主机配置文件目录
# cd /apps/vhosts/b.net/将原来的静态页面改名保持,创建新的动态资源.php
# mv index.html test.html重启服务测试
# vim index.php
phpinfo()
?>
# systemctl restart httpd
文章图片
(4)设置能通过phpmyadmin网页管理数据库 使用宿主机下载phpmyadmin图形工具用于图形化管理数据库:由于当前php版本为5.4.16,所以从https://www.phpmyadmin.net/files/找到降低版本phpMyAdmin-4.0.10.20-all-languages.zip,上传至centos7服务器/root目录下。
切换到/root目录对文件进行解压
# cd ~
# unzip phpMyAdmin-4.0.10.20-all-languages.zip
文章图片
将解压后的文件移动至虚拟主机所在的DocumentRoot路径下的phpmyadmin目录
# mv phpMyAdmin-4.0.10.20-all-languages /apps/vhosts/b.net/phpmyadmin切换当前目录到/apps/vhosts/b.net/phpmyadmin/目录
# cd /apps/vhosts/b.net/phpmyadmin/复制其中的文件config.sample.inc.php 命名为config.inc.php
# cp config.sample.inc.php config.inc.php编辑配置文件config.inc.php,添加随机数,此版本默认有可以不填,有的版本没有必须填写
# vim config.inc.php
文章图片
测试机浏览器打开192.168.10.30/phpmyadmin/index.php即可访问
文章图片
输入此前设置的数据库root用户名密码即可登陆
文章图片
推荐阅读
- CGI,FastCGI,PHP-CGI与PHP-FPM
- Flutter的ListView
- 构建App(一)(框架与结构)
- 如何在手机上查看测试vue-cli构建的项目
- 用Go构建区块链——3.持久化和命令行
- 运用flutter|运用flutter 构建一个发布版(release)APK
- 8、Flask构建弹幕微电影网站-搭建后台页面-密码修改、主页控制面板
- 倾诉
- 构建你的知识体系,让你学习效能倍增!
- 理解和构建自我|理解和构建自我 ——致女儿的第三封信