LCMP (Linux + Caddy + MariaDB + PHP)

技术 秋水逸冰 69048浏览 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 + Caddy2 + MariaDB + PHP) 环境。同时亦可使用本文提供的脚本,快速安装。

LCMP 一键安装脚本

Caddy 全部由 Teddysun Repository 提供。
MariaDB 由 MariaDB Repository 提供。
PHP 的 rpm 由 Remi Repository 提供,deb 由 deb.sury.org 提供。

Caddy 的 rpm 和 deb 全部由本人自行编译。理由如下:
1. 新增以下几个模块:

caddy.adapters.nginx
caddy.logging.encoders.formatted
caddy.logging.encoders.transform
dns.providers.cloudflare
http.handlers.forward_proxy
http.handlers.replace_response
http.handlers.webdav
http.reverse_proxy.transport.http_ntlm

2. 使用最新的 Go 1.25.7 来编译,以后也将跟随 Go 语言的最新版本来持续更新。

就是想简单搭建一个 Web 服务器,支持 MariaDB 和 PHP 而已,又对编译安装感到不耐烦?
没关系,来吧,LCMP (Linux + Caddy2 + MariaDB + PHP ) 一把梭,一般 5 – 10 分钟就能搞定环境搭建。

支持系统:
Enterprise Linux 8 / 9 / 10 (CentOS Stream, RHEL, Rocky Linux, AlmaLinux, Oracle Linux)
Debian 11 / 12 / 13
Ubuntu 20.04 / 22.04 / 24.04

支持架构:
x86_64 (amd64)
aarch64 (arm64)

详情:
https://lamp.sh/12.html
https://github.com/teddysun/lcmp

安装完成后浏览器打开的默认页:

使用上述 LCMP 一键安装脚本安装,则可以跳过下文第 1 至 5 的安装和设置过程,最后第 6 步的升级则需要注意。

1. 事前准备

禁用 SElinux

cat /etc/selinux/config

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

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

如果系统是 Enterprise Linux 9 / 10,则需要用如下方法禁用。

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-port=443/udp --zone=${default_zone}
firewall-cmd --permanent --add-masquerade
firewall-cmd --reload
firewall-cmd --list-all

2. 安装和设置 Caddy Web Server

执行以下命令安装 Teddysun Linux Repository:
适用于 Enterprise Linux 8 (CentOS Stream 8, RHEL 8, Rocky Linux 8, AlmaLinux 8, Oracle Linux 8) 的安装命令

dnf install -y https://dl.lamp.sh/linux/rhel/el8/x86_64/teddysun-release-1.0-1.el8.noarch.rpm

适用于 Enterprise Linux 9 (CentOS Stream 9, RHEL 9, Rocky Linux 9, AlmaLinux 9, Oracle Linux 9) 的安装命令

dnf install -y https://dl.lamp.sh/linux/rhel/el9/x86_64/teddysun-release-1.0-1.el9.noarch.rpm

适用于 Enterprise Linux 10 (CentOS Stream 10, RHEL 10, Rocky Linux 10, AlmaLinux 10, Oracle Linux 10) 的安装命令

dnf install -y https://dl.lamp.sh/linux/rhel/el10/x86_64/teddysun-release-1.0-1.el10.noarch.rpm

安装 Caddy,显示版本号

dnf makecache
dnf install -y caddy && caddy version

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

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

当前 MariaDB 的长期支持版本为 10.6, 10.11, 11.4, 11.8,选择 11.4 即可。

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

上述脚本执行完毕后,即引入 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

安装 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

接着安装 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,wsdlcache,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 有新版本需要升级时,只需执行以下命令即可。

dnf update -y php-*

需要注意的是,升级 PHP 后,会覆盖掉之前更改过的 PHP 目录权限,所以还需要将下列目录权限再次修改一下(该注意事项仅针对安装 rpm 包的系统)。

chown root:caddy /var/lib/php/{session,wsdlcache,opcache}

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

dnf update -y MariaDB-*

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

dnf 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年前(2024-02-14)回复
  2. 实在找不到地了,友情链接申请: https://renwole.com/ 任我乐 已经添加
    任我乐2年前(2024-02-14)回复
  3. 秋大,也出個 LNMP 吧,因爲 NGINX 用的人多,教學文檔也多,有問題可以更容易通過搜尋找到解決方式。
    QU2年前(2023-11-30)回复
  4. 秋大,要是服务器设置多个网站,怎么添加呢?
    Owen2年前(2023-09-15)回复
    • /etc/caddy/conf.d/ 下创建另外一个 conf 文件,比如 www.example2.com.conf,然后重启 Caddy 服务(systemctl restart caddy)即可。
      秋水逸冰2年前(2023-09-16)回复
      • 您好 秋大,这个我懂了,还有个问题,我在/data/www/default这里放了一个html正常访问,但是我新建的二级目录访问时显示的还是主页内容,例如,mywesite.com/new,显示mywesite.com的index.html内容,只有访问mywesite.com/new/index.html才能正常,这是怎么回事呢?
        Owen2年前(2023-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
          秋水逸冰2年前(2023-10-28)回复
  5. 大佬 有没有适用于Ubuntu的脚本
    Bravo3年前(2023-06-04)回复
    • 当然也有,只不过我不用 Ubuntu,所以未经验证,就没写出来。
      秋水逸冰3年前(2023-06-04)回复
    • 现在可以在 Ubuntu 下使用脚本一键安装了。 https://github.com/teddysun/lcmp
      Teddysun2年前(2023-11-18)回复