在 CentOS 上编译安装 LAMP(Linux、Apache、MySQL、PHP)环境,可以通过以下步骤实现,本文将详细介绍如何从源代码编译和安装这些组件,并提供相关配置和优化建议。
一、准备工作
1、系统版本:CentOS 7.6
2、服务器 IP 地址:192.168.80.10
3、客户端 IP 地址:WIN7 客户端,IP 地址为 192.168.80.2
4、下载压缩包:
[httpd2.4.43](https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd2.4.43.tar.gz)
[aPR1.7.0](https://mirror.bit.edu.cn/apache/apr/apr1.7.0.tar.gz)
[aprutil1.6.1](https://mirror.bit.edu.cn/apache/apr/aprutil1.6.1.tar.gz)
[pcre8.44](https://ftp.pcre.org/pub/pcre/pcre8.44.tar.gz)
[php7.2.31](https://www.php.net/distributions/php7.2.31.tar.gz)
[mysql5.7.30](https://downloads.mysql.com/archives/community/)
二、安装编译工具和开发包
yum install y epelrelease yum y install make gcc gccc++ openssl openssldevel expattdevel
三、编译安装依赖包
1. 编译安装 APR
tar xf apr1.7.0.tar.gz C /usr/local/src/ cd /usr/local/src/apr1.7.0/ ./configure prefix=/usr/local/apr make j 4 && make install && cd
2. 编译安装 APRUtil
tar xf aprutil1.6.1.tar.gz C /usr/local/src/ cd /usr/local/src/aprutil1.6.1/ ./configure prefix=/usr/local/aprutil withapr=/usr/local/apr/bin/apr1config make j 4 && make install && cd
3. 编译安装 PCRE
tar xf pcre8.44.tar.gz C /usr/local/src/ cd /usr/local/src/pcre8.44/ ./configure prefix=/usr/local/pcre make j 4 && make install && cd
四、编译安装 Apache
tar zxvf httpd2.4.43.tar.gz C /usr/local/src/ cd /usr/local/src/httpd2.4.43/ ./configure prefix=/usr/local/apache enableso enablerewrite enablessl withapr=/usr/local/apr withaprutil=/usr/local/aprutil withpcre=/usr/local/pcre enablemodules=most enablempmsshared=all withmpm=event make j 4 && make install && cd
生成启动脚本并启动服务
cp /usr/local/apache/bin/apachectl /etc/init.d/apachectl chmod +x /etc/init.d/apachectl vim /usr/lib/systemd/system/apache.service
插入以下内容:
[Unit] Description=apache After=network.target [Service] Type=forking ExecStart=/etc/init.d/apachectl start ExecReload=/etc/init.d/apachectl restart ExecStop=/etc/init.d/apachectl stop PrivateTmp=true [Install] WantedBy=multiuser.target
chmod 754 /usr/lib/systemd/system/apache.service systemctl start apache.service
修改 Apache 默认运行用户:
useradd M s /sbin/nologin apache vim /usr/local/apache/conf/httpd.conf 修改第166行,默认用户和默认组改为apache chown R apache:apache /usr/local/apache/ systemctl restart apache
五、安装配置 MySQL 数据库
useradd M s /sbin/nologin mysql tar xf mysql5.7.30el7x86_64.tar.gz mv mysql5.7.30el7x86_64 /usr/local/mysql mkdir p /data/mysql #创建存放数据的文件夹 touch /var/log/mysqld.log chown R mysql:mysql /usr/local/mysql/ chown R mysql:mysql /data/mysql/ chown R mysql:mysql /var/log/mysqld.log
创建 MySQL 配置文件:
vim /etc/my.cnf
添加以下内容:
[client] port = 3306 [mysqld] basedir = /usr/local/mysql datadir = /data/mysql socket = /tmp/mysql.sock user = mysql Disabling symboliclinks is recommended to prevent assorted security risks symboliclinks=0 logerror=/var/log/mysqld.log pidfile=/usr/local/mysql/data/mysqld.pid
初始化数据库并启动 MySQL:
cd /usr/local/mysql/bin ./mysqld initialize user=mysql basedir=/usr/local/mysql datadir=/data/mysql ./mysqld_safe user=mysql &
修改 root 密码:
mysqladmin u root password 'newpassword'
六、安装 PHP
tar zxvf php7.2.31.tar.gz C /usr/local/src/ cd /usr/local/src/php7.2.31/ ./configure prefix=/usr/local/php withopenssl withmysqli=mysqlnd withzlib enableinlineoptimization disableall enablembstring enablembregex withcurl enableftp enablembstring withmcrypt withgd withjpegdir=/usr withpngdir=/usr withfreetypedir=/usr withbz2 withgettext withxmlrpc withxml enablesoap enablesockets enablezip withapxs2 withcurl=yes withcurlinclude=yes withcurlshared=yes withcurlstreams=yes enablebcmath withpcreregex=yes withpcreinclude=yes withpcrelibrary=yes enableexif enableftp enablegd withjpegdir=yes withpngdir=yes withfreetypedir=yes withzlibdir=yes withbz2dir=yes withcurl=yes withcurlinclude=yes withcurlshared=yes withcurlstreams=yes enablebcmath withpcreregex=yes withpcreinclude=yes withpcrelibrary=yes enableexif enableftp enablegd withjpegdir=yes withpngdir=yes withfreetypedir=yes withzlibdir=yes withbz2dir=yes withcurl=yes withcurlinclude=yes withcurlshared=yes withcurlstreams=yes enablebcmath withpcreregex=yes withpcreinclude=yes withpcrelibrary=yes enableexif enableftp enablegd withjpegdir=yes withpngdir=yes withfreetypedir=yes withzlibdir=yes withbz2dir=yes withcurl=yes withcurlinclude=yes withcurlshared=yes withcurlstreams=yes enablebcmath withpcreregex=yes withpcreinclude=yes withpcrelibrary=yes enableexif enableftp enablegd withjpegdir=yes withpngdir=yes withfreetypedir=yes withzlibdir=yes withbz2dir=yes withcurl=yes withcurlinclude=yes withcurlshared=yes withcurlstreams=yes enablebcmath withpcreregex=yes withpcreinclude=yes withpcrelibrary=yes enableexif enableftp enablegd withjpegdir=yes withpngdir=yes withfreetypedir=yes withzlibdir=yes withbz2dir=yes withcurl=yes withcurlinclude=yes withcurlshared=yes withcurlstreams=yes enablebcmath withpcreregex=yes withpcreinclude=yes withpcrelibrary=yes enableexif enableftp enablegd withjpegdir=yes withpngdir=yes withfreetypedir=yes withzlibdir=yes withbz2dir=yes withcurl=yes withcurlinclude=yes withcurlshared=yes withcurlstreams=yes enablebcmath withpcreregex=yes withpcreinclude=yes withpcrelibrary=yes enableexif enableftp enablegd withjpegdir=yes withpngdir=yes withfreetypedir=yes withzlibdir=yes withbz2dir=yes withcurl=yes withcurlinclude=yes withcurlshared=yes withcurlstreams=yes enablebcmath withpcreregex=yes withpcreinclude=yes withpcrelibrary=yes enableexif enableftp enablegd withjpegdir=yes withpngdir=yes withfreetypedir=yes withzlibdir=yes withbz2dir=yes withcurl=yes withcurlinclude=yes withcurlshared=yes withcurlstreams=yes enablebcmath withpcreregex=yes withpcreinclude=yes withpcrelibrary=yes enableexif enableftp enablegd withjpegdir=yes withpngdir=yes withfreetypedir=yes withzlibdir=yes withbz2dir=yes withcurl=yes withcurlinclude=yes withcurlshared=yes withcurlstreams=yes enablebcmath withpcreregex=yes withpcreinclude=yes withpcrelibrary=yes enableexif enableftp enablegd withjpegdir=yes withpngdir=yes withfreetypedir=yes withzlibdir=yes withbz2dir=yeswithcurldir=yeswithcurlincludediryeswithcurlshareddiryeswithcurlstreamsdiryesenablebcmathwithpcreregexdiryeswithpcreincludediryeswithpcrelibrarydiryesenableexifenableftpenablegdwithjpegdiryeswithpngdiryeswithfreetypediryeswithzlibdiryeswithbz2diryeswithcurldiryeswithcurlincludediryeswithcurlshareddiryeswithcurlstreamsdiryesenablebcmathwithpcreregexdiryeswithpcreincludediryeswithpcrelibrarydiryesenableexifenableftpenablegdwithjpegdiryeswithpngdiryeswithfreetypediryeswithzlibdiryeswithbz2diryeswithcurldiryeswithcurlincludediryeswithcurlshareddiryeswithcurlstreamsdiryesenablebcmathwithpcreregexdiryeswithpcreincludediryeswithpcrelibrarydiryesenableexifenableftpenablegdwithjpegdiryeswithpngdiryeswithfreetypediryeswithzlibdiryeswithbz2diryeswithcurldiryeswithcurlincludediryeswithcurlshareddiryeswithcurlstreamsdiryesenablebcmathwithpcreregexdiryeswithpcreincludediryeswithpcrelibrarydiryesenableexifenableftp enablesockets enablezip # 指定其他模块和选项... make j 4 && make install && cd
七、配置 PHPMyAdmin
下载并解压 PHPMyAdmin:
wget https://files.phpmyadmin.net/phpMyAdmin/5.0.2/phpMyAdmin5.0.2alllanguages.tar.gz tar xzvf phpMyAdmin5.0.2alllanguages.tar.gz C /usr/local/phpMyAdmin/
配置虚拟主机:
vim /usr/local/apache/conf/extra/httpdvhosts.conf
添加以下内容:
<VirtualHost *:80> DocumentRoot "/usr/local/phpMyAdmin" ServerName pma.example.com <Directory "/usr/local/phpMyAdmin"> AllowOverride None Require all granted Options None AllowOverride All </Directory> ErrorLog "logs/pma_error_log" CustomLog "logs/pma_access_log" combined </VirtualHost>
启用虚拟主机配置:
vim /usr/local/apache/conf/httpd.conf
添加以下内容:
Include conf/extra/httpdvhosts.conf
重启 Apache:
systemctl restart apache
八、常见问题及解答(FAQs)
Q1:如何修改 PHP 的上传文件大小限制?
A1:编辑php.ini
文件,修改upload_max_filesize
和post_max_size
的值。
upload_max_filesize = 64M post_max_size = 64M
保存后重启 Apache 服务。
Q2:如何更改 MySQL 的数据存储目录?
A2:在my.cnf
文件中设置datadir
参数。
[mysqld] datadir = /new/path/to/datadirectory
然后重新启动 MySQL 服务。
Q3:如何启用 PHP 的扩展模块?
A3:编辑php.ini
文件,取消注释或添加相应的扩展模块。
extension=mysqli extension=gd
保存后重启 Apache 服务。