LNMP 手动部署指南
LNMP 手动部署完整指南
基于 LNMP 2.0 一键安装包逆向整理
所有配置文件和服务文件均可直接复制粘贴到终端创建
目录
- 系统兼容性矩阵
- 系统准备
- 创建用户和目录
- 编译安装依赖库
- 编译安装 Nginx
- 配置 Nginx
- 编译安装 MySQL/MariaDB
- 配置 MySQL/MariaDB
- 编译安装 PHP
- 配置 PHP-FPM
- 配置 php.ini
- 服务管理与开机自启
- MySQL 安全初始化
- 安装 phpMyAdmin
- 启动并验证
1. 系统兼容性矩阵
1.1 MySQL 版本与系统兼容性
源码编译MySQL 8.0需要 GCC 4.9+、内存≥8G
CentOS 7 建议使用二进制包安装MySQL 8.0(7.1节)
| MySQL 版本 | CentOS | Debian |
|---|---|---|
| MySQL 8.0 | 7 | 10, 11, 12 |
| MySQL 5.7 | 7 | 10, 11, 12 |
| MySQL 5.6 | 7 | 10, 11 |
| MySQL 5.5 | 7 | 10, 11 |
1.2 PHP 版本与系统兼容性
| PHP 版本 | CentOS | Debian |
|---|---|---|
| PHP 8.2 | 7 | 10, 11, 12 |
| PHP 8.1 | 7 | 10, 11, 12 |
| PHP 8.0 | 7 | 10, 11 |
| PHP 7.4 | 7 | 10, 11 |
| PHP 7.3 | 7 | 10, 11 |
| PHP 7.2 | 7 | 10, 11 |
| PHP 7.1 | 7 | 10, 11 |
| PHP 7.0 | 7 | 无 |
| PHP 5.6 | 7 | 无 |
| PHP 5.5 | 7 | 无 |
| PHP 5.4 | 7 | 无 |
| PHP 5.3 | 7 | 无 |
1.3 推荐组合
| 场景 | 推荐系统 | Nginx | MySQL | PHP |
|---|---|---|---|---|
| 新项目(推荐) | Debian 11/12 | 1.28.0 | 8.0.44 | 8.2.30 |
| 兼容旧项目 | Debian 10, CentOS 7 | 1.28.0 | 5.7.44 | 7.4.33 |
| 很老的系统 | CentOS 7 | 1.28.0 | 5.6.51 | 5.6.40 |
2. 系统准备
2.1 设置时区
rm -rf /etc/localtime
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime2.2 卸载系统自带 Apache/PHP/MySQL
Debian/Ubuntu:
apt-get update -y
for pkg in apache2 apache2-doc apache2-utils php5 php5-common php5-cgi php5-cli php5-mysql php5-curl php5-gd mysql-server mariadb-server; do apt-get purge -y $pkg; done
apt-get autoremove -y && apt-get cleanCentOS/RHEL:
yum -y remove httpd* php* mysql* mariadb*
yum clean all2.3 安装编译依赖
Debian/Ubuntu:
apt-get update -y
apt-get -fy install
export DEBIAN_FRONTEND=noninteractive
apt-get --no-install-recommends install -y build-essential gcc g++ make
for pkg in debian-keyring debian-archive-keyring cmake autoconf automake re2c wget cron bzip2 libzip-dev libc6-dev bison file flex m4 gawk less cpp binutils diffutils unzip tar bzip2 libbz2-dev libncurses5 libncurses5-dev libtool libevent-dev openssl libssl-dev zlibc libsasl2-dev libltdl3-dev libltdl-dev zlib1g zlib1g-dev libbz2-dev libglib2.0-0 libglib2.0-dev libpng3 libjpeg-dev libpng-dev libkrb5-dev curl libcurl4-openssl-dev libpcre3-dev libpq-dev gettext libxml2-dev libcap-dev ca-certificates libc-client2007e-dev psmisc patch git libc-ares-dev libicu-dev e2fsprogs libxslt1.1 libxslt1-dev libc-client-dev xz-utils libexpat1-dev libaio-dev libtirpc-dev libsqlite3-dev libonig-dev lsof pkg-config libtinfo-dev libnuma-dev libwebp-dev gnutls-dev iproute2 gzip libsodium-dev; do
apt-get --no-install-recommends install -y $pkg
doneCentOS/RHEL:
⚠️ CentOS 7 的默认 yum 源缺少oniguruma-devel,需要先安装 EPEL 源,否则编译 PHP 7.4+ 时会报Package 'oniguruma' not found。
# CentOS 7 需要先启用 EPEL 源(其他版本跳过此步)
yum -y install epel-release
for pkg in make cmake gcc gcc-c++ kernel-headers glibc-headers flex bison file libtool autoconf patch wget crontabs libjpeg-turbo-devel libpng libpng-devel gd gd-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel unzip tar bzip2 bzip2-devel libzip-devel libevent libevent-devel ncurses ncurses-devel curl curl-devel libcurl libcurl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel openssl openssl-devel pcre-devel gettext gettext-devel ncurses-devel gmp-devel unzip libcap diffutils ca-certificates net-tools libc-client-devel psmisc libXpm-devel git-core c-ares-devel libicu-devel libxslt libxslt-devel xz expat-devel libaio-devel libtirpc-devel perl cyrus-sasl-devel sqlite-devel oniguruma-devel lsof re2c pkg-config libarchive hostname numactl-devel libwebp-devel gnutls-devel initscripts iproute; do
yum -y install $pkg
done2.4 系统优化
# 库搜索路径
cat >> /etc/ld.so.conf << 'EOF'
/lib
/usr/lib
/usr/lib64
/usr/local/lib
EOF
ldconfig
# 64位系统符号链接
[ -d /usr/lib/x86_64-linux-gnu ] && ln -sf /usr/lib/x86_64-linux-gnu/libpng* /usr/lib/
[ -d /usr/lib/x86_64-linux-gnu ] && ln -sf /usr/lib/x86_64-linux-gnu/libjpeg* /usr/lib/
# 文件描述符限制
cat >> /etc/security/limits.conf << 'EOF'
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535
EOF
echo "fs.file-max=65535" >> /etc/sysctl.conf
# 禁用 SELinux(CentOS/RHEL)
if [ -s /etc/selinux/config ]; then
setenforce 0
sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
fi2.5 添加 Swap(内存 < 2GB 建议)
SWAP_TOTAL=$(awk '/SwapTotal/ {printf "%d", $2/1024}' /proc/meminfo)
if [ "$SWAP_TOTAL" -le 512 ] && [ ! -f /var/swapfile ]; then
dd if=/dev/zero of=/var/swapfile bs=1M count=2048
chmod 0600 /var/swapfile
mkswap /var/swapfile
swapon /var/swapfile
echo "/var/swapfile swap swap defaults 0 0" >> /etc/fstab
sysctl vm.swappiness=10
fi3. 创建用户和目录
groupadd www
useradd -s /sbin/nologin -g www www
groupadd mysql
useradd -s /sbin/nologin -M -g mysql mysql
mkdir -p /usr/local/nginx
mkdir -p /usr/local/mysql
mkdir -p /usr/local/mysql/var
mkdir -p /usr/local/php
mkdir -p /usr/local/php/{etc,conf.d}
mkdir -p /home/wwwroot/default
mkdir -p /home/wwwlogs
mkdir -p /usr/local/nginx/conf/vhost
mkdir -p /usr/local/nginx/conf/rewrite
chmod +w /home/wwwroot/default
chmod 777 /home/wwwlogs
chown -R www:www /home/wwwroot/default
mkdir -p /usr/local/src4. 编译安装依赖库
4.1 libiconv
cd /usr/local/src
wget https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.17.tar.gz
tar zxf libiconv-1.17.tar.gz
cd libiconv-1.17
./configure --enable-static
make -j$(nproc) && make install
cd ..
rm -rf libiconv-1.174.2 libmcrypt
cd /usr/local/src
wget https://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
tar zxf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure
make -j$(nproc) && make install
/sbin/ldconfig
cd libltdl/
./configure --enable-ltdl-install
make -j$(nproc) && make install
cd ../..
rm -rf libmcrypt-2.5.8
ln -sf /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -sf /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -sf /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -sf /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ldconfig4.3 mhash
cd /usr/local/src
wget https://sourceforge.net/projects/mhash/files/mhash/0.9.9.9/mhash-0.9.9.9.tar.bz2
tar jxf mhash-0.9.9.9.tar.bz2
cd mhash-0.9.9.9
./configure
make -j$(nproc) && make install
cd ..
rm -rf mhash-0.9.9.9
ln -sf /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -sf /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -sf /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -sf /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -sf /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
ldconfig4.4 mcrypt
如果你需要编译 PHP 5.x(5.2-5.6)并启用 mcrypt:
CentOS 6/7:
yum -y install libmcrypt-develDebian 10
apt -y install libmcrypt-dev4.5 FreeType
cd /usr/local/src
wget https://sourceforge.net/projects/freetype/files/freetype2/2.13.0/freetype-2.13.0.tar.xz
tar Jxf freetype-2.13.0.tar.xz
cd freetype-2.13.0
./configure --prefix=/usr/local/freetype --enable-freetype-config
make -j$(nproc) && make install
[ -d /usr/lib/pkgconfig ] && cp /usr/local/freetype/lib/pkgconfig/freetype2.pc /usr/lib/pkgconfig/
cat > /etc/ld.so.conf.d/freetype.conf << 'EOF'
/usr/local/freetype/lib
EOF
ldconfig
ln -sf /usr/local/freetype/include/freetype2/* /usr/include/
cd ..
rm -rf freetype-2.13.04.6 PCRE(不安装,保留源码给 Nginx 编译用)
cd /usr/local/src
wget https://sourceforge.net/projects/pcre/files/pcre/8.45/pcre-8.45.tar.bz2
tar jxf pcre-8.45.tar.bz24.7 libzip(CentOS 7 编译 PHP 7.4+ 需要)
CentOS 7 自带 libzip 0.10.1,PHP 7.4+ 要求 libzip ≥ 0.11,需手动编译新版。
Rocky Linux 8+、Ubuntu 18.04+ 等系统自带的 libzip 版本足够,可跳过此步。
使用 libzip 1.3.2 +./configure编译,无需 cmake3,兼容 CentOS 7 环境。
cd /usr/local/src
wget https://libzip.org/download/libzip-1.3.2.tar.gz
tar zxf libzip-1.3.2.tar.gz
cd libzip-1.3.2
./configure
make -j$(nproc) && make install
# 更新库和 pkg-config 路径
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH
ldconfig
cd /usr/local/src
rm -rf libzip-1.3.25. 编译安装 Nginx
5.1 编译(所有版本通用)
cd /usr/local/src
wget https://nginx.org/download/nginx-1.28.0.tar.gz
tar zxf nginx-1.28.0.tar.gz
cd nginx-1.28.0
./configure --user=www --group=www \
--prefix=/usr/local/nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_gzip_static_module \
--with-http_sub_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-http_realip_module \
--with-pcre=/usr/local/src/pcre-8.45 \
--with-pcre-jit
make -j$(nproc) && make install
cd ..
ln -sf /usr/local/nginx/sbin/nginx /usr/bin/nginx5.2 创建 Nginx 服务管理脚本
同时创建 init.d 和 systemd 服务文件,兼容 CentOS 6(SysVinit)和 CentOS 7+/Ubuntu 16.04+/Debian 8+(systemd)。
init.d 脚本(SysVinit / CentOS 6):
cat > /etc/init.d/nginx << 'EOF'
#! /bin/sh
# chkconfig: 2345 55 25
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
### END INIT INFO
NGINX_BIN='/usr/local/nginx/sbin/nginx'
CONFIG='/usr/local/nginx/conf/nginx.conf'
case "$1" in
start)
echo -n "Starting nginx... "
PID=$(ps -ef | grep "$NGINX_BIN" | grep -v grep | awk '{print $2}')
if [ "$PID" != "" ]; then
echo "nginx (pid $PID) already running."
exit 1
fi
$NGINX_BIN -c $CONFIG
if [ "$?" != 0 ]; then
echo " failed"
exit 1
else
echo " done"
fi
;;
stop)
echo -n "Stopping nginx... "
PID=$(ps -ef | grep "$NGINX_BIN" | grep -v grep | awk '{print $2}')
if [ "$PID" = "" ]; then
echo "nginx is not running."
exit 1
fi
$NGINX_BIN -s stop
if [ "$?" != 0 ]; then
echo " failed. Use force-quit"
$0 force-quit
else
echo " done"
fi
;;
status)
PID=$(ps -ef | grep "$NGINX_BIN" | grep -v grep | awk '{print $2}')
if [ "$PID" != "" ]; then
echo "nginx (pid $PID) is running..."
else
echo "nginx is stopped."
exit 0
fi
;;
force-quit|kill)
echo -n "Terminating nginx... "
PID=$(ps -ef | grep "$NGINX_BIN" | grep -v grep | awk '{print $2}')
if [ "$PID" = "" ]; then
echo "nginx is not running."
exit 1
fi
kill $PID
if [ "$?" != 0 ]; then
echo " failed"
exit 1
else
echo " done"
fi
;;
restart)
$0 stop
sleep 1
$0 start
;;
reload)
echo -n "Reload nginx... "
PID=$(ps -ef | grep "$NGINX_BIN" | grep -v grep | awk '{print $2}')
if [ "$PID" != "" ]; then
$NGINX_BIN -s reload
echo " done"
else
echo "nginx is not running, can't reload."
exit 1
fi
;;
configtest)
echo -n "Test nginx configure files... "
$NGINX_BIN -t
;;
*)
echo "Usage: $0 {start|stop|restart|reload|status|configtest|force-quit|kill}"
exit 1
;;
esac
EOF
chmod +x /etc/init.d/nginxsystemd 服务文件(CentOS 7+ / Ubuntu 16.04+ / Debian 8+):
cat > /etc/systemd/system/nginx.service << 'EOF'
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=false
[Install]
WantedBy=multi-user.target
EOF6. 配置 Nginx
6.1 主配置文件
cat > /usr/local/nginx/conf/nginx.conf << 'EOF'
user www www;
worker_processes auto;
worker_cpu_affinity auto;
error_log /home/wwwlogs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
multi_accept off;
accept_mutex off;
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;
sendfile on;
sendfile_max_chunk 512k;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";
server_tokens off;
access_log off;
server
{
listen 80 default_server;
#listen [::]:80 default_server ipv6only=on;
server_name _;
index index.html index.htm index.php;
root /home/wwwroot/default;
#error_page 404 /404.html;
include enable-php.conf;
location /nginx_status
{
stub_status on;
access_log off;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /.well-known {
allow all;
}
location ~ /\.
{
deny all;
}
access_log /home/wwwlogs/access.log;
}
include vhost/*.conf;
}
EOF6.2 PHP FastCGI 配置
cat > /usr/local/nginx/conf/enable-php.conf << 'EOF'
location ~ [^/]\.php(/|$)
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
EOF6.3 PHP PathInfo 配置(PathInfo 模式需要)
cat > /usr/local/nginx/conf/enable-php-pathinfo.conf << 'EOF'
location ~ [^/]\.php(/|$)
{
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
include pathinfo.conf;
}
EOF6.4 PathInfo 解析配置
cat > /usr/local/nginx/conf/pathinfo.conf << 'EOF'
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
try_files $fastcgi_script_name =404;
EOF6.5 fastcgi.conf 追加 open_basedir 安全限制
cat >> /usr/local/nginx/conf/fastcgi.conf << 'EOF'
fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root/:/tmp/:/proc/";
EOF6.6 网站根目录 .user.ini 安全文件
cat > /home/wwwroot/default/.user.ini << 'EOF'
open_basedir=/home/wwwroot/default:/tmp/:/proc/
EOF
chmod 644 /home/wwwroot/default/.user.ini
chattr +i /home/wwwroot/default/.user.ini6.7 常用 Rewrite 规则
cat > /usr/local/nginx/conf/rewrite/wordpress.conf << 'EOF'
location / {
try_files $uri $uri/ /index.php?$args;
}
EOF
cat > /usr/local/nginx/conf/rewrite/laravel.conf << 'EOF'
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
EOF
cat > /usr/local/nginx/conf/rewrite/thinkphp.conf << 'EOF'
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
}
EOF
cat > /usr/local/nginx/conf/rewrite/discuzx.conf << 'EOF'
rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;
rewrite ^([^\.]*)/article-([0-9]+)-([0-9]+)\.html$ $1/portal.php?mod=view&aid=$2&page=$3 last;
rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;
rewrite ^([^\.]*)/blog-([0-9]+)-([0-9]+)\.html$ $1/home.php?mod=space&uid=$2&do=blog&id=$3 last;
rewrite ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/index.php?action=$2&value=$3 last;
rewrite ^([^\.]*)/([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$ $1/plugin.php?id=$2:$3 last;
if (!-e $request_filename) {
return 404;
}
EOF7. 编译安装 MySQL/MariaDB
根据需要的版本选择对应章节操作。
源码编译MySQL 8.0需要 GCC 4.9+、内存≥8G
CentOS 7 建议使用二进制包安装MySQL 8.0(7.1节)
7.1 MySQL 8.0.44(二进制包安装,推荐省时)
cd /usr/local/src
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.44-linux-glibc2.17-x86_64.tar.xz
tar Jxf mysql-8.0.44-linux-glibc2.17-x86_64.tar.xz
mkdir -p /usr/local/mysql
mv mysql-8.0.44-linux-glibc2.17-x86_64/* /usr/local/mysql/7.2 MySQL 8.0.44(源码编译)
cd /usr/local/src
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.44.tar.gz
wget https://sourceforge.net/projects/boost/files/boost/1.77.0/boost_1_77_0.tar.bz2
tar jxf boost_1_77_0.tar.bz2
tar zxf mysql-8.0.44.tar.gz
cd mysql-8.0.44
mkdir build && cd build
cmake .. \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DSYSCONFDIR=/etc \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_BOOST=/usr/local/src/boost_1_77_0
make -j$(nproc) && make install
cd /usr/local/src7.3 MySQL 5.7.44(源码编译)
cd /usr/local/src
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.44.tar.gz
wget https://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
tar zxf boost_1_59_0.tar.gz
tar zxf mysql-5.7.44.tar.gz
cd mysql-5.7.44
# OpenSSL 3.0 系统需先编译 OpenSSL 1.1.1(见 7.8 节)
# 然后添加参数: -DWITH_SSL=/usr/local/openssl1.1.1
mkdir build && cd build
cmake .. \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DSYSCONFDIR=/etc \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_BOOST=/usr/local/src/boost_1_59_0
make -j$(nproc) && make install
cd /usr/local/src7.4 MySQL 5.6.51(源码编译)
cd /usr/local/src
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.6.51.tar.gz
tar zxf mysql-5.6.51.tar.gz
cd mysql-5.6.51
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DSYSCONFDIR=/etc \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLED_LOCAL_INFILE=1
make -j$(nproc) && make install
cd /usr/local/src7.5 MySQL 5.5.62(源码编译)
cd /usr/local/src
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.5.62.tar.gz
tar zxf mysql-5.5.62.tar.gz
cd mysql-5.5.62
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DSYSCONFDIR=/etc \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DWITH_READLINE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLED_LOCAL_INFILE=1
make -j$(nproc) && make install
cd /usr/local/src7.6 MariaDB 10.6.25(源码编译)
cd /usr/local/src
wget https://archive.mariadb.org/mariadb-10.6.25/source/mariadb-10.6.25.tar.gz
tar zxf mariadb-10.6.25.tar.gz
cd mariadb-10.6.25
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mariadb \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DWITH_READLINE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLED_LOCAL_INFILE=1 \
-DWITHOUT_TOKUDB=1
make -j$(nproc) && make install
cd /usr/local/src
# 创建 MariaDB 用户
groupadd mariadb
useradd -s /sbin/nologin -M -g mariadb mariadb7.7 MySQL/MariaDB 通用初始化
MySQL 5.7 / 8.0 初始化:
chown -R mysql:mysql /usr/local/mysql
mkdir -p /usr/local/mysql/var
chown -R mysql:mysql /usr/local/mysql/var
/usr/local/mysql/bin/mysqld --initialize-insecure \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/var \
--user=mysql
chown -R mysql:mysql /usr/local/mysql/var
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
chmod 755 /etc/init.d/mysql
# 以下 systemd 服务文件仅 systemd 系统需要(CentOS 7+、Debian 8+、Ubuntu 16.04+),CentOS 6 跳过此步
cat > /etc/systemd/system/mysql.service << 'EOF'
[Unit]
Description=MySQL Community Server
After=network.target syslog.target
[Service]
Type=forking
ExecStart=/etc/init.d/mysql start
ExecStop=/etc/init.d/mysql stop
ExecReload=/etc/init.d/mysql reload
Restart=no
PrivateTmp=false
[Install]
WantedBy=multi-user.target
EOFMySQL 5.5 / 5.6 初始化:
chown -R mysql:mysql /usr/local/mysql
mkdir -p /usr/local/mysql/var
chown -R mysql:mysql /usr/local/mysql/var
/usr/local/mysql/scripts/mysql_install_db \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/var \
--user=mysql
chown -R mysql:mysql /usr/local/mysql/var
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
chmod 755 /etc/init.d/mysql
# 以下 systemd 服务文件仅 systemd 系统需要(CentOS 7+、Debian 8+、Ubuntu 16.04+),CentOS 6 跳过此步
cat > /etc/systemd/system/mysql.service << 'EOF'
[Unit]
Description=MySQL Community Server
After=network.target syslog.target
[Service]
Type=forking
ExecStart=/etc/init.d/mysql start
ExecStop=/etc/init.d/mysql stop
ExecReload=/etc/init.d/mysql reload
Restart=no
PrivateTmp=false
[Install]
WantedBy=multi-user.target
EOFMariaDB 初始化:
chown -R mariadb:mariadb /usr/local/mariadb
mkdir -p /usr/local/mariadb/var
chown -R mariadb:mariadb /usr/local/mariadb/var
/usr/local/mariadb/scripts/mysql_install_db \
--basedir=/usr/local/mariadb \
--datadir=/usr/local/mariadb/var \
--user=mariadb
chown -R mariadb:mariadb /usr/local/mariadb/var
cp /usr/local/mariadb/support-files/mysql.server /etc/init.d/mariadb
chmod 755 /etc/init.d/mariadb
# 以下 systemd 服务文件仅 systemd 系统需要(CentOS 7+、Debian 8+、Ubuntu 16.04+),CentOS 6 跳过此步
cat > /etc/systemd/system/mariadb.service << 'EOF'
[Unit]
Description=MariaDB Database Server
After=network.target
[Service]
Type=forking
ExecStart=/etc/init.d/mariadb start
ExecStop=/etc/init.d/mariadb stop
ExecReload=/etc/init.d/mariadb reload
Restart=no
PrivateTmp=false
[Install]
WantedBy=multi-user.target
EOF库路径配置(MySQL):
cat > /etc/ld.so.conf.d/mysql.conf << 'EOF'
/usr/local/mysql/lib
/usr/local/lib
EOF
ldconfig
ln -sf /usr/local/mysql/lib/mysql /usr/lib/mysql
ln -sf /usr/local/mysql/include/mysql /usr/include/mysql
ln -sf /usr/local/mysql/bin/mysql /usr/bin/mysql
ln -sf /usr/local/mysql/bin/mysqldump /usr/bin/mysqldump
ln -sf /usr/local/mysql/bin/myisamchk /usr/bin/myisamchk
ln -sf /usr/local/mysql/bin/mysqld_safe /usr/bin/mysqld_safe
ln -sf /usr/local/mysql/bin/mysqlcheck /usr/bin/mysqlcheck库路径配置(MariaDB):
cat > /etc/ld.so.conf.d/mariadb.conf << 'EOF'
/usr/local/mariadb/lib
/usr/local/lib
EOF
ldconfig
ln -sf /usr/local/mariadb/bin/mysql /usr/bin/mysql
ln -sf /usr/local/mariadb/bin/mysqldump /usr/bin/mysqldump
ln -sf /usr/local/mariadb/bin/myisamchk /usr/bin/myisamchk
ln -sf /usr/local/mariadb/bin/mysqld_safe /usr/bin/mysqld_safe
ln -sf /usr/local/mariadb/bin/mysqlcheck /usr/bin/mysqlcheck7.8 OpenSSL 1.1.1 编译(旧版 PHP/MySQL 在 OpenSSL 3.0 系统上需要)
如果系统是 OpenSSL 3.0(如 Ubuntu 22.04+、CentOS 9 Stream),编译 PHP ≤ 7.1 或 MySQL 5.6/5.7 时需要先编译旧版 OpenSSL。
cd /usr/local/src
wget https://www.openssl.org/source/openssl-1.1.1u.tar.gz
tar zxf openssl-1.1.1u.tar.gz
cd openssl-1.1.1u
./config --prefix=/usr/local/openssl1.1.1 --openssldir=/usr/local/openssl1.1.1 shared zlib
make -j$(nproc) && make install
echo "/usr/local/openssl1.1.1/lib" >> /etc/ld.so.conf.d/openssl.conf
ldconfig8. 配置 MySQL/MariaDB
8.1 MySQL 8.0 配置文件
cat > /etc/my.cnf << 'EOF'
[client]
#password = your_password
port = 3306
socket = /tmp/mysql.sock
[mysqld]
port = 3306
socket = /tmp/mysql.sock
datadir = /usr/local/mysql/var
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
thread_cache_size = 8
tmp_table_size = 16M
performance_schema_max_table_instances = 500
explicit_defaults_for_timestamp = true
#skip-networking
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
default_authentication_plugin = mysql_native_password
log-bin=mysql-bin
binlog_format=mixed
server-id = 1
binlog_expire_logs_seconds = 864000
early-plugin-load = ""
default_storage_engine = InnoDB
innodb_file_per_table = 1
innodb_data_home_dir = /usr/local/mysql/var
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /usr/local/mysql/var
innodb_buffer_pool_size = 16M
innodb_log_file_size = 5M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
EOF8.2 MySQL 5.7 配置文件
cat > /etc/my.cnf << 'EOF'
[client]
#password = your_password
port = 3306
socket = /tmp/mysql.sock
[mysqld]
port = 3306
socket = /tmp/mysql.sock
datadir = /usr/local/mysql/var
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
thread_cache_size = 8
query_cache_size = 8M
tmp_table_size = 16M
performance_schema_max_table_instances = 500
explicit_defaults_for_timestamp = true
#skip-networking
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
log-bin=mysql-bin
binlog_format=mixed
server-id = 1
expire_logs_days = 10
early-plugin-load = ""
default_storage_engine = InnoDB
innodb_file_per_table = 1
innodb_data_home_dir = /usr/local/mysql/var
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /usr/local/mysql/var
innodb_buffer_pool_size = 16M
innodb_log_file_size = 5M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
EOF8.3 MySQL 5.6 配置文件
cat > /etc/my.cnf << 'EOF'
[client]
#password = your_password
port = 3306
socket = /tmp/mysql.sock
[mysqld]
port = 3306
socket = /tmp/mysql.sock
datadir = /usr/local/mysql/var
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
thread_cache_size = 8
query_cache_size = 8M
tmp_table_size = 16M
performance_schema_max_table_instances = 500
explicit_defaults_for_timestamp = true
#skip-networking
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
log-bin=mysql-bin
binlog_format=mixed
server-id = 1
expire_logs_days = 10
default_storage_engine = InnoDB
innodb_file_per_table = 1
innodb_data_home_dir = /usr/local/mysql/var
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /usr/local/mysql/var
innodb_buffer_pool_size = 16M
innodb_log_file_size = 5M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
EOF8.4 MySQL 5.5 配置文件
cat > /etc/my.cnf << 'EOF'
[client]
#password = your_password
port = 3306
socket = /tmp/mysql.sock
[mysqld]
port = 3306
socket = /tmp/mysql.sock
datadir = /usr/local/mysql/var
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
thread_cache_size = 8
query_cache_size = 8M
tmp_table_size = 16M
performance_schema_max_table_instances = 500
#skip-networking
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
log-bin=mysql-bin
binlog_format=mixed
server-id = 1
expire_logs_days = 10
default_storage_engine = InnoDB
innodb_file_per_table = 1
innodb_data_home_dir = /usr/local/mysql/var
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /usr/local/mysql/var
innodb_buffer_pool_size = 16M
innodb_log_file_size = 5M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
EOF8.5 MariaDB 配置文件
cat > /etc/my.cnf << 'EOF'
[client]
#password = your_password
port = 3306
socket = /tmp/mysql.sock
[mysqld]
port = 3306
socket = /tmp/mysql.sock
user = mariadb
basedir = /usr/local/mariadb
datadir = /usr/local/mariadb/var
log_error = /usr/local/mariadb/var/mariadb.err
pid-file = /usr/local/mariadb/var/mariadb.pid
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
thread_cache_size = 8
query_cache_size = 8M
tmp_table_size = 16M
explicit_defaults_for_timestamp = true
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
log-bin=mysql-bin
binlog_format=mixed
server-id = 1
expire_logs_days = 10
default_storage_engine = InnoDB
innodb_file_per_table = 1
innodb_data_home_dir = /usr/local/mariadb/var
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /usr/local/mariadb/var
innodb_buffer_pool_size = 16M
innodb_log_file_size = 5M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
EOF8.6 根据服务器内存调整参数
注意: MySQL 8.0 移除了 query_cache_size,8.0 的配置中不要包含此参数。
修改
/etc/my.cnf中对应值:
1-2G 内存配置
key_buffer_size = 32M
table_open_cache = 128
sort_buffer_size = 768K
read_buffer_size = 768K
thread_cache_size = 16
query_cache_size = 16M
tmp_table_size = 32M
innodb_buffer_pool_size = 128M
innodb_log_file_size = 32M
performance_schema_max_table_instances = 10002-4G 内存配置
key_buffer_size = 64M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
thread_cache_size = 32
query_cache_size = 32M
tmp_table_size = 64M
innodb_buffer_pool_size = 256M
innodb_log_file_size = 64M
performance_schema_max_table_instances = 20004-8G 内存配置
key_buffer_size = 128M
table_open_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
thread_cache_size = 64
query_cache_size = 64M
tmp_table_size = 64M
innodb_buffer_pool_size = 512M
innodb_log_file_size = 128M
performance_schema_max_table_instances = 40008-16G 内存配置
key_buffer_size = 256M
table_open_cache = 1024
sort_buffer_size = 4M
read_buffer_size = 4M
thread_cache_size = 128
query_cache_size = 128M
tmp_table_size = 128M
innodb_buffer_pool_size = 1024M
innodb_log_file_size = 256M
performance_schema_max_table_instances = 600016-32G 内存配置
key_buffer_size = 512M
table_open_cache = 2048
sort_buffer_size = 8M
read_buffer_size = 8M
thread_cache_size = 256
query_cache_size = 256M
tmp_table_size = 256M
innodb_buffer_pool_size = 2048M
innodb_log_file_size = 512M
performance_schema_max_table_instances = 800032G+ 内存配置
key_buffer_size = 1024M
table_open_cache = 4096
sort_buffer_size = 16M
read_buffer_size = 16M
thread_cache_size = 512
query_cache_size = 512M
tmp_table_size = 512M
innodb_buffer_pool_size = 4096M
innodb_log_file_size = 1024M
performance_schema_max_table_instances = 100009. 编译安装 PHP
根据需要的版本选择对应章节操作。
注意 PHP 版本与系统兼容性,参见第 1 节兼容性矩阵。
9.1 PHP 8.2.30
cd /usr/local/src
wget https://www.php.net/distributions/php-8.2.30.tar.bz2
tar jxf php-8.2.30.tar.bz2
cd php-8.2.30
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-config-file-scan-dir=/usr/local/php/conf.d \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv=/usr/local \
--with-freetype=/usr/local/freetype \
--with-jpeg \
--with-zlib \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--enable-pcntl \
--enable-ftp \
--enable-gd \
--with-openssl \
--with-mhash \
--enable-sockets \
--with-zip \
--enable-soap \
--with-gettext \
--enable-opcache \
--with-xsl \
--with-pear \
--with-webp \
--enable-exif \
--with-sodium # CentOS 7 请去掉此行
make -j$(nproc) && make install
cd /usr/local/src9.2 PHP 8.1.34
cd /usr/local/src
wget https://www.php.net/distributions/php-8.1.34.tar.bz2
tar jxf php-8.1.34.tar.bz2
cd php-8.1.34
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-config-file-scan-dir=/usr/local/php/conf.d \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv=/usr/local \
--with-freetype=/usr/local/freetype \
--with-jpeg \
--with-zlib \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--enable-pcntl \
--enable-ftp \
--enable-gd \
--with-openssl \
--with-mhash \
--enable-sockets \
--with-zip \
--enable-soap \
--with-gettext \
--enable-opcache \
--with-xsl \
--with-pear \
--with-webp \
--enable-exif \
--with-sodium # CentOS 7/Debian 10 请去掉此行
make -j$(nproc) && make install
cd /usr/local/src9.3 PHP 8.0.30
cd /usr/local/src
wget https://www.php.net/distributions/php-8.0.30.tar.bz2
tar jxf php-8.0.30.tar.bz2
cd php-8.0.30
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-config-file-scan-dir=/usr/local/php/conf.d \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv=/usr/local \
--with-freetype=/usr/local/freetype \
--with-jpeg \
--with-zlib \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--enable-pcntl \
--enable-ftp \
--enable-gd \
--with-openssl \
--with-mhash \
--enable-sockets \
--with-zip \
--enable-soap \
--with-gettext \
--enable-opcache \
--with-xsl \
--with-pear \
--with-webp \
--enable-exif \
--with-sodium # CentOS 7/Debian 10 请去掉此行
# CentOS 7/Debian 10 使用以下命令编译安装
make ZEND_EXTRA_LIBS='-liconv' -j$(nproc) && make install
make -j$(nproc) && make install
cd /usr/local/src9.4 PHP 7.4.33
cd /usr/local/src
wget https://www.php.net/distributions/php-7.4.33.tar.bz2
tar jxf php-7.4.33.tar.bz2
cd php-7.4.33
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-config-file-scan-dir=/usr/local/php/conf.d \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir \
--with-freetype=/usr/local/freetype \
--with-jpeg \
--with-png \
--with-zlib \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--enable-pcntl \
--enable-ftp \
--enable-gd \
--with-openssl \
--with-mhash \
--enable-sockets \
--with-xmlrpc \
--with-zip \
--without-libzip \
--enable-soap \
--with-gettext \
--enable-opcache \
--with-xsl \
--with-pear \
--with-webp \
--enable-exif \
--with-sodium # CentOS 7/Debian 10 请去掉此行
# CentOS 7/Debian 10,11 使用以下命令编译安装
make ZEND_EXTRA_LIBS='-liconv' -j$(nproc) && make install
make -j$(nproc) && make install
cd /usr/local/src9.5 PHP 7.3.33
cd /usr/local/src
wget https://www.php.net/distributions/php-7.3.33.tar.bz2
tar jxf php-7.3.33.tar.bz2
cd php-7.3.33
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-config-file-scan-dir=/usr/local/php/conf.d \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir \
--with-freetype-dir=/usr/local/freetype \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--enable-pcntl \
--enable-ftp \
--with-gd \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--without-libzip \
--enable-soap \
--with-gettext \
--enable-opcache \
--with-xsl \
--with-pear \
--enable-exif \
--with-sodium # CentOS 7/Debian 10,11 请去掉此行
# CentOS 7/Debian 10,11 使用以下命令编译安装
make EXTRA_LIBS='-liconv -ldl -lm -lssl -lcrypto -lz -lresolv -lxslt -lexslt -lexpat -lxml2 -lmcrypt -lfreetype -lpng -ljpeg -lcurl -licui18n -licuuc -licudata -licuio -lstdc++' -j$(nproc) && make install
make -j$(nproc) && make install
cd /usr/local/src9.6 PHP 7.2.34
cd /usr/local/src
wget https://www.php.net/distributions/php-7.2.34.tar.bz2
tar jxf php-7.2.34.tar.bz2
cd php-7.2.34
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-config-file-scan-dir=/usr/local/php/conf.d \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir \
--with-freetype-dir=/usr/local/freetype \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--enable-pcntl \
--enable-ftp \
--with-gd \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--with-gettext \
--enable-opcache \
--with-xsl \
--enable-exif \
--with-sodium # CentOS 7/Debian 10,11 请去掉此行
# CentOS 7/Debian 10,11 使用以下命令编译安装
make EXTRA_LIBS='-liconv -ldl -lm -lssl -lcrypto -lz -lresolv -lxslt -lexslt -lexpat -lxml2 -lmcrypt -lfreetype -lpng -ljpeg -lcurl -licui18n -licuuc -licudata -licuio -lstdc++' -j$(nproc) && make install
make -j$(nproc) && make install
cd /usr/local/src9.7 PHP 7.1.33
cd /usr/local/src
wget https://www.php.net/distributions/php-7.1.33.tar.bz2
tar jxf php-7.1.33.tar.bz2
cd php-7.1.33
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-config-file-scan-dir=/usr/local/php/conf.d \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir \
--with-freetype-dir=/usr/local/freetype \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--enable-pcntl \
--with-mcrypt \
--enable-ftp \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--with-gettext \
--enable-opcache \
--with-xsl \
--enable-exif
# CentOS 7/Debian 10,11 使用以下命令编译安装
make EXTRA_LIBS='-liconv -ldl -lm -lssl -lcrypto -lz -lresolv -lxslt -lexslt -lexpat -lxml2 -lmcrypt -lfreetype -lpng -ljpeg -lcurl -licui18n -licuuc -licudata -licuio -lstdc++' -j$(nproc) && make install
make -j$(nproc) && make install
cd /usr/local/src9.8 PHP 7.0.33
cd /usr/local/src
wget https://www.php.net/distributions/php-7.0.33.tar.bz2
tar jxf php-7.0.33.tar.bz2
cd php-7.0.33
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-config-file-scan-dir=/usr/local/php/conf.d \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir \
--with-freetype-dir=/usr/local/freetype \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--enable-pcntl \
--with-mcrypt \
--enable-ftp \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--with-gettext \
--enable-opcache \
--with-xsl
# CentOS 7 使用以下命令编译安装
make EXTRA_LIBS='-liconv -ldl -lm -lssl -lcrypto -lz -lresolv -lxslt -lexslt -lexpat -lxml2 -lmcrypt -lfreetype -lpng -ljpeg -lcurl -licui18n -licuuc -licudata -licuio -lstdc++' -j$(nproc) && make install
make -j$(nproc) && make install
cd /usr/local/src9.9 PHP 5.6.40
cd /usr/local/src
wget https://www.php.net/distributions/php-5.6.40.tar.bz2
tar jxf php-5.6.40.tar.bz2
cd php-5.6.40
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-config-file-scan-dir=/usr/local/php/conf.d \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir \
--with-freetype-dir=/usr/local/freetype \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--with-mcrypt \
--enable-ftp \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--with-gettext \
--enable-opcache \
--enable-intl \
--with-xsl
# CentOS 7 使用以下命令编译安装
make EXTRA_LIBS='-liconv -ldl -lm -lssl -lcrypto -lz -lresolv -lxslt -lexslt -lexpat -lxml2 -lmcrypt -lfreetype -lpng -ljpeg -lcurl -licui18n -licuuc -licudata -licuio -lstdc++' -j$(nproc) && make install
make -j$(nproc) && make install
cd /usr/local/src9.10 PHP 5.5.38
cd /usr/local/src
wget https://www.php.net/distributions/php-5.5.38.tar.bz2
tar jxf php-5.5.38.tar.bz2
cd php-5.5.38
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-config-file-scan-dir=/usr/local/php/conf.d \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir \
--with-freetype-dir=/usr/local/freetype \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--with-mcrypt \
--enable-ftp \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--with-gettext \
--enable-opcache \
--enable-intl \
--with-xsl
# CentOS 7 使用以下命令编译安装
make EXTRA_LIBS='-liconv -ldl -lm -lssl -lcrypto -lz -lresolv -lxslt -lexslt -lexpat -lxml2 -lmcrypt -lfreetype -lpng -ljpeg -lcurl -licui18n -licuuc -licudata -licuio -lstdc++' -j$(nproc) && make install
make -j$(nproc) && make install
cd /usr/local/src9.11 PHP 5.4.45
cd /usr/local/src
wget https://www.php.net/distributions/php-5.4.45.tar.bz2
tar jxf php-5.4.45.tar.bz2
cd php-5.4.45
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-config-file-scan-dir=/usr/local/php/conf.d \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir \
--with-freetype-dir=/usr/local/freetype \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--with-mcrypt \
--enable-ftp \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--with-gettext \
--enable-intl \
--with-xsl
# CentOS 7 使用以下命令编译安装
make EXTRA_LIBS='-liconv -ldl -lm -lssl -lcrypto -lz -lresolv -lxslt -lexslt -lexpat -lxml2 -lmcrypt -lfreetype -lpng -ljpeg -lcurl -licui18n -licuuc -licudata -licuio -lstdc++' -j$(nproc) && make install
make -j$(nproc) && make install
cd /usr/local/src9.12 PHP 5.3.29
cd /usr/local/src
wget https://www.php.net/distributions/php-5.3.29.tar.bz2
tar jxf php-5.3.29.tar.bz2
cd php-5.3.29
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-config-file-scan-dir=/usr/local/php/conf.d \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir \
--with-freetype-dir=/usr/local/freetype \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-magic-quotes \
--enable-safe-mode \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--with-mcrypt \
--enable-ftp \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--with-gettext
# CentOS 7 使用以下命令编译安装
make EXTRA_LIBS='-liconv -ldl -lm -lssl -lcrypto -lz -lresolv -lxslt -lexslt -lexpat -lxml2 -lmcrypt -lfreetype -lpng -ljpeg -lcurl -licui18n -licuuc -licudata -licuio -lstdc++' -j$(nproc) && make install
make -j$(nproc) && make install
cd /usr/local/src9.13 PHP 编译通用后续步骤
ln -sf /usr/local/php/bin/php /usr/bin/php
ln -sf /usr/local/php/bin/phpize /usr/bin/phpize
ln -sf /usr/local/php/bin/pear /usr/bin/pear
ln -sf /usr/local/php/bin/pecl /usr/bin/pecl
ln -sf /usr/local/php/sbin/php-fpm /usr/bin/php-fpm
# 找到PHP源码目录(只找目录,排除压缩文件)
PHP_SRC=$(find /usr/local/src -maxdepth 1 -type d -name 'php-*' | grep -v '\.tar' | tail -1)
echo "PHP源码目录: $PHP_SRC"
cp ${PHP_SRC}/php.ini-production /usr/local/php/etc/php.ini
cp ${PHP_SRC}/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
# 以下 systemd 服务文件仅 systemd 系统需要(CentOS 7+、Debian 8+、Ubuntu 16.04+),CentOS 6 跳过此步
cat > /etc/systemd/system/php-fpm.service << 'EOF'
[Unit]
Description=The PHP FastCGI Process Manager
After=network.target
[Service]
Type=simple
PIDFile=/usr/local/php/var/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=false
[Install]
WantedBy=multi-user.target
EOF10. 配置 PHP-FPM
10.1 PHP-FPM 配置文件(所有 PHP 版本通用)
cat > /usr/local/php/etc/php-fpm.conf << 'EOF'
[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log
log_level = notice
[www]
listen = /tmp/php-cgi.sock
listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.max_children = 10
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 6
pm.max_requests = 1024
pm.process_idle_timeout = 10s
request_terminate_timeout = 100
request_slowlog_timeout = 0
slowlog = var/log/slow.log
EOF10.2 根据服务器内存调整 PHP-FPM 进程数
| 内存 | max_children | start_servers | min_spare_servers | max_spare_servers |
|---|---|---|---|---|
| ≤1G | 10 | 2 | 1 | 6 |
| 1-2G | 20 | 10 | 10 | 20 |
| 2-4G | 40 | 20 | 20 | 40 |
| 4-8G | 60 | 30 | 30 | 60 |
| 8G+ | 80 | 40 | 40 | 80 |
11. 配置 php.ini
sed -i 's/post_max_size =.*/post_max_size = 50M/' /usr/local/php/etc/php.ini
sed -i 's/upload_max_filesize =.*/upload_max_filesize = 50M/' /usr/local/php/etc/php.ini
sed -i 's/;date.timezone =.*/date.timezone = PRC/' /usr/local/php/etc/php.ini
sed -i 's/short_open_tag =.*/short_open_tag = On/' /usr/local/php/etc/php.ini
sed -i 's/;cgi.fix_pathinfo=.*/cgi.fix_pathinfo=0/' /usr/local/php/etc/php.ini
sed -i 's/max_execution_time =.*/max_execution_time = 300/' /usr/local/php/etc/php.ini
sed -i 's/disable_functions =.*/disable_functions = passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server/' /usr/local/php/etc/php.ini11.1 配置 pear/pecl
pear config-set php_ini /usr/local/php/etc/php.ini
pecl config-set php_ini /usr/local/php/etc/php.ini11.2 安装 Composer(可选)
wget -O /usr/local/bin/composer https://mirrors.aliyun.com/composer/composer.phar
chmod +x /usr/local/bin/composer12. 服务管理与开机自启
前面各组件安装步骤中已同时创建了 init.d 脚本和 systemd 服务文件。本节根据系统类型自动选择正确的方式启用开机自启。
- systemd 系统(CentOS 7+、Ubuntu 16.04+、Debian 8+):使用
systemctl enable- SysVinit 系统(CentOS 6):使用
chkconfig- Debian/Ubuntu 非 systemd:使用
update-rc.d
12.1 启用开机自启
systemd 系统(CentOS 7+、Ubuntu 16.04+、Debian 8+):
systemctl daemon-reload
systemctl enable nginx
systemctl enable php-fpm
# 使用 MySQL 时:
systemctl enable mysql
# 使用 MariaDB 时:
systemctl enable mariadbCentOS 6(SysVinit):
chkconfig --add nginx
chkconfig nginx on
chkconfig --add php-fpm
chkconfig php-fpm on
chkconfig --add mysql
chkconfig mysql on
# 使用 MariaDB 时改为:
chkconfig --add mariadb && chkconfig mariadb onDebian/Ubuntu 非 systemd:
update-rc.d -f nginx defaults
update-rc.d -f php-fpm defaults
update-rc.d -f mysql defaults
# 使用 MariaDB 时改为:
update-rc.d -f mariadb defaults12.2 服务管理命令
systemd 系统(CentOS 7+、Ubuntu 16.04+、Debian 8+):
# Nginx
systemctl start nginx
systemctl stop nginx
systemctl reload nginx
systemctl restart nginx
systemctl status nginx
# PHP-FPM
systemctl start php-fpm
systemctl stop php-fpm
systemctl restart php-fpm
systemctl reload php-fpm
systemctl status php-fpm
# MySQL
systemctl start mysql
systemctl stop mysql
systemctl restart mysql
systemctl reload mysql
systemctl status mysql
# MariaDB
systemctl start mariadb
systemctl stop mariadb
systemctl restart mariadb
systemctl status mariadbSysVinit 系统(CentOS 6 等):
# Nginx
/etc/init.d/nginx start
/etc/init.d/nginx stop
/etc/init.d/nginx reload
/etc/init.d/nginx restart
/etc/init.d/nginx status
# PHP-FPM
/etc/init.d/php-fpm start
/etc/init.d/php-fpm stop
/etc/init.d/php-fpm restart
/etc/init.d/php-fpm status
# MySQL
/etc/init.d/mysql start
/etc/init.d/mysql stop
/etc/init.d/mysql restart
/etc/init.d/mysql status
# MariaDB
/etc/init.d/mariadb start
/etc/init.d/mariadb stop
/etc/init.d/mariadb restart
/etc/init.d/mariadb status13. MySQL 安全初始化
# 启动 MySQL
/etc/init.d/mysql start
# 或 systemd 系统:
systemctl start mysql
sleep 2
# 设置 root 密码(请替换 YOUR_PASSWORD)
MYSQL_ROOT_PASSWORD="YOUR_PASSWORD"
/usr/local/mysql/bin/mysqladmin -u root password "${MYSQL_ROOT_PASSWORD}"
# 安全加固
/usr/local/mysql/bin/mysql -u root -p"${MYSQL_ROOT_PASSWORD}" -e "
DELETE FROM mysql.user WHERE User='';
DROP USER IF EXISTS ''@'%';
DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
DROP DATABASE IF EXISTS test;
FLUSH PRIVILEGES;
"
# 安全加固(5.5/5.6 不支持 DROP USER IF EXISTS,用 DELETE 替代)
/usr/local/mysql/bin/mysql -u root -p"${MYSQL_ROOT_PASSWORD}" -e "
DELETE FROM mysql.user WHERE User='';
DELETE FROM mysql.user WHERE User='' AND Host != 'localhost';
DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
DROP DATABASE IF EXISTS test;
FLUSH PRIVILEGES;
"
/etc/init.d/mysql restart
# 或 systemd 系统:
systemctl restart mysql14. 安装 phpMyAdmin
cd /usr/local/src
wget https://files.phpmyadmin.net/phpMyAdmin/5.2.3/phpMyAdmin-5.2.3-all-languages.tar.xz
tar Jxf phpMyAdmin-5.2.3-all-languages.tar.xz
mv phpMyAdmin-5.2.3-all-languages /home/wwwroot/default/phpmyadmin
BLOWFISH_SECRET=$(head -c 32 /dev/urandom | base64 | head -c 32)
cat > /home/wwwroot/default/phpmyadmin/config.inc.php << EOF
<?php
\$cfg['blowfish_secret'] = '${BLOWFISH_SECRET}';
\$i = 0;
\$i++;
\$cfg['Servers'][\$i]['auth_type'] = 'cookie';
\$cfg['Servers'][\$i]['host'] = 'localhost';
\$cfg['Servers'][\$i]['compress'] = false;
\$cfg['Servers'][\$i]['AllowNoPassword'] = false;
\$cfg['UploadDir'] = 'upload';
\$cfg['SaveDir'] = 'save';
?>
EOF
mkdir -p /home/wwwroot/default/phpmyadmin/{upload,save}
chmod 755 -R /home/wwwroot/default/phpmyadmin/
chown www:www -R /home/wwwroot/default/phpmyadmin/
cat > /home/wwwroot/default/phpinfo.php << 'EOF'
<?php
phpinfo();
?>
EOF
chown www:www /home/wwwroot/default/phpinfo.php15. 启动并验证
15.1 启动所有服务
/etc/init.d/nginx start
/etc/init.d/php-fpm start
/etc/init.d/mysql start 或 /etc/init.d/mariadb start
# systemd 系统:
systemctl start nginx
systemctl start php-fpm
systemctl start mysql 或 systemctl start mariadb15.2 检查状态
/etc/init.d/nginx status
/etc/init.d/php-fpm status
/etc/init.d/mysql status
# systemd 系统:
systemctl status nginx
systemctl status php-fpm
systemctl status mysql
ss -ntl | grep -E '80|3306'
php -v
nginx -t
curl -I http://localhost/phpinfo.php