LCMP (Linux + Caddy + MariaDB + PHP)

技术 秋水逸冰 12767浏览 10评论

Caddy Web Server 自v2.6 版本发布以后,就默认支持 HTTP/3。可以说是最早支持 HTTP/3 的 Web Server,而 Nginx 直到 2023 年 5 月 23 日 1.25.0 版本才开始体验支持。
至于 Apache httpd 也不知道要到猴年马月才支持 HTTP/3 了。
按照本文教程,即可搭建 LCMP (Linux + Caddy + MariaDB + PHP) 环境。同时亦可使用本文提供的脚本,快速安装。

LCMP 一键安装

2023 年 11 月 6 日更新
就是想简单搭建一个 Web 服务器,支持 MariaDB 和 PHP 而已,又对编译安装感到不耐烦?
没关系,来吧,LCMP (Linux + Caddy2 + MariaDB + PHP ) 一把梭,一般 10 分钟内搞定环境搭建。
支持系统:
Enterprise Linux 7 (CentOS 7, RHEL 7)
Enterprise Linux 8 (CentOS 8, RHEL 8, Rocky Linux 8, AlmaLinux 8, Oracle Linux 8)
Enterprise Linux 9 (CentOS 9, RHEL 9, Rocky Linux 9, AlmaLinux 9, Oracle Linux 9)
Debian 10
Debian 11
Debian 12
Ubuntu 20.04
Ubuntu 22.04
详情:https://github.com/teddysun/lcmp

1. 事前准备

禁用 SElinux

cat /etc/selinux/config

如果没有发现 SELINUX=disabled 这一行,则需要禁用。

sed -i 's@^SELINUX.*@SELINUX=disabled@g' /etc/selinux/config
setenforce 0

如果系统是 Enterprise Linux 9 (CentOS 9, RHEL 9, Rocky Linux 9, AlmaLinux 9),则需要用如下方法禁用。

grubby --update-kernel ALL --args selinux=0

然后执行 reboot 重启系统。待到重启完成即禁用了 SElinux。
设置防火墙 firewall

firewall-cmd --state

如果显示是 running 状态,则需要放行 80 和 443 端口,也就是 http 和 https 服务。

default_zone=$(firewall-cmd --get-default-zone)
firewall-cmd --permanent --add-service=https --zone=${default_zone}
firewall-cmd --permanent --add-service=http --zone=${default_zone}
firewall-cmd --permanent --add-masquerade
firewall-cmd --reload
firewall-cmd --list-all

 

2. 安装和设置 Caddy Web Server

引入 caddy repo 以及安装 caddy
如果系统是 Enterprise Linux 9 (CentOS 9, RHEL 9, Rocky Linux 9, AlmaLinux 9), Enterprise Linux 8 (CentOS 8, RHEL 8, Rocky Linux 8, AlmaLinux 8)

dnf install -y dnf-plugins-core
dnf copr enable @caddy/caddy -y && dnf install -y caddy && caddy version

如果系统是 RHEL 7 或 CentOS 7

yum install -y yum-plugin-copr && yum copr enable @caddy/caddy -y && yum install -y caddy

创建必要的目录,设置目录权限

mkdir -p /data/www/default
mkdir -p /var/log/caddy/
mkdir -p /etc/caddy/conf.d/
chown -R caddy.caddy /data/www/default
chown -R caddy.caddy /var/log/caddy/

网站的根目录为 /data/www/default,等全部的安装过程结束后,便可将应用程序放到该目录下运行了。
编辑 caddy 默认配置文件 /etc/caddy/Caddyfile

{
	admin off
}
:80 {
	# Set this path to your site's directory.
	root * /data/www/default
	encode gzip
	# Enable the static file server.
	file_server {
		index index.html
	}
	# Serve a PHP site through php-fpm:
	php_fastcgi unix//run/php-fpm/www.sock
	log {
		output file /var/log/caddy/access.log
	}
}
import /etc/caddy/conf.d/*.conf

caddy 默认只开启了 80 端口,如果想要搭建自己的网站,则需要手动创建配置文件并重启 caddy 服务。
创建网站,以 www.example.com 为例。
创建 /etc/caddy/conf.d/www.example.com.conf 配置文件,内容如下:

www.example.com {
	header {
		Strict-Transport-Security "max-age=31536000; preload"
		X-Content-Type-Options nosniff
		X-Frame-Options SAMEORIGIN
	}
	# Set this path to your site's directory.
	root * /data/www/default
	encode gzip
	# Serve a PHP site through php-fpm:
	php_fastcgi unix//run/php-fpm/www.sock
	# Enable the static file server.
	file_server {
		index index.html
	}
	log {
		output file /var/log/caddy/ssl_access.log {
			roll_size 100mb
			roll_keep 3
			roll_keep_for 7d
		}
	}
}

3. 安装和设置 MariaDB

引入 MariaDB repo 以及安装 MariaDB

wget -qO mariadb_repo_setup.sh https://downloads.mariadb.com/MariaDB/mariadb_repo_setup
chmod +x mariadb_repo_setup.sh

The current maintained versions are: 10.3, 10.4, 10.5, 10.6, 10.11 (maintained for 5 years), 10.8, 10.9, 10.10, 11.0 (maintained for one year)
当前 MariaDB 的长期支持版本为 10.3, 10.4, 10.5, 10.6, 10.11,选择 10.11 即可。

./mariadb_repo_setup.sh --mariadb-server-version=mariadb-10.11

上述脚本执行完毕后,即引入 MariaDB repo,下面开始安装 MariaDB。

dnf install -y MariaDB-common MariaDB-server MariaDB-client MariaDB-shared MariaDB-backup

安装完毕后,编辑 /etc/my.cnf.d/server.cnf,使其默认编码为 utf8mb4

lnum=$(sed -n '/\[mariadb\]/=' /etc/my.cnf.d/server.cnf)
sed -i "${lnum}acharacter-set-server = utf8mb4\n\n\[client-mariadb\]\ndefault-character-set = utf8mb4" /etc/my.cnf.d/server.cnf

启动 MariaDB

systemctl start mariadb

修改用户 root 的密码,删除 test 数据库及不必要的用户名。

db_pass="Thisisdbrootpassword"
mysql -e "grant all privileges on *.* to root@'127.0.0.1' identified by \"${db_pass}\" with grant option;"
mysql -e "grant all privileges on *.* to root@'localhost' identified by \"${db_pass}\" with grant option;"
mysql -uroot -p${db_pass} 2>/dev/null <<EOF
drop database if exists test;
delete from mysql.db where user='';
delete from mysql.db where user='PUBLIC';
delete from mysql.user where user='';
delete from mysql.user where user='mysql';
delete from mysql.user where user='PUBLIC';
flush privileges;
exit
EOF

4. 安装和设置 PHP

引入 PHP repo
如果系统是 Enterprise Linux 9 (CentOS 9, RHEL 9, Rocky Linux 9, AlmaLinux 9) x86_64 aarch64

dnf config-manager --set-enabled crb
dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm
dnf install -y https://rpms.remirepo.net/enterprise/remi-release-9.rpm

如果系统是 Enterprise Linux 8 (CentOS 8, RHEL 8, Rocky Linux 8, AlmaLinux 8) x86_64

dnf config-manager --set-enabled powertools
dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
dnf install -y https://rpms.remirepo.net/enterprise/remi-release-8.rpm

如果系统是 RHEL 7 or CentOS 7 x86_64

yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum install -y yum-utils

安装 PHP
如果系统是 Enterprise Linux 9 (CentOS 9, RHEL 9, Rocky Linux 9, AlmaLinux 9), Enterprise Linux 8 (CentOS 8, RHEL 8, Rocky Linux 8, AlmaLinux 8)

dnf module reset -y php
dnf module install -y php:remi-8.2

如果系统是 RHEL 7 or CentOS 7 x86_64

yum-config-manager --disable 'remi-php*'
yum-config-manager --enable remi-php82

接着安装 PHP 其他必要的组件。

dnf install -y php-cli php-bcmath php-embedded php-gd php-imap php-mysqlnd php-dba php-pdo php-pdo-dblib php-pgsql php-odbc php-enchant php-gmp php-intl php-ldap php-snmp php-soap php-tidy php-opcache php-process php-pspell php-shmop php-sodium php-ffi php-brotli php-lz4 php-xz php-zstd
dnf install -y php-pecl-imagick-im7 php-pecl-zip php-pecl-mongodb php-pecl-swoole5 php-pecl-grpc php-pecl-yaml php-pecl-uuid

确认安装 PHP 版本及模块。

php -v
php -m

编辑 PHP 的 php-fpm 配置文件 /etc/php-fpm.d/www.conf,使其支持 caddy

sed -i "s@^user.*@user = caddy@" /etc/php-fpm.d/www.conf
sed -i "s@^group.*@group = caddy@" /etc/php-fpm.d/www.conf
sed -i "s@^listen.acl_users.*@listen.acl_users = apache,nginx,caddy@" /etc/php-fpm.d/www.conf
sed -i "s@^;php_value\[opcache.file_cache\].*@php_value\[opcache.file_cache\] = /var/lib/php/opcache@" /etc/php-fpm.d/www.conf

更改 PHP 的目录权限,使其支持 caddy

chown root.caddy /var/lib/php/session
chown root.caddy /var/lib/php/wsdlcache
chown root.caddy /var/lib/php/opcache

编辑 PHP 的配置文件 /etc/php.ini,使其更加符合生产环境,以及支持 MariaDB 连接

sed -i "s@^disable_functions.*@disable_functions = passthru,exec,shell_exec,system,chroot,chgrp,chown,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore@" /etc/php.ini
sed -i "s@^max_execution_time.*@max_execution_time = 300@" /etc/php.ini
sed -i "s@^max_input_time.*@max_input_time = 300@" /etc/php.ini
sed -i "s@^post_max_size.*@post_max_size = 50M@" /etc/php.ini
sed -i "s@^upload_max_filesize.*@upload_max_filesize = 50M@" /etc/php.ini
sed -i "s@^expose_php.*@expose_php = Off@" /etc/php.ini
sed -i "s@^short_open_tag.*@short_open_tag = On@" /etc/php.ini

sock_location="/var/lib/mysql/mysql.sock"
sed -i "s#mysqli.default_socket.*#mysqli.default_socket = ${sock_location}#" /etc/php.ini
sed -i "s#pdo_mysql.default_socket.*#pdo_mysql.default_socket = ${sock_location}#" /etc/php.ini

 

5. 启动 PHP 和 caddy 服务

启动 PHP 的 php-fpm 服务

systemctl start php-fpm

启动 caddy 服务

systemctl start caddy

允许 mariadb, php-fpm, caddy 服务开机自启动

systemctl enable mariadb
systemctl enable php-fpm
systemctl enable caddy

确认 mariadb, php-fpm, caddy 服务状态

systemctl status mariadb
systemctl status php-fpm
systemctl status caddy

确认 mariadb, php-fpm, caddy 服务的进程

ps -ef | grep -v grep | grep "/usr/bin/caddy"
ps -ef | grep -v grep | grep php-fpm
ps -ef | grep -v grep | grep mariadbd

 

6. 升级 PHP 版本的注意事项

当 PHP 有新版本需要升级时,只需执行以下命令即可。

yum update -y php-*

需要注意的是,升级 PHP 后,会覆盖掉之前更改过的 PHP 目录权限,所以还需要将下列目录权限再次修改一下。

chown root.caddy /var/lib/php/session
chown root.caddy /var/lib/php/wsdlcache
chown root.caddy /var/lib/php/opcache

当 MariaDB 有新版本需要升级时,只需执行以下命令即可。

yum update -y MariaDB-*

当 Caddy 有新版本需要升级时,只需执行以下命令即可。

yum update -y caddy

安装了 phpmyAdmin 后,其配置信息如下图:

写在最后

请关注我的 Telegram 频道:https://t.me/qiushuiyibing
我会在此不定期发布一些杂七杂八的作品。
同时也欢迎加入交流群:https://t.me/qiushui2018

转载请注明:秋水逸冰 » LCMP (Linux + Caddy + MariaDB + PHP)

发表我的评论
取消评论

请输入正确答案后提交评论 *超出时限。 请再次填写验证码。

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (10)

  1. Oracle Linux 8.9下安装PHP 8.2及其组件后,命令行运行php时提示警告:php-sodium和php-pecl-zip这两个模块无法加载,尽管在/usr/lib64/php/modules下存在上述两个模块的so文件,是什么原因呢?
    lalala2个月前(02-14)回复
  2. 实在找不到地了,友情链接申请: https://renwole.com/ 任我乐 已经添加
    任我乐2个月前(02-14)回复
  3. 秋大,也出個 LNMP 吧,因爲 NGINX 用的人多,教學文檔也多,有問題可以更容易通過搜尋找到解決方式。
    QU5个月前(11-30)回复
  4. 秋大,要是服务器设置多个网站,怎么添加呢?
    Owen7个月前(09-15)回复
    • /etc/caddy/conf.d/ 下创建另外一个 conf 文件,比如 www.example2.com.conf,然后重启 Caddy 服务(systemctl restart caddy)即可。
      秋水逸冰7个月前(09-16)回复
      • 您好 秋大,这个我懂了,还有个问题,我在/data/www/default这里放了一个html正常访问,但是我新建的二级目录访问时显示的还是主页内容,例如,mywesite.com/new,显示mywesite.com的index.html内容,只有访问mywesite.com/new/index.html才能正常,这是怎么回事呢?
        Owen6个月前(10-11)回复
        • 这里需要对这个二级目录进行重写。添加如下配置后,重启 caddy 即可生效。 redir /new /new/ @new path_regexp branch ^/new/([^/]*)/?(.*)?$ handle @new { uri strip_prefix /new root * /data/www/default/new try_files {path} /{re.branch.1}/index.html file_server browse } 具体参考:https://github.com/caddyserver/caddy/issues/3504
          秋水逸冰6个月前(10-28)回复
  5. 大佬 有没有适用于Ubuntu的脚本
    Bravo11个月前(06-04)回复
    • 当然也有,只不过我不用 Ubuntu,所以未经验证,就没写出来。
      秋水逸冰11个月前(06-04)回复
    • 现在可以在 Ubuntu 下使用脚本一键安装了。 https://github.com/teddysun/lcmp
      Teddysun5个月前(11-18)回复