源码编译安装LAMP架构并部署WordPress(CentOS 7)
本文档详细介绍通过源码编译方式在CentOS 7系统上安装LAMP(Linux+Apache+MySQL+PHP)架构的完整步骤,适用于需要自定义服务配置的场景。
一、系统初始化配置
1. 关闭防火墙和SELinux
[root@zhangyiwei-2 ~]# systemctl stop firewalld.service # 停止防火墙服务
[root@zhangyiwei-2 ~]# systemctl enable firewalld.service # 禁止防火墙开机自启
[root@zhangyiwei-2 ~]# setenforce 0 # 临时关闭SELinux
2. 配置yum网络源(阿里云 CentOS 7 yum 源配置文件)
[root@zhangyiwei-2 ~]# cd /etc/yum.repos.d/
[root@zhangyiwei-2 yum.repos.d]# ls
CentOS-Base.repo CentOS-Debuginfo.repo CentOS-Media.repo CentOS-Vault.repo
CentOS-CR.repo CentOS-fasttrack.repo CentOS-Sources.repo
[root@zhangyiwei-2 yum.repos.d]# rm -rf *
[root@zhangyiwei-2 yum.repos.d]# ls
[root@zhangyiwei-2 yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
--2025-08-19 15:13:39-- https://mirrors.aliyun.com/repo/Centos-7.repo
正在解析主机 mirrors.aliyun.com (mirrors.aliyun.com)... 171.43.201.243, 171.43.201.244, 171.43.201.237
正在连接 mirrors.aliyun.com (mirrors.aliyun.com)|171.43.201.243|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:2523 (2.5K) [application/octet-stream]
正在保存至: “/etc/yum.repos.d/CentOS-Base.repo”
100%[======================================>] 2,523 --.-K/s 用时 0s
2025-08-19 15:13:39 (1.02 GB/s) - 已保存 “/etc/yum.repos.d/CentOS-Base.repo” [2523/2523])
3.上传所需安装包到根目录下
二、源码编译安装Apache(httpd)
1. 安装依赖工具包
[root@zhangyiwei-2 yum.repos.d]# yum -y install bzip2 vim make wget openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ libxml2-devel
已加载插件:fastestmirror, langpacks
Determining fastest mirrors
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
***********************
完毕!
2. 安装APR(Apache可移植运行时库)
[root@zhangyiwei-2 ~]# ls # 所需安装包已经上传至服务器
anaconda-ks.cfg initial-setup-ks.cfg
apr-1.7.0.tar.gz mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
apr-util-1.6.1.tar.gz php-7.1.10.tar.bz2
httpd-2.4.54.tar.gz
[root@zhangyiwei-2 ~]# tar -xzf apr-1.7.0.tar.gz # 解压至任意目录
[root@zhangyiwei-2 ~]# ls
anaconda-ks.cfg httpd-2.4.54.tar.gz
apr-1.7.0 initial-setup-ks.cfg
apr-1.7.0.tar.gz mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
apr-util-1.6.1.tar.gz php-7.1.10.tar.bz2
[root@zhangyiwei-2 ~]# cd apr-1.7.0/
[root@zhangyiwei-2 apr-1.7.0]# ls
apr-config.in build-outputs.mk helpers misc strings
apr.dep CHANGES include mmap support
apr.dsp CMakeLists.txt libapr.dep network_io tables
apr.dsw config.layout libapr.dsp NOTICE test
apr.mak configure libapr.mak NWGNUmakefile threadproc
apr.pc.in configure.in libapr.rc passwd time
apr.spec docs LICENSE poll tools
atomic dso locks random user
build emacs-mode Makefile.in README
buildconf encoding Makefile.win README.cmake
build.conf file_io memory shmem
[root@zhangyiwei-2 apr-1.7.0]# sed -i '/$RM "$cfgfile"/d' configure # 修复编译错误
[root@zhangyiwei-2 apr-1.7.0]# ./configure --prefix=/usr/local/apr # 配置安装路径
****************
config.status: creating include/arch/unix/apr_private.h
config.status: executing libtool commands
config.status: executing default commands
[root@zhangyiwei-2 apr-1.7.0]# make # 编译
******************
-1 export_vars.c | sed -e 's/^\#[^!]*//' | sed -e '/^$/d' >> aprutil.exp
sed 's,^\(location=\).*$,\1installed,' < apu-1-config > apu-config.out
make[1]: 离开目录“/root/apr-util-1.6.1”
[root@zhangyiwei-2 apr-1.7.0]# make install # 编译安装
*******************
/usr/bin/install -c -m 644 build/apr_rules.out /usr/local/apr/build-1/apr_rules.mk
/usr/bin/install -c -m 755 apr-config.out /usr/local/apr/bin/apr-1-config
3. 安装APR-util(APR工具库)
[root@zhangyiwei-2 ~]# ls
anaconda-ks.cfg httpd-2.4.54.tar.gz
apr-1.7.0 initial-setup-ks.cfg
apr-1.7.0.tar.gz mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
apr-util-1.6.1 php-7.1.10.tar.bz2
apr-util-1.6.1.tar.gz
[root@zhangyiwei-2 ~]# tar -xzf apr-util-1.6.1.tar.gz
[root@zhangyiwei-2 ~]# cd apr-util-1.6.1/
[root@zhangyiwei-2 apr-util-1.6.1]#
[root@zhangyiwei-2 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr # 配置安装路径并关联APR
*********************
config.status: creating test/Makefile
config.status: creating include/private/apu_config.h
config.status: executing default commands
[root@zhangyiwei-2 apr-util-1.6.1]# make #编译
********************
-1 export_vars.c | sed -e 's/^\#[^!]*//' | sed -e '/^$/d' >> aprutil.exp
sed 's,^\(location=\).*$,\1installed,' < apu-1-config > apu-config.out
make[1]: 离开目录“/root/apr-util-1.6.1”
[root@zhangyiwei-2 apr-util-1.6.1]# make install #编译安装
***************************
----------------------------------------------------------------------
/usr/bin/install -c -m 644 aprutil.exp /usr/local/apr-util/lib
/usr/bin/install -c -m 755 apu-config.out /usr/local/apr-util/bin/apu-1-config
4. 编译安装httpd(Apache)
[root@zhangyiwei-2 apr-util-1.6.1]# cd
[root@zhangyiwei-2 ~]# tar -xzf httpd-2.4.54.tar.gz # 解压httpd源码包
[root@zhangyiwei-2 ~]# cd httpd-2.4.54/
[root@zhangyiwei-2 httpd-2.4.54]# ./configure --prefix=/usr/local/apache \ # 配置安装参数
> --sysconfdir=/etc/httpd24 \
> --enable-so \
> --enable-ssl \
> --enable-cgi \
> --enable-rewrite \
> --with-zlib \
> --with-pcre \
> --with-apr=/usr/local/apr \
> --with-apr-util=/usr/local/apr-util/ \
> --enable-modules=most \
> --enable-mpms-shared=all \
> --with-mpm=prefork
********************
LDFLAGS:
LIBS:
C preprocessor: gcc -E
[root@zhangyiwei-2 httpd-2.4.54]# make #编译
************
[root@zhangyiwei-2 httpd-2.4.54]# make install #编译安装
[root@zhangyiwei-2 httpd-2.4.54]# /usr/local/apache/bin/apachectl start # 启动Apache服务
三、源码编译安装MySQL(MariaDB)
1. 创建MySQL用户
[root@zhangyiwei-2 ~]# useradd -r -M -s /sbin/nologin mysql # 创建系统用户(无登录权限)
2. 安装依赖包
[root@zhangyiwei-2 ~]# yum -y install libncurses* # 安装MySQL依赖的终端处理库
3. 配置MySQL
[root@zhangyiwei-2 ~]# tar -xzf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/ # 解压并移动到安装目录
[root@zhangyiwei-2 ~]# ln -sv /usr/local/mysql-5.7.37-linux-glibc2.12-x86_64 /usr/local/mysql # 创建软链接
"/usr/local/mysql" -> "/usr/local/mysql-5.7.37-linux-glibc2.12-x86_64"
# 设置权限
[root@zhangyiwei-2 ~]# mchown -R mysql.mysql /usr/local/mysql # 设置权限
[root@zhangyiwei-2 ~]# mmkdir -p /opt/data # 创建数据存储目录
[root@zhangyiwei-2 ~]# mchown -R mysql.mysql /opt/data/ #将data目录的所有者所属组设为mysql
[root@zhangyiwei-2 ~]# yum -y install libaioyum -y install libaio # 安装I/O异步操作库
4. 初始化数据库
[root@zhangyiwei-2 ~]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data/ # 无密码初始化
*********
2025-08-19T08:08:16.780399Z 0 [Warning] CA certificate ca.pem is self signed.
2025-08-19T08:08:16.938081Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
[root@zhangyiwei-2 ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql # 头文件链接
[root@zhangyiwei-2 ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf # 配置库文件路径
[root@zhangyiwei-2 ~]# ldconfig # 刷新系统库缓存
5. 配置MySQL服务
# 编辑配置文件
[root@zhangyiwei-2 ~]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql # 安装目录
datadir = /opt/data # 数据目录
socket = /tmp/mysql.sock # 套接字文件
port = 3306 # 端口
pid-file = /opt/data/mysql.pid # PID文件
user = mysql # 运行用户
skip-name-resolve # 禁用域名解析
# 修改服务脚本
[root@zhangyiwei-2 ~]# sed -ri "s#^(basedir=).*#\1/usr/local/mysql#g" /usr/local/mysql/support-files/mysql.server
[root@zhangyiwei-2 ~]# sed -ri "s#^(datadir=).*#\1/opt/data#g" /usr/local/mysql/support-files/mysql.server
# 创建systemd服务单元
[root@zhangyiwei-2 ~]# vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=mysql server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
# 启动服务并设置开机自启
[root@zhangyiwei-2 ~]# systemctl daemon-reload
[root@zhangyiwei-2 ~]# systemctl enable --now mysqld
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service.
6. 设置数据库密码并配置环境变量
/usr/local/mysql/bin/mysql -uroot -e "set password=password('redhat')" # 设置root密码为redhat
或者
[root@zhangyiwei-2 ~]# /usr/local/mysql/bin/mysql -uroot -p
***********
mysql> set password=password('redhat')e
[root@zhangyiwei-2 ~]# export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
[root@zhangyiwei-2 ~]# source /etc/profile # 生效环境变量
[root@zhangyiwei-2 ~]# mysql -uroot -p #此时可以使用mysql命令
四、源码编译安装PHP
1. 安装PHP依赖包
[root@zhangyiwei-2 ~]# yum -y install \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel
2. 编译安装PHP
# 解压PHP源码包
[root@zhangyiwei-2 ~]# tar -xjf php-7.1.10.tar.bz2
[root@zhangyiwei-2 ~]# cd php-7.1.10/
# 配置编译参数
[root@zhangyiwei-2 php-7.1.10]# ./configure \
> --prefix=/usr/local/php \
> --with-apxs2=/usr/local/apache/bin/apxs \
> --with-mysql-sock=/tmp/mysql.sock \
> --with-mysqli \
> --with-zlib \
> --with-curl \
> --with-gd \
> --with-jpeg-dir \
> --with-png-dir \
> --with-freetype-dir \
> --with-openssl \
> --enable-mbstring \
> --enable-xml \
> --enable-session \
> --enable-ftp \
> --enable-pdo \
> --enable-tokenizer \
> --enable-zip
[root@zhangyiwei-2 ~]# make && make install # 编译并安装
3. 配置PHP
# 复制配置文件(开发环境用development,生产环境用production)
[root@zhangyiwei-2 ~]# [root@zhangyiwei-2 php-7.1.10]# cp /root/php-7.1.10/php.ini-development /usr/local/php/lib/php.ini
# 设置时区为上海
[root@zhangyiwei-2 ~]# sed -i 's/;date.timezone =/date\.timezone = \Asia\/Shanghai/' /usr/local/php/lib/php.ini
# 配置Apache支持PHP
[root@zhangyiwei-2 ~]# echo "AddType application/x-httpd-php .php" >> /etc/httpd24/httpd.conf # 关联PHP文件类型
[root@zhangyiwei-2 ~]# echo "AddType application/x-httpd-php-source .phps" >> /etc/httpd24/httpd.conf
[root@zhangyiwei-2 ~]# sed -i 's/index.html/index\.php index\.html/' /etc/httpd24/httpd.conf # 设置默认首页为PHP
# 创建PHP测试页
[root@zhangyiwei-2 ~]# rm -f /usr/local/apache/htdocs/index.html
[root@zhangyiwei-2 ~]# vim /usr/local/apache/htdocs/index.php
<?php
phpinfo();
?>
# 重启Apache服务
[root@zhangyiwei-2 ~]# /usr/local/apache/bin/apachectl stop
[root@zhangyiwei-2 ~]# /usr/local/apache/bin/apachectl start
五、验证LAMP架构
通过浏览器访问服务器IP(如http://192.168.100.10
),若显示PHP信息页面,说明LAMP架构编译安装成功,可继续部署WordPress。
@zhangyiwei-2 ~]# vim /usr/local/apache/htdocs/index.php
<?php phpinfo(); ?>重启Apache服务
[root@zhangyiwei-2 ~]# /usr/local/apache/bin/apachectl stop
[root@zhangyiwei-2 ~]# /usr/local/apache/bin/apachectl start
## 五、验证LAMP架构
通过浏览器访问服务器IP(如`http://192.168.100.10`),若显示PHP信息页面,说明LAMP架构编译安装成功,可继续部署WordPress。
