LAMP依赖环境
依赖包
httpd-2.4.37.tar.gz #Apache 主程序包
apr-1.6.2.tar.gz #Apache 依赖包
apr-util-1.6.0.tar.gz #Apache 依赖包
pcre-8.41.tar.gz #Apache 依赖包
php-7.1.24.tar #php 主程序包
mysql-5.7.19.tar.gz #mysql 主程序包
下载地址
下载源码安装包
https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.43.tar.gz
https://mirror.bit.edu.cn/apache/apr/apr-1.7.0.tar.gz
https://mirror.bit.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
https://ftp.pcre.org/pub/pcre/pcre-8.44.tar.gz
准备环境
[root@SR ~]# yum -y install make gcc gcc-c++ openssl openssl-devel expatdevel # 安装依赖环境
[root@SR ~]# systemctl stop firewalld.service && setenforce 0 && iptables -F # 安全策略关闭
Apache编译安装
Apache依赖包安装
apr
[root@SR ~]# tar xf apr-1.7.0.tar.gz -C /usr/local/src/ # 目录解压到/usr/local/src/
[root@SR ~]# cd /usr/local/src/apr-1.7.0/
[root@SR apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@SR apr-1.7.0]# make -j 4 && make install # 使用4个线程进行编译
apr-util
root@SR ~]# tar -xvf apr-util-1.6.1.tar.gz -C /usr/local/src/
[root@SR ~]# cd /usr/local/src/apr-util-1.6.1/
[root@SR apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/bin/apr-1-config
[root@SR apr-util-1.6.1]# make -j 4 && make install
# 注意:如果执行 make 报错:
xml/apr_xml.c:35:19: fatal error: expat.h: No such file or directory
# 解决:安装如下依赖包
[root@SR ~]# yum -y install expat-devel
prce
[root@SR ~]# tar -xvf pcre-8.44.tar.gz -C /usr/local/src/
[root@SR ~]# cd /usr/local/src/pcre-8.44/
[root@SR pcre-8.44]# ./configure --prefix=/usr/local/pcre
[root@SR pcre-8.44]# make -j 4 && make install
Apache编译安装
[root@SR ~]# tar -zxvf httpd-2.4.43.tar.gz -C /usr/local/src/
[root@SR ~]# cd /usr/local/src/httpd-2.4.43/
[root@SR httpd-2.4.37]# ./configure --prefix=/usr/local/apache --enable-so --enable-rewrite --enable-ssl --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre --enable-modules=most --enable-mpms-shared=all --with-mpm=event
[root@SR ~]# make -j 4 && make install
make报错解决办法
'''
如果make报如下错误
/usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_StopParser'
/usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_Parse'
/usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_ErrorString'
/usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_SetElementHandler'
collect2: error: ld returned 1 exit status
make[2]: *** [htpasswd] 错误 1
make[2]: 离开目录“/usr/local/src/httpd-2.4.28/support”
make[1]: *** [all-recursive] 错误 1
make[1]: 离开目录“/usr/local/src/httpd-2.4.28/support”
make: *** [all-recursive] 错误 1
'''
解决办法
编译参数说明
--prefix=/usr/local/apache |
#安装路径 |
--enable-so |
支持动态加载模块 |
--enable-rewrite |
支持网站地址重写 |
--enable-ssl |
支持 SSL 加密 |
--with-pcre=/usr/local/pcre |
pcre 路径 |
--with-apr=/usr/local/apr |
apr 路径 |
--with-apr-util=/usr/local/apr-util |
apr-util 路径 |
--with-mpm=event |
三种稳定的多进程处理模块 它们分别是 prefork, worker 和 event |
Apache启动脚本
[root@SR ~]# cp /usr/local/apache/bin/apachectl /etc/init.d/
[root@SR ~]# chmod +x /etc/init.d/apachectl # 配置执行权限
Apache服务脚本
[root@SR ~]# 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=multi-user.target
[root@SR ~]# chmod +754 /usr/lib/systemd/system/apache.service
启动服务
[root@SR ~]# systemctl enable apache
[root@SR ~]# systemctl start apache.service
设置Apache专有用户权限
[root@SR ~]# chown -R apache:apache /usr/local/apache/
[root@SR conf]# ll /usr/local/apache/
总用量 36
drwxr-xr-x. 2 apache apache 262 7月 31 09:06 bin
drwxr-xr-x. 2 apache apache 167 7月 31 09:06 build
drwxr-xr-x. 2 apache apache 78 7月 31 09:06 cgi-bin
drwxr-xr-x. 4 apache apache 106 7月 31 09:14 conf
drwxr-xr-x. 3 apache apache 4096 7月 31 09:06 error
drwxr-sr-x. 2 apache apache 24 3月 26 22:46 htdocs
drwxr-xr-x. 3 apache apache 8192 7月 31 09:06 icons
drwxr-xr-x. 2 apache apache 4096 7月 31 09:06 include
drwxr-xr-x. 2 apache apache 58 7月 31 09:14 logs
drwxr-xr-x. 4 apache apache 30 7月 31 09:06 man
drwxr-sr-x. 14 apache apache 8192 3月 26 22:46 manual
drwxr-xr-x. 2 apache apache 4096 7月 31 09:06 modules
修改 Apache 的运行用户
# 默认是 daemon 用户运行的进程
[root@SR httpd-2.4.43]# ps -axu | grep apache
root 51472 0.0 0.0 77108 2400 ? Ss 09:08 0:00 /usr/local/apache/bin/httpd -k start
daemon 51476 0.0 0.1 368156 4200 ? Sl 09:08 0:00 /usr/local/apache/bin/httpd -k start
daemon 51477 0.1 0.1 368156 4228 ? Sl 09:08 0:00 /usr/local/apache/bin/httpd -k start
daemon 51478 0.0 0.1 368156 4240 ? Sl 09:08 0:00 /usr/local/apache/bin/httpd -k start
root 51562 0.0 0.0 112728 976 pts/2 S+ 09:08 0:00 grep --color=auto apache
[root@SR conf]# vim httpd.conf
修改
User daemon ----> User apache
修改
Group daemon ----> Group apache
[root@SR conf]# systemctl restart apache.service
[root@SR conf]# ps -axu | grep apache
root 51751 0.0 0.0 77108 2404 ? Ss 09:14 0:00 /usr/local/apache/bin/httpd -k start
apache 51753 0.0 0.1 368156 4244 ? Sl 09:14 0:00 /usr/local/apache/bin/httpd -k start
apache 51754 0.0 0.1 368156 4244 ? Sl 09:14 0:00 /usr/local/apache/bin/httpd -k start
apache 51756 0.0 0.1 368156 4236 ? Sl 09:14 0:00 /usr/local/apache/bin/httpd -k start
root 51850 0.0 0.0 112728 976 pts/2 S+ 09:16 0:00 grep --color=auto apache
![](https://img2020.cnblogs.com/blog/1725357/202007/1725357-20200731141732428-741952264.png)
MySQL编译安装
创建MySQL专属用户
[root@SR ~]# useradd -M -s /sbin/nologin mysql
解压
[root@SR ~]# tar xf mysql-5.7.30-el7-x86_64.tar.gz
[root@SR ~]# mv mysql-5.7.30-el7-x86_64 /usr/local/mysql # 移动并且重命名
创建数据目录与日志
[root@SR ~]# mkdir -p /data/mysql
[root@SR ~]# touch /var/log/mysql.log
修改权限为mysql用户
[root@SR ~]# chown -R mysql:mysql /usr/local/mysql/
[root@SR ~]# chown -R mysql:mysql /data/mysql/
[root@SR ~]# chown -R mysql:mysql /var/log/mysqld.log
创建MySQL配置文件
[root@SR ~]# vim /etc/my.cnf
[client]
port = 3306
socket = /data/mysql/mysql.sock
[mysqld]
port = 3306
user = mysql
socket = /data/mysql/mysql.sock
basedir = /usr/local/mysql
datadir = /data/mysql
log-error=/var/log/mysqld.log
添加环境变量
[root@SR ~]# vim /etc/profile.d/mysql.sh
MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/bin
[root@SR mysql]# source /etc/profile # 文件生效
数据库初始化
[root@SR ~]# cd /usr/local/mysql/
[root@SR mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql # 数据库初始化
[root@SR mysql]# cat /var/log/mysqld.log |grep password # 查看临时密码
k_i;HR*Lu3sp # 临时密码
配置开机启动
[root@SR mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@SR mysql]# chmod 755 /etc/init.d/mysqld
[root@SR mysql]# chkconfig --add mysqld # 添加mysql服务 控制运行级别
[root@SR mysql]# chkconfig --list mysqld # 查看所有服务
[root@SR mysql]# /etc/init.d/mysqld start
![](https://img2020.cnblogs.com/blog/1725357/202007/1725357-20200731150311595-853398811.png)
登录MySQL
[root@SR mysql]# mysql -uroot -p #输入临时密码进入
mysql> set password for root@localhost = password('root123'); # 修改密码
mysql> flush privileges; # 刷新权限
Query OK, 0 rows affected (0.00 sec)
PHP编译安装
添加依赖包
[root@SR ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo # 安装epel扩展源
[root@SR ~]# yum clean all
[root@SR ~]# yum makecache
[root@SR ~]# yum -y install php-mcrypt libmcrypt libmcrypt-devel autoconf freetype gd libmcrypt libpng libpng-devel libjpeg libxml2 libxml2-devel zlib curl curl-devel re2c php-pear # 安装依赖包
解压
[root@SR ~]# tar -xvf php-7.2.31.tar.gz -C /usr/local/src
编译安装
[root@SR ~]# cd /usr/local/src/php-7.2.31
[root@SR php-7.2.31]# ./configure --prefix=/usr/local/php/ --with-apxs2=/usr/local/apache/bin/apxs --enable-mbstring --with-curl --with-gd --enable-fpm --enable-mysqlnd --with-pdo-mysql=mysqlnd --with-config-file-path=/usr/local/php/etc/ --with-mysqli=mysqlnd --with-mysql-sock=/data/mysql/mysql.sock --enable-maintainer-zts
[root@xuegod63 ~]# make -j 4 && make install
参数解释
--with-apxs2 |
将 php 编译为 Apache 的一个模块进行使用 |
--enable-mbstring |
多字节字符串的支持 |
--with-curl |
支持 cURL |
--with-gd |
支持 gd库 |
--enable-fpm |
支持构建 fpm |
--enable-mysqlnd |
启用 mysqlnd |
--with-pdo-mysql |
支持 pdo: MySQL 支持 |
--with-config-file-path |
设置配置文件路径 |
--with-mysqli |
支持 MySQLi |
--with-mysql-sock |
关联 mysql 的 socket 文件 |
--enable-maintainer-zts |
如果 Apache 是 event 模型,那需要指定 zts |
生成配置文件
[root@SR php-7.2.31]# cp php.ini-production /usr/local/php/etc/php.ini
运行Apache支持php文件
[root@SR php-7.2.31]# vim /usr/local/apache/conf/httpd.conf
# 在393行以下添加如下代码
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
[root@SR php-7.2.31]# systemctl restart apache.service # 重启
页面测试
[root@SR php-7.2.31]# cd /usr/local/apache/htdocs/
[root@SR htdocs]# vim index.php
# 添加探针
<?php
phpinfo();
?>
![](https://img2020.cnblogs.com/blog/1725357/202007/1725357-20200731153802475-708498847.png)
连接MySQL
<?php
$mysqli = new mysqli("localhost", "root", " XueGod!@#123");
if(!$mysqli) {
echo"database error";
}else{
echo"php env successful";
}
$mysqli->close();
phpinfo();
?>
![](https://img2020.cnblogs.com/blog/1725357/202007/1725357-20200731154010245-1127454946.png)