Linux(9)Apache
文章目录
- Apache
- 1)概述
- 2)部署方案
- 3)yum安装Apache
- 3.1、安装Apache
- 3.2、启动Apache
- 3.4、检查服务是否启动成功
- 3.5、创建一个html页面
- 3.6、yum安装Apache相关配置文件
- 4)源码安装Apache
- 4.1、源码编译口诀
- 4.2、安装Apache
- 4.2.1、获取软件(官网)
- 4.2.2、创建管理目录
- 4.2.3、解压软件包
- 4.2.4、进入解压文件中
- 4.2.5、检查环境
- 4.2.6、编译即安装
- 4.2.7、启动服务
- 4.2.8、测试
- 4.2.9、添加防火墙规则
- 4.3错误汇总
- 4.3.1.缺少gcc环境
- 4.3.2.缺少Expat库
- 4.3.3.缺少`pcre-devel`
- 4.3.4.致命错误:
- 5)源码Httpd目录的结构
- 6)一台服务器运行多个Web服务
- 6.1、开启虚拟主机的模块(修改主配置文件)
- 6.2、配置虚拟主机
- 6.2.1.基于端口号
- 6.2.2.基于域名
- 6.2.3.基于IP地址
- 6.3、重启和检查配置文件
- 7)用`systemctl`管理源安装的Apache
Apache
1)概述
Apache 是世界上使用最为广泛的 Web 服务器软件之一,由 Apache 软件基金会开发和维护。
2)部署方案
- 基于仓库(本地/)
- 基于源码
3)yum安装Apache
3.1、安装Apache
#软件名为httpd
[root@localhost ~]# yum -y install httpd
3.2、启动Apache
管理服务的命令
-
命令:
systemctl
-
语法:
systemctl [options] [serever_name]
-
选项:
[!NOTE]
start
启动stop
停止restart
重启relaod
重新加载enable
加入开机自启disable
开机不自启#现在立即关闭防火墙并开机不自启(永久关闭防火墙) [root@localhost ~]# systemctl disable firewalld --now
[root@localhost ~]# systemctl start httpd
3.4、检查服务是否启动成功
[root@localhost ~]# ss -luntp | grep 80
tcp LISTEN 0 128 [::]:80 [::]:* users:(("httpd",pid=65695,fd=4),("httpd",pid=65694,fd=4),("httpd",pid=65693,fd=4),("httpd",pid=65692,fd=4),("httpd",pid=65691,fd=4),("httpd",pid=65686,fd=4))
访问测试
-
本地测试:
curl 127.0.0.1
-
客户端测试:IP地址
3.5、创建一个html页面
-
命令:
vim/var/www/html/index.html
-
路径:
/var/www/html/index.html
3.6、yum安装Apache相关配置文件
[!IMPORTANT]
/etc/httpd/conf
httpd的主配置目录
/etc/httpd/logs
服务日志目录
/usr/sbin/htppd
服务启动脚本程序
/var/log/httpd
存放log日志
4)源码安装Apache
4.1、源码编译口诀
1.检查环境
- 命令:
configure
2.编译(测试)
- 命令:
make
3.编译安装
- 命令:
make install
4.2、安装Apache
4.2.1、获取软件(官网)
-
命令:
wget
Apache官网:www.apache.org apr:https://dlcdn.apache.org//apr/apr-1.7.5.tar.gz apr-util:https://dlcdn.apache.org//apr/apr-util-1.6.3.tar.gz httpd:https://dlcdn.apache.org/httpd/httpd-2.4.62.tar.gz ======================================================== [root@localhost ~]# wget https://dlcdn.apache.org//apr/apr-1.7.5.tar.gz
4.2.2、创建管理目录
-
默认目录:
/usr/local
[root@localhost local]# mkdir httpd24 [root@localhost httpd24]# mkdir apr [root@localhost httpd24]# mkdir apr-util [root@localhost httpd24]# mkdir apache
4.2.3、解压软件包
-
命令:
tar -xf [压缩包] -C [解压的位置]
[root@localhost httpd24]# tar -xf apr-1.7.5.tar.gz -C apr/
4.2.4、进入解压文件中
[root@localhost httpd24]# cd apr/
[root@localhost apr]# cd apr-1.7.5/
4.2.5、检查环境
-
命令:
configure --prefix
-
--with-apr=/usr/local/httpd24/apr
#依赖apr#检查apr安装环境 [root@localhost apr-1.7.5]# ./configure --prefix /usr/local/httpd24/apr================================================= #检查apr-util安装环境 [root@localhost apr-util-1.6.3]# ./configure --prefix /usr/local/httpd24/apr-util --with-apr=/usr/local/httpd24/apr ================================================= #检查apache安装环境 \ 不进行换行 [root@localhost apache httpd-2.4.62]#./configure --prefix /usr/local/httpd24/apache \--with-apr=/usr/local/httpd24/apr\--with-apr-util=/usr/local/httpd24/apr-util
4.2.6、编译即安装
-
命令:
make && make install
[root@localhost apr-1.7.5]# make && make install
4.2.7、启动服务
-
bin/apachectl
[root@localhost apache]$ bin/apachectl
4.2.8、测试
- 命令:
lsof -i:80
- 命令:
ss -luntp |grep httpd
4.2.9、添加防火墙规则
-
命令:
firewall-cmd --add-port=80/tcp
[root@localhost apache]# firewall-cmd --add-port=80/tcp success
4.3错误汇总
4.3.1.缺少gcc环境
configure: error: in '/usr/local/httpd24/apr/apr-1.7.5':
configure: error: no acceptable C compiler found in $PATH
See 'config.log' for more details
解决方法:安装gcc
[root@localhost apr-1.7.5]# yum -y install gcc gcc-c++
4.3.2.缺少Expat库
xml/apr_xml.c:35:19: fatal error: expat.h: No such file or directory#include <expat.h>^
compilation terminated.
make[1]: *** [xml/apr_xml.lo] 错误 1
make[1]: 离开目录“/usr/local/httpd24/apr-util/apr-util-1.6.3”
make: *** [all-recursive] 错误 1
解决方法:安装expat库expat-devel
[root@localhost apr-util-1.6.3]# yum -y install expat-devel
4.3.3.缺少pcre-devel
configure: error: pcre(2)-config for libpcre not found. PCRE is required and available from http://pcre.org/
解决方法:安装pcre-devel
[root@localhost httpd-2.4.62]# yum -y install pcre-devel
4.3.4.致命错误:
collect2: error: ld returned 1 exit status
make[2]: *** [htpasswd] 错误 1
make[2]: 离开目录“/usr/local/httpd24/apache/httpd-2.4.62/support”
make[1]: *** [all-recursive] 错误 1
make[1]: 离开目录“/usr/local/httpd24/apache/httpd-2.4.62/support”
make: *** [all-recursive] 错误 1
解决方法:清楚所有编译(apr apr-util httpd
)
-
命令:
make clean
5)源码Httpd目录的结构
所有的服务的相关配置文件都会存放在你所指定的目录中:–prefix=路径
[root@bogon apache]# tree -L 1
├── bin #二级制文件
├── build #构建
├── cgi-bin
├── conf #主配置文件的存放目录
├── error #错误信息(服务启动错误日志)
├── htdocs #html存放的位置
├── include #引入/导入
├── logs #存放访问及错误日志的目录
└── modules #存放目录的配置文件目录
Httpd的配置文件目录
[root@bogon conf]# tree -L 1
├── extra #虚拟主机存放配置文件
├── httpd.conf #主配置文件
├── magic
├── mime.types
└── original
Httpd的日志存放目录
[root@bogon apache]# tree -L 1 logs/
logs/
├── access_log #访问日志
├── error_log #错误日志
└── httpd.pid #httpd服务的进程ID
6)一台服务器运行多个Web服务
虚拟主机实现
- 基于端口(例80)
- 基于域名
- 基于IP地址
实现步骤
6.1、开启虚拟主机的模块(修改主配置文件)
-
命令:
vim /usr/local/http24/apache/conf/httpd.conf
475 # Virtual hosts 476 Include conf/extra/httpd-vhosts.conf #把注释#删除
-
描述:当服务开启时需要读取配置文件
6.2、配置虚拟主机
命令:vim/usr/local/http24/apache/conf/extrahttpd-vhosts.conf
<VirtualHost *:80>
#服务管理ServerAdmin SereveName test.com#服务名称SereveAlias#服务别名DocumentRoot "/usr/local/http24/apache/htdocs/test.com"#服务的根目录ErrorLog "logs/test.com-error_log"#服务错误日志CustomLog "logs/test.com-access_log" common#服务访问日志
6.2.1.基于端口号
<VirtualHost *:80>DocumentRoot "/usr/local/http24/apache/htdocs/test.com"ErrorLog "logs/test.com-error_log"CustomLog "logs/test.com-access_log" common
</VirtualHost>Listen 81
<VirtualHost *:81>DocumentRoot "/usr/local/http24/apache/htdocs/test81.com"ErrorLog "logs/test81.com-error_log"CustomLog "logs/test81.com-access_log" common
</VirtualHost>
#在/usr/local/http24/apache/htdocs 创建目录 并新建index.html
#添加规则 firewall-cmd --add-port=81/tcp
#访问时在IP地址后加上端口号
6.2.2.基于域名
<VirtualHost *:80>SereveName test.comDocumentRoot "/usr/local/http24/apache/htdocs/test.com"ErrorLog "logs/test.com-error_log"CustomLog "logs/test.com-access_log" common
</VirtualHost>#Listen 81
<VirtualHost *:80>SereveName test81.comDocumentRoot "/usr/local/http24/apache/htdocs/test81.com"ErrorLog "logs/test81.com-error_log"CustomLog "logs/test81.com-access_log" common
</VirtualHost>
#vim/etc/hosts 添加IP地址 加对应域名
#在测试机上/windows/system32/drivers/etc/hosts 也添加IP地址 加对应域名
6.2.3.基于IP地址
<VirtualHost 192.168.108.46:80>SereveName test.comDocumentRoot "/usr/local/http24/apache/htdocs/test.com"ErrorLog "logs/test.com-error_log"CustomLog "logs/test.com-access_log" common
</VirtualHost>#Listen 81
<VirtualHost 192.168.108.100*:80>SereveName test81.comDocumentRoot "/usr/local/http24/apache/htdocs/test81.com"ErrorLog "logs/test81.com-error_log"CustomLog "logs/test81.com-access_log" common
</VirtualHost>
6.3、重启和检查配置文件
- 命令:
bin/apachectl -t
- 命令:
bin/apachectl -k restart
7)用systemctl
管理源安装的Apache
创建 Apache 服务单元文件
vim /etc/systemd/system/httpd.service[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target[Service]
Type=forking
# 修改为你的 Apache 可执行文件的实际路径
ExecStart=/usr/local/apache24/bin/apachectl start
# 修改为你的 Apache 可执行文件的实际路径
ExecStop=/usr/local/apache24/bin/apachectl graceful-stop
# 修改为你的 Apache 可执行文件的实际路径
ExecReload=/usr/local/apache24/bin/apachectl graceful
PrivateTmp=true[Install]
WantedBy=multi-user.target
重新加载System管理器配置
systemctl daemon-reload