1.安装Apache
1.yum install -y httpd httpd-manual
启动Apache服务
2.systemctl start httpd
3.systemctl enable httpd
查看监听端口:
4.ss -antulp| grep httpd

从上图中我们可以知道apache服务监听的是80端口
5.firewall-cmd --list-all ##列出火墙信息

6.firewall-cmd --permanent --add-service=http ##永久允许http
7.firewall-cmd --reload ##重新加载火墙策略
8.firewall-cmd --list-all ##再次列出火墙信息

/var/www/html/ ##apache的根目录,默认发布文件
此时在另外一台主机上访问httpd的服务主机ip,可以看到Apache的测试页面:

/var/www/html/index.html ##apache的默认发布文件
测试:
vim /var/www/hteml/index.html
hello world

3.apache的基础信息
#主配置目录: /etc/httpd/conf
#主配置文件:/etc/httpd/conf/httpd.conf
#子配置目录:/etc/httpd/conf.d/
#子配置文件:/etc/httpd/conf.d/*.conf
#默认发布目录:/var/www/html
#默认发布文件:/var/www/html/index
#默认端口:80
#默认安全上下文:http_sys_content_t
#程序开启默认用户:apache
#apache日志:/etc/httpd/logs/
日志文件中有登陆日志(access_log)和错误日志(error_log):

实验:修改Apache的监听接口
vim /etc/httpd/conf/httpd.conf

修改默认发布文件:
默认发布文件就是访问apache时候没有指定文件名称是默认访问的文件
这个文件可以指定多个,有访问顺序:
vim /etc/httpd/config/httpd.conf
160 # DirectoryIndex: sets the file that Apache will serve if a directory
161 # is requested.
162 #
163 <IfModule dir_module>
164 DirectoryIndex index.html test.html ###在后面加上test.html在index
.html文件不在的时候访问test.html文件vim /var/www/html/test.html
1 test.html
rm -rf /var/www/html/index.html
systemctl restart httpd
访问172.25.254.106:8080此时的页面是:
注:在两个文件都存在的时候会访问写在前面的文件
修改默认发布目录:vim /etc/httpd/config/httpd.conf
120 DocumentRoot "/westos/html"
124 <Directory "/westos/">
125 Require all granted
126 </Directory>[root@localhost html]# semanage fcontext -a -t httpd_sys_content_t '/westos(/.*)?'
[root@localhost html]# restorecon -RvvF /westos/systemctl restart httpd
注:此时的默认发布页也要修改为你在后面设定的默认优先访问的页面再次访问得到/westos/html/'s page
4.apache的虚拟主机
[root@localhost html]# vim /etc/httpd/conf.d/adefault.conf ###编辑httpd的子配置文件
1 <VirtualHost _default_:80> ###虚拟主机的默认页监听端口
2 DocumentRoot "/var/www/html" ###虚拟主机的根目录设置成/var/www/html
3 CustomLog "logs/www.westos.com.log" combined ###虚拟主机默认页的日志存储位置
注:上面写的日志的存储位置是:logs/www.westos.com.log,因为上面已经将虚拟主机的根目录设置成了/var/www/html
这里日志存储的绝对位置是:/var/www/html/logs/www.westos.com.log
4 </VirtualHost>
[root@localhost html]# vim /etc/httpd/conf.d/news.westos.conf
1 <VirtualHost *:80>
2 ServerName news.westos.com
3 DocumentRoot "/var/www/virtual/news.westos.com/html/"
4 CustomLog "logs/news.westos.com.logs" combind
5 </VirtualHost>
6 <Directory "/var/www/virtual/news.westos.com/html">
7 Require all granted
8 </Directory>
[root@http_server www]# mkdir -p /var/www/virtual/linux.westos.com/html/
vim /var/www/virtual/linux.westos.com/html/index.html
1 welcome linux.westos.com's page
测试:访问linux.westos.com

在测试端要有linux.westos.com网址的解析。
vim /etc/hosts ###编辑本地地址解析
下面是添加第二个虚拟主机,步骤和第一个虚拟主机一样。
cp linux.conf c.conf ###复制linux的配置文件到c.conf
vim c.conf 将文件中的linux字符全部修改为c字符,vim命令:%s/linux/c/g 如下图:

mkdir -p /var/www/virtual/c.westos.com/html ##创建c.westos.com 的家目录
vim /var/www/virtual/c.westos.com/html/index.html ##编辑默认访问的html文件
systemctl restart httpd ##重启httpd服务

Apache内部的访问控制:
1.针对与主机的访问控制
1 <VirtualHost _default_:80>
2 DocumentRoot "/var/www/html"
3 </VirtualHost>
4 <Directory "/var/www/html">
5 Require all granted ###列表读取顺序,后读取的列表会覆盖与前>面读取的内容相同的部分
6 Order Allow,Deny
7 Allow from all
8 Deny from 172.25.254.6
9 </Directory>
在172.25.254.6主机上面访问(黑名单用户)

在172.25.254.207上面访问是可以访问的

上面的设置是将172.25.254.6加入到黑名单中,其他中组合情况读者可以自行设置,实验
上面的访问控制是针对ip的,这种访问控制的机制是有危险性的,可以用ip地址掩饰来获取访问权限。下面介绍基于用户方式的访问控制
2.用户方式的访问控制
htpasswd -cm /etc/httpd/htuser admin ##创建认证文件,创建用户admin
htpasswd -m /etc/httpd/htuser admin1
vim adefault.conf
10 <Directory "/var/www/html/admin"> ##目录:/var/www/html/admin以下的设置对于这个目录生效
11 AuthUserFile "/etc/httpd/htuser" ##认证文件是:/etc/httpd/htuser,这个文件的名称是自己设定的
12 AuthName "Please input username and password" ##提示信息
13 AuthType Basic ##认证方式:基本
14 Require valid-user ##限制用户:可以使用的用户(文件中的所有用户)
15 ## Require user admin ##只有用户admin可以登陆
16 </Directory>
实验结果:


此时admin和admin1都可以登陆而设置了只有admin1可以登陆的时候会有下面结果:(禁止admin登录)

####apache支持的四种语言###
1.html
2.php
[root@localhost mnt]# vim /var/www/html/index.php
<?php
phpinfo();
?>
yum install php -y
systemctl restart httpd
测试:
172.25.254.106/index.php
在没安装php软件的时候访问172.25.254.106/index.php时候会出现下面的结果:

安装之后访问,得到下面的结果:

3.cgi
mkdir -p /var/www/html/cgi
[root@localhost mnt]# semanage fcontext -a -t httpd_sys_script_exec_t '/var/www/html/cgi(/.*)?'
[root@localhost mnt]# restorecon -RvvF /var/www/html/cgi/
[root@localhost mnt]# vim /var/www/html/cgi/index.cgi
#!/usr/bin/perl
print "Content-type:text/html\n\n";
print `date`;
[root@localhost mnt]# chmod +x /var/www/html/cgi/index.cgi
[root@localhost mnt]# /var/www/html/cgi/index.cgi
Content-type:text/html
Tue Feb 19 01:49:21 EST 2019
[root@localhost mnt]# vim /etc/httpd/conf.d/adefault.conf
18 <Directory "/var/www/html/cgi">
19 Options +ExecCGI
20 AddHandler cgi-script .cgi
21 </Directory>
systemctl restart httpd
测试:172.25.254.106/cgi/index.cgi

4.wsgi
yum install mod_wsgi -y
[root@localhost mnt]# vim /var/www/html/cgi/westos.wsgi
#!/usr/bin/env python
import time
def application (environ, start_response):
response_body = 'UNIX EPOCH time is now: %s\n' % time.time()
status = '200 OK'
response_headers = [('Content-Type', 'text/plain'),
('Content-Length', '1'),
('Content-Length', str(len(response_body)))]
start_response(status, response_headers)
return [response_body]
注:这只是一个脚本文件,作为测试,需要的话可以自行拷贝,这里没有加上行号。
chmod +x westos.wsgi ###给脚本文件加上运行权限
[root@localhost mnt]# vim /etc/httpd/conf.d/adefault.conf
1 <VirtualHost _default_:80>
2 DocumentRoot "/var/www/html"
3 WSGIScriptAlias /WSGI /var/www/html/cgi/westos.wsgi
4 </VirtualHost>
systemctl restart httpd
测试:
http://172.25.254.100/WSGI

complete!
#7.https
yum install mod_ssl -y
yum install crypto-utils -y
genkey www.westos.com




vim /etc/httpd/conf.d/ssl.conf
101 SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt
108 SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key
systemctl stop firewalld
测试:


点击Confirm Security Exception,重新访问

#8.网址自动加密
cp -p news.westos.conf z_login.westos.conf
vim z_login.westos.conf
1 <VirtualHost *:443>
2 ServerName login.westos.com
3 DocumentRoot /var/www/virtual/login.westos.com/html/
4 CustomLog "logs/login.westos.com.logs" combind
5 SSLEngine on
6 SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt
7 SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key
8 </VirtualHost>
9 <Directory "/var/www/virtual/login.westos.com/html">
10 Require all granted
11 </Directory>
12 <VirtualHost *:80>
13 ServerName login.westos.com
14 RewriteEngine On
15 RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
16 </VirtualHost>
^(/.*)$ ##客户在浏览器地址中输入的所有字符
https:// ##强制客户加密访问
{HTTP_HOST} ##客户请求主机
$1 ##"$1"标示^(/.*)$的值
[redirext=301] ##永久重写 302临时


本文详细介绍了Apache服务器的安装、启动、配置及优化方法,包括端口监听、防火墙设置、虚拟主机配置、访问控制、SSL加密等核心功能。同时,涵盖了如何处理不同类型的网页语言,如HTML、PHP、CGI和WSGI。


2186

被折叠的 条评论
为什么被折叠?



