
博主之前一直使用军哥的LNMP脚本部署环境,但从去年开始(也就是25年)发现,总是装不上mariadb和PHP,论坛也看到类似的求助,但军哥好像不维护了,原因未知……
最后博主决定自己部署安装,也是源码安装的,博主声明,下文所有操作均为实际操作总结得出,如遇到问题欢迎交流。
更新系统
dnf update -y启用EPEL仓库和PowerTools/CRB仓库
dnf install epel-release -y
dnf config-manager --set-enabled crb安装依赖
dnf install -y vim wget tar gzip make cmake gcc gcc-c++ \
autoconf automake libtool pkgconfig \
openssl-devel pcre2-devel zlib-devel \
libxml2-devel libxslt-devel gd-devel \
libwebp-devel libjpeg-turbo-devel libpng-devel \
freetype-devel sqlite-devel oniguruma-devel \
bzip2-devel readline-devel libicu-devel \
ncurses-devel libedit-devel libzip-devel \
curl-devel libevent-devel \
systemd-devel pam-devel libsodium libsodium-devel php-pear php-devel创建安装目录
mkdir -p /usr/local/{php,mariadb,nginx}创建nginx用户和mariadb用户
useradd -r -s /sbin/nologin nginx
useradd -r -s /sbin/nologin mysql下载并解压Mariadb,这里以12.1.2为例
wget https://dlm.mariadb.com/4509471/MariaDB/mariadb-12.1.2/source/mariadb-12.1.2.tar.gz && \
tar xvf mariadb-12.1.2.tar.gz && cd mariadb-12.1.2配置编译参数
cd mariadb-11.8.5
cmake . \
-DCMAKE_INSTALL_PREFIX=/usr/local/mariadb \
-DMYSQL_DATADIR=/usr/local/mariadb/data \
-DSYSCONFDIR=/etc \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_ZLIB=system \
-DWITH_LIBWRAP=0 \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock编译安装
make -j$(nproc) && make install初始化数据库
/usr/local/mariadb/scripts/mysql_install_db \
--user=mysql \
--basedir=/usr/local/mariadb \
--datadir=/usr/local/mariadb/data目录权限配置
chown -R mysql:mysql /usr/local/mariadb/data
chmod 750 /usr/local/mariadb/data创建 my.conf 配置文件
tee /etc/my.cnf > /dev/null << 'EOF'
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
port = 3306
socket = /tmp/mysql.sock
basedir = /usr/local/mariadb
datadir = /usr/local/mariadb/data
pid-file = /usr/local/mariadb/data/mysql.pid
log-error = /usr/local/mariadb/data/mysql-error.log
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
[mysql]
default-character-set = utf8mb4
EOF创建 mariadb systemd服务
cat support-files/mariadb.service > /etc/systemd/system/mariadb.service启动 mariadb
systemctl daemon-reload && systemctl start mariadb && systemctl status mariadb加入开机启动
systemctl enable mariadb设置 root 用户密码
/usr/local/mariadb/bin/mysqladmin -u root password 'your_password_here'下载Nginx并解压,这里以1.26.3为例
wget https://nginx.org/download/nginx-1.26.3.tar.gz
tar xvf nginx-1.26.3.tar.gz && cd nginx-1.26.3编译配置
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_v3_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_sub_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module编译安装
make -j$(nproc) && make install创建 Nginx的systemctl 服务
tee /etc/systemd/system/nginx.service > /dev/null << 'EOF'
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOFnginx 主配置文件
tee /usr/local/nginx/conf/nginx.conf > /dev/null << 'EOF'
user nginx nginx;
worker_processes auto;
error_log /usr/local/nginx/logs/error.log;
pid /usr/local/nginx/logs/nginx.pid;
events {
worker_connections 1024;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /usr/local/nginx/logs/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
gzip on;
gzip_min_length 1k;
gzip_comp_level 2;
gzip_types text/plain text/css application/json application/javascript text/xml;
include /usr/local/nginx/conf/vhost/*.conf;
}
EOF创建虚拟主机目录
mkdir -p /usr/local/nginx/conf/vhost启动Nginx服务
systemctl daemon-reload && systemctl start nginx && systemctl status nginx加入开机启动
systemctl enable nginxfirewall放行http https
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload && firewall-cmd --list-all如果需要放行指定端口
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --add-port=443/tcp下载PHP并解压,这里以8.5.1为例
wget https://www.php.net/distributions/php-8.5.1.tar.gz && tar xvf php-8.5.1.tar.gz && cd php-8.5.1编译配置
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-config-file-scan-dir=/usr/local/php/etc/php.d \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv \
--with-zlib \
--enable-xml \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--enable-gd \
--with-webp \
--with-jpeg \
--with-freetype \
--enable-soap \
--enable-pcntl \
--enable-sockets \
--with-openssl \
--enable-intl \
--with-curl \
--with-zip \
--with-bz2 \
--with-gettext \
--with-mhash \
--with-pear \
--enable-zts=no \
--disable-rpath \
--with-sodium \
--with-libxml编译安装
make -j$(nproc) && make install复制并修改配置文件
cp php.ini-production /usr/local/php/etc/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
sed -i 's/;pid = run\/php-fpm.pid/pid = run\/php-fpm.pid/' /usr/local/php/etc/php-fpm.conf
sed -i 's/listen = 127.0.0.1:9000/listen = 127.0.0.1:9000/' /usr/local/php/etc/php-fpm.d/www.conf
sed -i 's/;listen.owner = nobody/listen.owner = nginx/' /usr/local/php/etc/php-fpm.d/www.conf
sed -i 's/;listen.group = nobody/listen.group = nginx/' /usr/local/php/etc/php-fpm.d/www.conf
sed -i 's/user = nobody/user = nginx/' /usr/local/php/etc/php-fpm.d/www.conf
sed -i 's/group = nobody/group = nginx/' /usr/local/php/etc/php-fpm.d/www.conf创建 system 服务 php-fpm
tee /etc/systemd/system/php-fpm.service > /dev/null << 'EOF'
[Unit]
Description=PHP FastCGI Process Manager
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/php/var/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF创建运行目录
mkdir -p /usr/local/php/var/run
chown -R nginx:nginx /usr/local/php/var启动服务
systemctl daemon-reload && systemctl start php-fpm && systemctl status php-fpm创建测试页面
tee /usr/local/nginx/conf/vhost/80.conf > /dev/null << 'EOF'
server {
listen 80;
server_name localhost;
root /usr/local/nginx/html;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
EOF重启Nginx服务
systemctl restart nginx创建 php info 页面
tee /usr/local/nginx/html/index.php > /dev/null << 'EOF'
<?php
phpinfo();
?>
EOF加入环境变量
echo 'export PATH=/usr/local/php/bin:/usr/local/php/sbin:/usr/local/mariadb/bin:/usr/local/nginx/sbin:$PATH' >> /etc/profile
source /etc/profile上述内容为博主实操验证所得,由于环境不同,人工操作易出错等原因,博主不保证100%成功,如遇到异常、错误、失败等情况,请核对操作是否有误,如实在找不到原因的可以提问腾讯元宝、豆包、kimi等智能助手,当然了,也欢迎和博主交流。