在linux操作系统下面,apache是非常流行的web服务器,大部分生产环境都是使用apache作为web服务器的,apache和php是亲密搭档,所以学习php编程就一定要学会安装使用apache这个web服务器软件
CentOS:CentOS Linux release 7.6.1810 (Core)
Linux:Linux version 3.10.0-1062.el7.x86_64
GCC:gcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
apr-1.6.5.tar.gz
apr-util-1.6.1.tar.gz
httpd-2.4.46.tar.gz
2020/09/12
如果是新环境,我们需要设置时区以保证时间显示正确
timedatectl set-timezone Asia/Shanghai
如果环境里没有wget,通过yum安装一下
yum -y install wget
如果环境里没有编译工具,通过yum安装一下
yum -y install gcc gcc-c++ make
这些是apache依赖的软件库
yum -y install expat-devel pcre-devel openssl-devel perl libxml* curl-devel freetype-devel autoconf
建立软件安装根目录
mkdir -p /tongfu.net/env/
建立软件安装包保存目录
mkdir /packages cd /packages
apr-1.6.5.tar.gz
apr-util-1.6.1.tar.gz
httpd-2.4.46.tar.gz
下载apr,apr-util,httpd软件包
wget http://mirror.bit.edu.cn/apache/apr/apr-1.6.5.tar.gz wget http://mirror.bit.edu.cn/apache/apr/apr-util-1.6.1.tar.gz wget http://mirror.bit.edu.cn/apache/httpd/httpd-2.4.46.tar.gz
链接: https://pan.baidu.com/s/1-tpTzQy7KeoOLO5IPLsIqA 提取码: buyx
链接: https://pan.baidu.com/s/14N5yMQKQQTOACrl4--PutA 提取码: f2fk
链接: https://pan.baidu.com/s/1BeLWykiEc0_9Txp7AH974g 提取码: uu7m
tar xzvf apr-1.6.5.tar.gz cd apr-1.6.5 ./configure --prefix=/tongfu.net/env/apr-1.6.5 make && make install cd ..
tar xzvf apr-util-1.6.1.tar.gz cd apr-util-1.6.1 ./configure --prefix=/tongfu.net/env/apr-util-1.6.1 \ --with-apr=/tongfu.net/env/apr-1.6.5/bin/apr-1-config make && make install cd ..
tar xzvf httpd-2.4.46.tar.gz cd httpd-2.4.46 ./configure --prefix=/tongfu.net/env/httpd-2.4.46 \ --with-apr=/tongfu.net/env/apr-1.6.5 \ --with-apr-util=/tongfu.net/env/apr-util-1.6.1 \ --with-pcre \ --enable-so \ --enable-rewrite \ --enable-ssl make && make install cd ..
打开 httpd.conf 配置文件
将 ServerAdmin 设置为 webmaster@tongfu.net
将 ServerName 设置为 localhost
将 Listen 设置为 8080
[root@tfdev]# vi /tongfu.net/env/httpd-2.4.46/conf/httpd.conf ServerAdmin webmaster@localhost ServerName localhost Listen 8080
注意:为什么我们要把Apache的端口设置为 8080 呢?因为一般情况下Apache都是工作在Nginx后面的
启动apache
/tongfu.net/env/httpd-2.4.46/bin/apachectl start
添加系统服务脚本
[root@tfdev]# cat > /lib/systemd/system/httpd.service <<EOF [Unit] Description=httpd After=network.target [Service] Type=forking ExecStart=/tongfu.net/env/httpd-2.4.46/bin/apachectl start ExecReload=/tongfu.net/env/httpd-2.4.46/bin/apachectl restart ExecStop=/tongfu.net/env/httpd-2.4.46/bin/apachectl stop PrivateTmp=true [Install] WantedBy=multi-user.target EOF
使用 systemctl 管理 httpd 服务
systemctl enable httpd # 设置自动启动 systemctl start httpd # 启动服务 systemctl stop httpd # 停止服务 systemctl restart httpd # 重启服务
默认CentOS 7会开启防火墙,开发环境的话,直接将防火墙关掉就可以了
systemctl stop firewalld systemctl disable firewalld
通过curl命令测试
[root@fdev]# curl 'http://localhost:8080/' <html><body><h1>It works!</h1></body></html>
通过浏览器测试