[Centos系统] CentOS7 安装配置HTTP服务器详解

1050 0
Honkers 2025-3-6 04:11:57 来自手机 | 显示全部楼层 |阅读模式

CentOS7 安装配置HTTP服务器详解

1、HTTP简介

Apache HTTP Server(简称Apache),中文名:阿帕奇,是Apache软件基金会的一个开放源码的网页服务器

Apache HTTP服务器是一个模块化的服务器,源于NCSAhttpd服务器,经过多次修改,成为世界使用排名第一的Web服务器软件

它快速、可靠并且可通过简单的API扩展,将Perl/Python等解释器编译到服务器中,可以在大多数计算机操作系统中运行,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一

2、关闭防火墙

为了避免不必要的麻烦,我们先关闭防火墙和selinux,等搭建成功之后再开启防火墙和相应的端口

  1. [root@centos7 ~]# systemctl status firewalld.service # 查看防火墙状态
  2. [root@centos7 ~]# systemctl stop firewalld.service # 停止防火墙服务
  3. [root@centos7 ~]# systemctl disable firewalld.service # 关闭防火墙开启自启动
  4. # 把文件中的SELINUX=enforcing 改为SELINUX=disabled
  5. [root@centos7 ~]# vim /etc/selinux/config
  6. [root@centos7 ~]# setenforce 0 # 使修改马上生效
复制代码

3、安装HTTP软件包

查看一下系统版本

  1. [root@centos7 ~]# rpm -q centos-release
  2. centos-release-7-9.2009.0.el7.centos.x86_64
复制代码

查看是否已经安装了http服务器

  1. # 如果没有返回任何结果,表示没有安装;如果返回文件包名,这表示已经安装了该服务;
  2. [root@centos7 ~]# httpd -version
  3. Server version: Apache/2.4.6 (CentOS)
  4. Server built: Oct 1 2020 16:52:05 # 代表已安装
  5. [root@centos7 ~]# rpm -qa|grep httpd
  6. httpd-manual-2.4.6-95.el7.centos.noarch
  7. httpd-tools-2.4.6-95.el7.centos.x86_64
  8. httpd-2.4.6-95.el7.centos.x86_64 # 代表已安装
  9. [root@centos7 ~]# rpm -e httpd-manual-2.4.6-95.el7.centos.noarch # 卸载httpd
  10. [root@centos7 ~]# rpm -e httpd-2.4.6-95.el7.centos.x86_64
  11. [root@centos7 ~]# rpm -e httpd-tools-2.4.6-95.el7.centos.x86_64
  12. # 再次检查
  13. [root@centos7 ~]# rpm -qa|grep httpd
  14. [root@centos7 ~]# httpd -version
  15. -bash: /usr/sbin/httpd: 没有那个文件或目录.
复制代码

开始安装

  • 采用yum在线安装方式
  1. [root@centos7 ~]# yum install -y mod_ssl openssl httpd
复制代码

  • 采用rpm离线安装方式

*.rpm下载 快捷下载

  1. # 进入准备好httpd服务所需要依赖的目录
  2. [root@centos7 ~]# cd /data/http/httpuser/
  3. [root@centos7 httpuser]# ll
  4. 总用量 4740
  5. -rw-r--r-- 1 sftpuser sftp 106124 8月 12 23:55 apr-1.4.8-7.el7.x86_64.rpm
  6. -rw-r--r-- 1 sftpuser sftp 94132 8月 12 23:55 apr-util-1.5.2-6.el7.x86_64.rpm
  7. -rw-r--r-- 1 sftpuser sftp 18976 8月 12 23:55 apr-util-ldap-1.5.2-6.el7.x86_64.rpm
  8. -rw-r--r-- 1 sftpuser sftp 2846172 8月 12 23:55 httpd-2.4.6-95.el7.centos.x86_64.rpm
  9. -rw-r--r-- 1 sftpuser sftp 1409564 8月 13 10:21 httpd-manual-2.4.6-95.el7.centos.noarch.rpm
  10. -rw-r--r-- 1 sftpuser sftp 95136 8月 12 23:55 httpd-tools-2.4.6-95.el7.centos.x86_64.rpm
  11. -rw-r--r-- 1 sftpuser sftp 31264 8月 12 23:55 mailcap-2.1.41-2.el7.noarch.rpm
  12. -rw-r--r-- 1 sftpuser sftp 116812 8月 12 23:55 mod_ssl-2.4.6-95.el7.centos.x86_64.rpm
  13. -rw-r--r-- 1 sftpuser sftp 239900 8月 12 23:55 postgresql-libs-9.2.24-4.el7_8.x86_64.rpm
  14. # 开始安装
  15. - 安装依赖的顺序按照以下先后顺序进行
  16. - 安装rpm包的命令:rpm -ivh 包名
  17. [root@centos7 httpuser]# rpm -ivh apr-1.4.8-7.el7.x86_64.rpm
  18. [root@centos7 httpuser]# rpm -ivh apr-util-1.5.2-6.el7.x86_64.rpm
  19. [root@centos7 httpuser]# rpm -ivh apr-util-ldap-1.5.2-6.el7.x86_64.rpm
  20. [root@centos7 httpuser]# rpm -ivh mailcap-2.1.41-2.el7.noarch.rpm
  21. [root@centos7 httpuser]# rpm -ivh postgresql-libs-9.2.24-4.el7_8.x86_64.rpm
  22. [root@centos7 httpuser]# rpm -ivh httpd-tools-2.4.6-95.el7.centos.x86_64.rpm
  23. [root@centos7 httpuser]# rpm -ivh httpd-2.4.6-95.el7.centos.x86_64.rpm
  24. [root@centos7 httpuser]# rpm -ivh httpd-manual-2.4.6-95.el7.centos.noarch.rpm
  25. [root@centos7 httpuser]# rpm -ivh mod_ssl-2.4.6-95.el7.centos.x86_64.rpm
复制代码
  • 采用tar.gz编译安装方式

*.tar.gz下载 此方法的安装过程自行百度,此处不在做介绍

设置为开机自动启动服务

  1. [root@centos7 ~]# systemctl enable httpd.service
复制代码

启动httpd服务

  1. # http服务器的服务名是httpd,相关的操作如下:
  2. [root@centos7 ~]# systemctl start httpd.service # 启动服务
  3. systemctl stop httpd.service # 停止服务
  4. systemctl restart httpd.service # 重启服务
  5. systemctl status httpd.service # 查看服务状态
  6. systemctl enable httpd.service # 设置开机自启动httpd服务
  7. systemctl disable httpd.service # 禁用开机自启动httpd服务
复制代码

4、配置HTTP服务器

备份配置文件

  1. # Apache默认将网站的根目录指向/var/www/html
  2. # 默认的主配置文件/etc/httpd/conf/httpd.conf
  3. # 配置存储在的/etc/httpd/conf.d/目录
  4. # 防止后期配置文件出错后无法还原
  5. [root@centos7 ~]# cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.backup
  6. [root@centos7 ~]# cd /etc/httpd/conf/
  7. [root@centos7 ssh]# ll
  8. ......
  9. -rw-r--r-- 1 root root 11752 8月 13 12:02 httpd.conf
  10. -rw-r--r-- 1 root root 11752 8月 16 22:10 httpd.conf.backup
  11. ......
复制代码

其余配置默认就好

新建一个测试文件

  1. # 先新建一个存放文件的文件夹并授权
  2. [root@centos7 ~]# mkdir -p /var/www/html/upload
  3. [root@centos7 ~]# chmod 755 /var/www/html/upload
  4. # 进入文件夹
  5. [root@centos7 ~]# cd /var/www/html/upload
  6. # 新建测试文件,然后保存退出
  7. [root@centos7 ~]# vim 测试_20220712.txt
  8. [root@centos7 upload]#
复制代码

5、重启并配置防火墙

  1. systemctl enable firewalld.service # 重启防火墙开机自启动
  2. systemctl restart firewalld.service # 重启防火墙服务
  3. firewall-cmd --version # 查看防火墙版本
  4. firewall-cmd --list-all # 查看已开放的端口
  5. firewall-cmd --query-port=80/tcp # 查询TCP的80端口占用情况
  6. firewall-cmd --permanent --zone=public --add-port=80/tcp # 开通http服务80端口
  7. firewall-cmd --reload # 刷新防火墙,重新载入
  8. # 设置关闭SELinux对ftp的限制
  9. setsebool -P ftpd_full_access on
  10. sed -i s#enforcing#disabled#g /etc/sysconfig/selinux
  11. setenforce 0 && getenforce
  12. getenforce
复制代码

6、重启HTTP服务

  1. systemctl restart httpd.service
复制代码

至此,HTTP其实就已经搭建成功,可以登录了!

7、访问测试

查看IP地址

ip addr

注意:

  • 云服务器的ip地址为公网ip地址
  • 虚拟机的ip地址为NAT模式下的固定ip地址,下图用的就是固定ip

浏览器访问测试

在浏览器中输入:http://你的ip地址,显示如下

8、拓展配置

文件目录列表访问问题

vim /etc/httpd/conf.d/welcome.conf

  1. # 修改/etc/httpd/conf.d/welcome.conf配置文件
  2. # 把Options -Indexes中的减号改为加号
  3. ......
  4. <LocationMatch "^/+$">
  5. Options +Indexes
  6. ErrorDocument 403 /.noindex.html
  7. </LocationMatch>
  8. ......
  9. # 重启httpd服务
  10. [root@centos7 ~]# systemctl restart httpd.service
复制代码

静态资源名称乱码问题

vim /etc/httpd/conf/httpd.conf

  1. # 编辑httpd配置文件,增加(或修改)页面的默认编码类型为UTF-8
  2. ......
  3. IndexOptions Charset=UTF-8
  4. ......
  5. # 重启httpd服务
  6. [root@centos7 ~]# systemctl restart httpd.service
复制代码

点击链接直接在浏览器中打开的问题

vim /etc/httpd/conf/httpd.conf

  1. # 编辑本地配置文件,将预期直接下载的文件扩展名配置上
  2. # *.txt文件直接下载
  3. AddType application/x-txt-compressed .txt
  4. # *.pdf文件直接下载
  5. AddType application/x-pdf-compressed .pdf
  6. # *.json文件直接下载
  7. AddType application/x-json-compressed .json
  8. # 重启httpd服务
  9. [root@centos7 ~]# systemctl restart httpd.service
  10. # 清理浏览器缓存
复制代码

注意:

  • 配置完成并重启服务后先清理一下浏览器缓存,有可能会出现因为浏览器缓存未清理,而导致未达到预期效果的问题

文件名较长显示不全的问题

vim /etc/httpd/conf.d/autoindex.conf

  1. # 编辑httpd配置文件,增加(或修改)索引名长度限制为*(任意长度,不作限制)
  2. ......
  3. IndexOptions FancyIndexing HTMLTable VersionSort NameWidth=*
  4. ......
  5. # 重启httpd服务
  6. [root@centos7 ~]# systemctl restart httpd.service
复制代码

指定目录启用用户授权的问题

  1. # 添加访问用户并设置密码
  2. [root@centos7 ~]# htpasswd -c /etc/httpd/auth.pwd httpuser
  3. New password: # 密码输入不显示,正常输入后直接按回车就行
  4. Re-type new password:
  5. Adding password for user httpuser
  6. [root@centos7 ~]#
  7. # 添加用户访问权限配置,直接在配置文件中新增以下内容即可
  8. [root@centos7 ~]# vim /etc/httpd/conf/httpd.conf
  9. <Directory "/var/www/html/upload">
  10. AuthName "xxxxx"
  11. AuthType basic
  12. AuthUserFile /etc/httpd/auth.pwd
  13. Require valid-user
  14. # Require user httpuser01
  15. </Directory>
  16. # 重启httpd服务
  17. [root@centos7 ~]# systemctl restart httpd.service
复制代码

参考网址:

httpd的配置文件常见设置

Web服务之二:httpd安装配置

centos7搭建http服务器访问文件目录列表

解决Httpd静态资源服务器资源乱码问题

Apache解决访问文件自动打开问题

Linux学习之HTTP服务

Centos7/8搭建https服务器(SSL域名证书的申请和部署–Apache及Nginx实现HTTPS)

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2026 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行