Detailed explanation of CentOS7 installation and configuration HTTP server
1. Introduction to HTTP
Apache HTTP Server ( Apache for short ), Chinese name: Apache, is an open source web server of the Apache Software Foundation
The Apache HTTP server is a modular server, derived from the NCSA httpd server, which has been modified many times to become the world's number one Web server software
It is fast, reliable, and extensible through a simple API. It compiles interpreters such as Perl/Python into the server and can 计算机操作系统
run on most servers. It is the most popular Web server-side software because of its cross-platform and security. one
2. Turn off the firewall
In order to avoid unnecessary trouble, we first close the firewall and selinux, and then open the firewall and the corresponding port after the build is successful
[root@centos7 ~]# systemctl status firewalld.service # 查看防火墙状态
[root@centos7 ~]# systemctl stop firewalld.service # 停止防火墙服务
[root@centos7 ~]# systemctl disable firewalld.service # 关闭防火墙开启自启动
# 把文件中的SELINUX=enforcing 改为SELINUX=disabled
[root@centos7 ~]# vim /etc/selinux/config
[root@centos7 ~]# setenforce 0 # 使修改马上生效
3. Install the HTTP package
Check the system version
[root@centos7 ~]# rpm -q centos-release
centos-release-7-9.2009.0.el7.centos.x86_64
Check if the http server is installed
# 如果没有返回任何结果,表示没有安装;如果返回文件包名,这表示已经安装了该服务;
[root@centos7 ~]# httpd -version
Server version: Apache/2.4.6 (CentOS)
Server built: Oct 1 2020 16:52:05 # 代表已安装
[root@centos7 ~]# rpm -qa|grep httpd
httpd-manual-2.4.6-95.el7.centos.noarch
httpd-tools-2.4.6-95.el7.centos.x86_64
httpd-2.4.6-95.el7.centos.x86_64 # 代表已安装
[root@centos7 ~]# rpm -e httpd-manual-2.4.6-95.el7.centos.noarch # 卸载httpd
[root@centos7 ~]# rpm -e httpd-2.4.6-95.el7.centos.x86_64
[root@centos7 ~]# rpm -e httpd-tools-2.4.6-95.el7.centos.x86_64
# 再次检查
[root@centos7 ~]# rpm -qa|grep httpd
[root@centos7 ~]# httpd -version
-bash: /usr/sbin/httpd: 没有那个文件或目录.
start installation
- Install
yum
online
[root@centos7 ~]# yum install -y mod_ssl openssl httpd
- Install
rpm
offline
# 进入准备好httpd服务所需要依赖的目录
[root@centos7 ~]# cd /data/http/httpuser/
[root@centos7 httpuser]# ll
总用量 4740
-rw-r--r-- 1 sftpuser sftp 106124 8月 12 23:55 apr-1.4.8-7.el7.x86_64.rpm
-rw-r--r-- 1 sftpuser sftp 94132 8月 12 23:55 apr-util-1.5.2-6.el7.x86_64.rpm
-rw-r--r-- 1 sftpuser sftp 18976 8月 12 23:55 apr-util-ldap-1.5.2-6.el7.x86_64.rpm
-rw-r--r-- 1 sftpuser sftp 2846172 8月 12 23:55 httpd-2.4.6-95.el7.centos.x86_64.rpm
-rw-r--r-- 1 sftpuser sftp 1409564 8月 13 10:21 httpd-manual-2.4.6-95.el7.centos.noarch.rpm
-rw-r--r-- 1 sftpuser sftp 95136 8月 12 23:55 httpd-tools-2.4.6-95.el7.centos.x86_64.rpm
-rw-r--r-- 1 sftpuser sftp 31264 8月 12 23:55 mailcap-2.1.41-2.el7.noarch.rpm
-rw-r--r-- 1 sftpuser sftp 116812 8月 12 23:55 mod_ssl-2.4.6-95.el7.centos.x86_64.rpm
-rw-r--r-- 1 sftpuser sftp 239900 8月 12 23:55 postgresql-libs-9.2.24-4.el7_8.x86_64.rpm
# 开始安装
- 安装依赖的顺序按照以下先后顺序进行
- 安装rpm包的命令:rpm -ivh 包名
[root@centos7 httpuser]# rpm -ivh apr-1.4.8-7.el7.x86_64.rpm
[root@centos7 httpuser]# rpm -ivh apr-util-1.5.2-6.el7.x86_64.rpm
[root@centos7 httpuser]# rpm -ivh apr-util-ldap-1.5.2-6.el7.x86_64.rpm
[root@centos7 httpuser]# rpm -ivh mailcap-2.1.41-2.el7.noarch.rpm
[root@centos7 httpuser]# rpm -ivh postgresql-libs-9.2.24-4.el7_8.x86_64.rpm
[root@centos7 httpuser]# rpm -ivh httpd-tools-2.4.6-95.el7.centos.x86_64.rpm
[root@centos7 httpuser]# rpm -ivh httpd-2.4.6-95.el7.centos.x86_64.rpm
[root@centos7 httpuser]# rpm -ivh httpd-manual-2.4.6-95.el7.centos.noarch.rpm
[root@centos7 httpuser]# rpm -ivh mod_ssl-2.4.6-95.el7.centos.x86_64.rpm
- Compile
tar.gz
and install
*.tar.gz download The installation process of this method is Baidu, and I will not introduce it here
Set to start the service automatically at boot
[root@centos7 ~]# systemctl enable httpd.service
Start the httpd service
# http服务器的服务名是httpd,相关的操作如下:
[root@centos7 ~]# systemctl start httpd.service # 启动服务
systemctl stop httpd.service # 停止服务
systemctl restart httpd.service # 重启服务
systemctl status httpd.service # 查看服务状态
systemctl enable httpd.service # 设置开机自启动httpd服务
systemctl disable httpd.service # 禁用开机自启动httpd服务
4. Configure the HTTP server
backup configuration file
# Apache默认将网站的根目录指向/var/www/html
# 默认的主配置文件/etc/httpd/conf/httpd.conf
# 配置存储在的/etc/httpd/conf.d/目录
# 防止后期配置文件出错后无法还原
[root@centos7 ~]# cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.backup
[root@centos7 ~]# cd /etc/httpd/conf/
[root@centos7 ssh]# ll
......
-rw-r--r-- 1 root root 11752 8月 13 12:02 httpd.conf
-rw-r--r-- 1 root root 11752 8月 16 22:10 httpd.conf.backup
......
The rest of the configuration is fine by default
Create a new test file
# 先新建一个存放文件的文件夹并授权
[root@centos7 ~]# mkdir -p /var/www/html/upload
[root@centos7 ~]# chmod 755 /var/www/html/upload
# 进入文件夹
[root@centos7 ~]# cd /var/www/html/upload
# 新建测试文件,然后保存退出
[root@centos7 ~]# vim 测试_20220712.txt
[root@centos7 upload]#
5. Restart and configure the firewall
systemctl enable firewalld.service # 重启防火墙开机自启动
systemctl restart firewalld.service # 重启防火墙服务
firewall-cmd --version # 查看防火墙版本
firewall-cmd --list-all # 查看已开放的端口
firewall-cmd --query-port=80/tcp # 查询TCP的80端口占用情况
firewall-cmd --permanent --zone=public --add-port=80/tcp # 开通http服务80端口
firewall-cmd --reload # 刷新防火墙,重新载入
# 设置关闭SELinux对ftp的限制
setsebool -P ftpd_full_access on
sed -i s#enforcing#disabled#g /etc/sysconfig/selinux
setenforce 0 && getenforce
getenforce
6. Restart the HTTP service
systemctl restart httpd.service
So far, HTTP has actually been built successfully, and you can log in!
7. Access test
View IP address
ip addr
Notice:
- The ip address of the cloud server is
公网ip地址
- The ip address of the virtual machine is in NAT mode
固定ip地址
, and the fixed ip is used in the figure below
Browser Access Test
Enter in the browser: http://你的ip地址
, the display is as follows
8. Expand configuration
file directory listing access problem
vim /etc/httpd/conf.d/welcome.conf
# 修改/etc/httpd/conf.d/welcome.conf配置文件
# 把Options -Indexes中的减号改为加号
......
<LocationMatch "^/+$">
Options +Indexes
ErrorDocument 403 /.noindex.html
</LocationMatch>
......
# 重启httpd服务
[root@centos7 ~]# systemctl restart httpd.service
Static resource name garbled problem
vim /etc/httpd/conf/httpd.conf
# 编辑httpd配置文件,增加(或修改)页面的默认编码类型为UTF-8
......
IndexOptions Charset=UTF-8
......
# 重启httpd服务
[root@centos7 ~]# systemctl restart httpd.service
Click on the link to open the question directly in your browser
vim /etc/httpd/conf/httpd.conf
# 编辑本地配置文件,将预期直接下载的文件扩展名配置上
# *.txt文件直接下载
AddType application/x-txt-compressed .txt
# *.pdf文件直接下载
AddType application/x-pdf-compressed .pdf
# *.json文件直接下载
AddType application/x-json-compressed .json
# 重启httpd服务
[root@centos7 ~]# systemctl restart httpd.service
# 清理浏览器缓存
Notice:
- After the configuration is complete and the service is restarted, clear the browser cache first. There may be a problem that the expected effect is not achieved because the browser cache is not cleared.
The problem of incomplete display of long file names
vim /etc/httpd/conf.d/autoindex.conf
# 编辑httpd配置文件,增加(或修改)索引名长度限制为*(任意长度,不作限制)
......
IndexOptions FancyIndexing HTMLTable VersionSort NameWidth=*
......
# 重启httpd服务
[root@centos7 ~]# systemctl restart httpd.service
The problem of enabling user authorization for the specified directory
# 添加访问用户并设置密码
[root@centos7 ~]# htpasswd -c /etc/httpd/auth.pwd httpuser
New password: # 密码输入不显示,正常输入后直接按回车就行
Re-type new password:
Adding password for user httpuser
[root@centos7 ~]#
# 添加用户访问权限配置,直接在配置文件中新增以下内容即可
[root@centos7 ~]# vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html/upload">
AuthName "xxxxx"
AuthType basic
AuthUserFile /etc/httpd/auth.pwd
Require valid-user
# Require user httpuser01
</Directory>
# 重启httpd服务
[root@centos7 ~]# systemctl restart httpd.service
Reference URL:
Common settings for httpd configuration files
Web service two: httpd installation and configuration
Centos7 builds http server to access file directory list
Solve the problem of garbled characters in Httpd static resource server resources
Apache solves the problem of automatically opening access files