一、apache 是什么?
apache(全称 apache http server)是当前最流行的开源web服务器软件之一,由apache软件基金会维护。它以稳定性高、模块化设计和灵活的配置著称,支持linux、windows等多平台,是搭建个人博客、企业官网乃至复杂web应用的首选工具。
#apache的基本信息
/etc/httpd/conf | #apache的配置目录 |
---|---|
/etc/http/conf.d | #子配置目录 |
/etc/httpd/conf/httpd.conf | #主配置文件 |
/lib/systemd/system/htpd.service | #启动文件 |
:80 | #默认端口 |
/var/www/html | #默认发布目录 |
index.html | #默认发布文件 |
二、安装apache
# 1. 安装apache sudo dnf install httpd -y # 2. 放行防火墙(允许http/https流量) sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload # 3. 启动并设置开机自启 sudo systemctl enable --now httpd # 4.生成默认测试页文件 echo 172.25.254.100 > /var/www/html/index.html # 5.测试: curl 172.25.254.100 172.25.254.100
三、apache的基本配置信息
1.端口修改
#修改配置文件 [root@apache ~]# vim /etc/httpd/conf/httpd.conf 47 listen 8080 #刷新服务 [root@apache ~]# systemctl reload httpd #设定火墙通过 [root@apache ~]# firewall-cmd --permanent --add-port=8080/tcp success [root@apache ~]# firewall-cmd --reload #检测 [root@apache ~]# netstat -antlupe | grep httpd tcp6 0 0 :::8080 :::* listen 0 78081 32315/httpd #访问: [root@apache ~]# curl 172.25.254.100:8080 172.25.254.100
2.默认发布目录
修改selinux ——开着的话会有所影响 grubby --update-kernel all --args selinux=0 reboot ——重启 getenforce disabled #建立默认发布目录 [root@apache ~]# mkdir /web/html -p #修改配置文件 [root@apache ~]# vim /etc/httpd/conf/httpd.conf 125 documentroot "/web/html" #指定默认发布目录位置 126 <directory "/web/html"> 127 require all granted #对于目录访问进行授权 128 </directory> [root@apache ~]# systemctl restart httpd [root@apache ~]# echo "/web/html's page" > /web/html/index.html [root@apache ~]# curl 172.25.254.100:8080 /web/html's page
3.默认发布文件
#建立新的默认发布文件 [root@apache ~]# echo "/web/html/lee's page" > /web/html/lee.html #当没有对配置进行修改时新默认发布文件不会被默认访问 [root@apache ~]# curl 172.25.254.100:8080 /web/html's page [root@apache ~]# curl 172.25.254.100:8080/lee.html /web/html/lee's page #修改配置文件 [root@apache ~]# vim /etc/httpd/conf/httpd.conf 172 <ifmodule dir_module> 173 directoryindex lee.html index.html 174 </ifmodule> #重启服务 [root@apache ~]# systemctl reload httpd #测试: [root@apache ~]# curl 172.25.254.100:8080 /web/html/lee's page
4.https
#安装mod_ssl [root@apache ~]# dnf install mod_ssl -y #建立证书和key文件目录 [root@apache ~]# mkdir /etc/httpd/certs #制作证书 [root@apache ~]# openssl req \ -newkey rsa:2048 \ -nodes \ -sha256 \ -keyout /etc/httpd/certs/timinglee.org.key \ -x509 \ -days 365 \ -out /etc/httpd/certs/timinglee.org.crt country name (2 letter code) [xx]:cn state or province name (full name) []:shannxi locality name (eg, city) [default city]:xi'an organization name (eg, company) [default company ltd]:timinglee organizational unit name (eg, section) []:webserver common name (eg, your name or your server's hostname) []:www.timinglee.org email address []:timinglee@timinglee.org #命令执行完成,证书出现 [root@apache ~]# ls /etc/httpd/certs/ timinglee.org.crt timinglee.org.key #编辑主配置文件 [root@apache ~]# vim /etc/httpd/conf.d/ssl.conf 86 sslcertificatefile /etc/httpd/certs/timinglee.org.crt 95 sslcertificatekeyfile /etc/httpd/certs/timinglee.org.key #重启服务 root@apache ~]# systemctl reload httpd [root@apache ~]# netstat -antlupe | grep httpd tcp6 0 0 :::443 :::* listen 0 85111 33518/httpd tcp6 0 0 :::80 :::* listen 0 80172 33518/httpd #在浏览器中访问 https://服务器ip
5.apache的虚拟主机
修改selinux ——有所影响 grubby --update-kernel all --args selinux=1 reboot ——重启 getenforce enforcing #为每个发布站点建立默认发布目录 [root@apache ~]# mkdir -p /var/www/virtual/timinglee.org/news [root@apache ~]# mkdir -p /var/www/virtual/timinglee.org/bbs #为每个站点建立默认发布文件 [root@apache ~]# echo new.timinglee.org > /var/www/virtual/timiniglee.org/news/index.html [root@apache ~]# echo bbs.timinglee.org > /var/www/virtual/timiniglee.org/bbs/index.html #修改配置文件 [root@apache ~]# vim /etc/httpd/conf.d/vhosts.conf <virtualhost _default_:80> documentroot /var/www/html </virtualhost> <virtualhost *:80> servername bbs.timinglee.org documentroot /var/www/virtual/timiniglee.org/bbs/ </virtualhost> <virtualhost *:80> servername news.timinglee.org documentroot /var/www/virtual/timiniglee.org/news/ </virtualhost> #刷新服务 [root@apache ~]# systemctl reload httpd #测试: 1.在浏览器所在主机中手动编写本地解析文件 [root@apache ~]# vim /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 #加入虚拟主机解析域名 172.25.254.100 mariadb.timinglee.org www.timinglee.org news.timinglee.org bbs.timinglee.org 2.测试效果 [root@apache ~]# curl www.timinglee.org 172.25.254.100 [root@apache ~]# curl bbs.timinglee.org bbs.timinglee.org [root@apache ~]# curl news.timinglee.org new.timinglee.org
到此这篇关于apache http server 从安装到配置过程详解的文章就介绍到这了,更多相关apache http server安装配置内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!