Debian11配置Apache2使用PHP-FPM - 荒岛
荒岛最近要部署一个php程序只支持apache,属实有点蛋疼,用了几年的nginx,早就把apache的配置方法给忘光了,没办法只能抓紧时间重新学一下apache2.4的配置,这篇文章记录一下配置过程。
默认情况下apache2.4还是用的mpm_prefork和mod_php来跑php,但是用这种方法早就已经过时了。
目前推荐的做法是使用mpm_event和phpfpm,所以这篇文章只介绍一下这种方法。
实际上这篇文章也算是走了一遍在debian11上搭建lamp的整个流程。。做事做全套吧。。
安装apache2:
apt -y updateapt -y install apache2
安装mariadb:
apt -y install mariadb-server
添加php8的存储库:
apt -y install apt-transport-https ca-certificates lsb-releasewget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpgecho "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.listapt -y update
安装phpfpm:
apt -y install php8.0-fpm
顺便把程序需要用到的其他php扩展也装上:
apt -y install php8.0-mysql php8.0-imagick php8.0-gd php8.0-curl php8.0-dom php8.0-mbstring php8.0-zip php8.0-bcmath php8.0-intl
启动以及设置apache2/mariadb/phpfpm开机自启:
systemctl enable --now apache2 mariadb php8.0-fpm
[重要]这里有个特别需要注意的地方,如果之前系统安装过这个包:libapache2-mod-php8.0
这个包会把apache的mpm模式改为prefork,我们需要手动改回event:
a2dismod php8.0a2dismod mpm_preforka2enmod mpm_event
查看当前apache的mpm模式,正常情况下应该回显event:
a2query -M
没有安装过这个包的话就不用管了。
启用proxy_fcgi和setenvif模块,用于支持phpfpm:
a2enmod proxy_fcgi setenvif
重启apache2使其生效:
systemctl restart apache2
启用php8的fpm配置:
a2enconf php8.0-fpm
重载apache2使其生效:
systemctl reload apache2
启用rewrite模块,用于支持.htaccess文件:
a2enmod rewrite
重启apache2使其生效:
systemctl restart apache2
rewrite模块虽然启用了,但由于debian默认的配置不允许使用.htaccess文件,所以还需要修改配置文件。
这里提供一个覆写配置的方案,就不用改动原来官方的配置了,在conf-available目录新建一个配置文件:
nano /etc/apache2/conf-available/allow-override.conf
写入如下配置:
AllowOverride all
然后启用这个配置:
a2enconf allow-override
重载apache2使其生效:
systemctl reload apache2
新建站点(vhost)配置文件:
nano /etc/apache2/sites-available/example.conf
写入如下配置:
ServerName apache.example.com ServerAdmin webmaster@localhost DocumentRoot /var/www/example ErrorLog ${APACHE_LOG_DIR}/example_error.log CustomLog ${APACHE_LOG_DIR}/example_access.log combined
检查配置是否正确:
apache2ctl configtest
正常的话就可以启用这个站点(vhost)了,命令后面跟的是站点配置文件的文件名:
a2ensite example
站点目录所有者和使用nginx+phpfpm时一样,都是使用www-data这个用户,所以要更改的话可以使用下面的命令:
chown -R www-data:www-data /var/www/example
站点启用后也是需要重载apache2才能生效的:
systemctl reload apache2
现在可以仍一个phpinfo上去看看配置是否正常,能正常访问到phpinfo并在serverapi这里显示fpm/fastcgi说明正常:
一路走到这里就基本熟悉apache2在debian上的配置了,实际上就靠这几个命令行工具就能完成绝大多数的配置:
a2ensite // 启用站点a2dissite // 关闭站点a2enconf // 启用配置文件a2disconf // 关闭配置文件a2enmod // 启用模块a2dismod // 关闭模块a2query // 查看apache2的各种信息
启用站点、关闭站点、启用配置文件、关闭配置文件,这些操作都可以通过reload来生效,启用模块、关闭模块则必须通过restart来生效。
这几个命令实际对应也就只操作下面这几个目录:
/etc/apache2/sites-available // 可用站点/etc/apache2/sites-enabled // 已经启用的站点/etc/apache2/conf-available // 可用配置文件/etc/apache2/conf-enabled // 已经启用的配置文件/etc/apache2/mods-available // 可用模块/etc/apache2/mods-enabled // 已经启用的模块
启用了某个站点、配置文件、模块就只是把available目录内的配置文件做一个符号链接到enabled目录内,关闭的话就是删除符合链接。
a2query更是一个非常好用的查看apache2的状态工具,比如要查看当前启用的模块:
a2query -m
会列出当前已经启用的模块:
authn_file (enabled by maintainer script)authz_host (enabled by maintainer script)filter (enabled by maintainer script)dir (enabled by maintainer script)alias (enabled by maintainer script)auth_basic (enabled by maintainer script)proxy_fcgi (enabled by site administrator)reqtimeout (enabled by maintainer script)setenvif (enabled by maintainer script)mime (enabled by maintainer script)rewrite (enabled by site administrator)negotiation (enabled by maintainer script)authn_core (enabled by maintainer script)mpm_event (enabled by site administrator)ssl (enabled by site administrator)authz_core (enabled by maintainer script)autoindex (enabled by maintainer script)deflate (enabled by maintainer script)authz_user (enabled by maintainer script)proxy (enabled by site administrator)access_compat (enabled by maintainer script)env (enabled by maintainer script)socache_shmcb (enabled by site administrator)status (enabled by maintainer script)
签发ssl证书这块还是可以用certbot:
apt -y install python3-certbot-apache
然后一条命令即可完事:
certbot --apache
剩下的就和apache2没啥关系了,数据库初始化:
mysql_secure_installation
新版本的mariadb10.5,和以往有点不一样,会提示你是否要切换到unix_socket authentication,这里选n:
Enter current password for root (enter for none):Switch to unix_socket authentication [Y/n] nChange the root password? [Y/n] YRemove anonymous users? [Y/n] YDisallow root login remotely? [Y/n] YRemove test database and access to it? [Y/n] YReload privilege tables now? [Y/n] Y
创建数据库和用户并授权:
mysql -u root -pCREATE DATABASE example CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;GRANT ALL PRIVILEGES ON example.* TO example@localhost IDENTIFIED BY 'password';FLUSH PRIVILEGES;quit
关于phpfpm的配置,这里稍微提一嘴,我个人推荐用ondemand模式来管理phpfpm的进程,用这个模式的话不会浪费你的机器内存,对于一些机器内存不大的机器来说比较友好。
默认情况下是dynamic模式,要修改的话,编辑如下配置文件:
nano /etc/php/8.0/fpm/pool.d/www.conf
找到下面的配置修改:
pm = ondemandpm.max_children = 5pm.process_idle_timeout = 30s;pm.max_requests = 500
在ondemand模式下只有pm.max_children参数是生效的,这个参数直接指定最大的phpfpm进程数量。我这里就只设置了5,对于一般站点来说够用了,不够的话自己往上加。
php8的php.ini配置:
nano /etc/php/8.0/fpm/php.ini
一般就是这些配置改改就行了:
max_execution_time = 300post_max_size = 100Mupload_max_filesize = 100Mmemory_limit = 256M
本文章由 flowerss 抓取自RSS,版权归源站点所有。