1.配置主机:
ip hostname--apache.example.com yum源
2.yum install http -y
systemctl start httpd
systemclt enable httpd
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
apache发放目录:/var/www/html
发布文件:vim index.html
vim index.html
hello
172.25.254.119
在浏览器中可以访问到
查看那端口号为80 ,也可以更改访问的端口号,vim /etc/httpd/conf/httpd.conf
Listen 所要更改的端口号
firewall-cmd --permanent --add-port=所要更改的端口号/tcp
firewall-cmd --reload
systemctl restart httpd
在浏览器中查看172.25.254.119:已经该的端口号
更改默认的发布文件
vim /etc/httpd/conf/httpd.conf
DirectoryIndex index.html westos
systemctl restart httpd
删除index.html 之后可以访问到westos中的内容
更改默认发布目录
vim /etc/httpd/conf/httpd.conf
DocumentRoot “/var/www”
Listen 800 //端口号需要更改,80对应index.html这个发布目录。
DirectoryIndex westos index.html //westos优先读取
在/var/www/目录下新建文件westos:vim westos
/var/www/westos is xinjain
在浏览器中可以访问到/var/www/westos文件中的内容
注意:如果无法访问到的,可以使用命令:setenforce 0 //关闭selinux
则在浏览器中可以访问到
实现一个ip对应多个域名操作:
cd /var/www/html
mkdir /var/www/news
mkdir /var/www/music
echo news.westos.com > /var/news/www/westos
echo music.westos.com > /var/music/www/westos
cd /etc/httpd/conf.d
vim default.conf
<Virtualhost _default_:80>
Documentroot /var/www/html
customlog "logs/default.log" combined
</Virtualhost>
<Directory /var/www/html>
require all granted
</Directory>
:wq
cp default.conf news.conf
vim news.conf
<Virtualhost *:80>
servername news.westos.com
Documentroot /var/www/news
customlog "logs/news.log" combined
</Virtualhost>
<Directory /var/www/news>
require all granted
</Directory>
vim music.conf
<Virtualhost *:80>
servername news.westos.com
Documentroot /var/www/news
customlog "logs/news.log" combined
</Virtualhost>
<Directory /var/www/news>
require all granted
</Directory>
:wq
在主机端:vim /etc/hosts
添加:172.25.254.119 www.westos.com news.westos.com music.westos.com
补充:1.Linux 的/etc/hosts是配置ip地址和其对应主机名的文件,这里可以记录本机的或其他主机的ip及其对应主机名。
2.hosts文件是Linux系统中一个负责IP地址与域名快速解析的文件,以ASCII格式保存在“/etc”目录下,文件名为“hosts”(不同的linux版本,这个配置文件也可能不同。比如Debian的对应文件是/etc/hostname)。hosts文件包含了IP地址和主机名之间的映射,还包括主机名的别名。在没有域名服务器的情况下,系统上的所有网络程序都通过查询该文件来解析对应于某个主机名的IP地址,否则就需要使用DNS服务程序来解决。通常可以将常用的域名和IP地址映射加入到hosts文件中,实现快速方便的访问。
3.Linux主机名的相关配置文件就是/etc/hosts;这个文件告诉本主机哪些域名对应那些ip,那些主机名对应哪些ip:
4、 /hosts文件可以帮助解决哪些问题
4.1 远程登录linux主机过慢问题
有时客户端想远程登录一台linux主机,但每次登录输入密码后都会等很长一段时间才会进入,这是因为linux主机在返回信息时需要解析ip,如果在linux主机的hosts文件事先加入客户端的ip地址,这时再从客户端远程登录linux就会变很快。
注:这里所说的远程登录不仅仅是ssh,还可能是mysql远程登录,或是文件共享的查询等。
4.2 双机互连
当两台主机只是双机互连时,这时两台主机都需要设置自己的ip,同时在对方的hosts文件里加入自己的ip和主机名。
5、主机名修改工具hostname;
通过hostname 工具来设置主机名只是临时的,下次重启系统时,此主机名将不会存在
systemctl restart httpd
重新启动服务后,在浏览器中分别输入www.westos.com,news.westos.com,music.westos.com 都可以看见他同样显示westos的内容
设置文件查看需要指定加密用户才可以查看年;
cd /etc/httpd/conf
htpasswd -cm apacheusr admin //-c表示创建用户
输入两边密码;
cat apacheusr
admin:$apr1$GSO6HJaz$Dk377Fq/WbQiVDB0azlpa0 //说明用户已经加密
此时在浏览器中无法查看到内容,
yum install php.x86_64
systemctl restart httpd
cd /etc/http/conf.d
vim php.conf
Directory index.php
cd /etc/httpd/conf.d
vim news.westos.com
添加:<Directory /var/www/news/admin>
Authuserfile /etc/httpd/conf/apacheusr
Authname "please input your name and passwd"
Authtype basic
# Require user admin
Require valid-user
</Directory>
设置完成后,在浏览器查看:news.westos.com/admin ,会弹出一个需要输入用户和密码的框,输入用户名和密码后才可以查看文件
设置将脚本里的内容执行并可在网页上显示:
yum install httpd-manual -y
mkdir /var/www/music/cgi
vim /var/www/music/cgi/index.cgi
#!/usr/bin/perl
print "Content-type:text/html\n\n";
print `date`;
perl /var/www/music/cgi/index.cgi //执行prel语句所写的cgi内容
此时在浏览器中只能查看到cgi文件中的字符,无法得到语句的实现
vim /etc/httpd/conf/httpd.conf.d
DirectoryIndex index.php index.html westos
vim /etc/httpd/conf.d/music.conf
添加内容:<Directory /var/www/music>
require all granted
</Directory>
<Directory /var/www/music/cgi>
options +ExecCGI
AddHandler cgi-scripts .cgi
</Directory>
systemctl restart httpd
cd /var/www/music/cgi
chmod +x index.cgi
此时在浏览器中就可以查看到 cgi 文件中语句执行的内容(此处应该为显示当前的时间)
如果不行,则执行命令:setenforce 0
[root@apache~]#ls -Z /var/www/cgi-bin/ -d
drwxr-xr-x. root root system_u:object_r:httpd_sys_scripts_exec_t:s0 /var/www/cgi_bin/
[root@apache~]#semanage fcontext -a -t http_sys_script_exec_t '/var/www/music/cgi(/.*)?'
[root@apache~]#restorecon -FvvR /var/www/music/cgi
apache默认只能访问/var/www目录,并只能监听80和443端口,因此能有效的防范0-day类的攻击。举例来说,系统上的 Apache 被发现存在一个漏洞,使得某远程用户可以访问系统上的敏感文件(比如 /etc/passwd 来获得系统已存在用户),而修复该安全漏洞的 Apache 更新补丁尚未释出。此时 SELinux 可以起到弥补该漏洞的缓和方案。因为 /etc/passwd 不具有 Apache 的访问标签,所以 Apache 对于 /etc/passwd 的访问会被 SELinux 阻止。
开启apache的另一监听端口(443)
netstat -antlpe | grep httpd //查看httpd的监听端口
此时只有80端口开启
yum instal mod_ssl -y
cd /etc/httpd/conf.d
vim ssl.conf
在100行:SSLCertificateFile /etc/pki
在107行:SSLCertificateKeyFile /etc/pki/tls/private/apache.example.com.key //将SSL指令指向密钥匙】文件
netstat -antlpe | grep httpd
此时apache的来嗯个端口全都开启(80和443)
配置https(自定义自签名证书)
自定义自签名证书。如果加密的通信非常重要,而经过验证的身份部重要,管理员可以通过生成self-
signed certificate来避免与认证机构进行交互所带来的复杂性。
使用genkey实用程序(通过crypto-utils软件包分发),生成自签名证书及其关联的
私钥。为了简化起见,genkey将在“正确”的位置(/etc/pki/tls目录)创建证书及其
关联的密钥。相应地,必须以授权用户(root)身份运行该实用程序。
生成自签名证书
yum install crypto-utils mod_ssl -y
genkey apache.example.com //--days可以指定证书的有效期
安装证书及其私钥
确定已安装mod_ssl软件包。
[root@apache ~]# yum install mod_ssl
由于私钥是敏感信息,请确保其只被root用户读取。
[root@apache~]# ls -l /etc/pki/tls/private/server0.example.com.key
-r--------. 1 root root 1737 Dec 22 15:06 /etc/pki/tls/private/server0.example.com.key
编辑/etc/httpd/conf.d/ssl.conf, 将SSLCertificateFile和SSLCertificateKeyFile指令设置为分别指向X.509证书和密钥文件。
SSLCertificateFile /etc/pki/tls/certs/server0.example.com.crt
SSLCertificateKeyFile /etc/pki/tls/private/server0.example.com.key
重启Web服务器。
[root@apache ~]# systemctl restart httpd
如要进行确认,请使用https协议(https://serverX.example.com)通过Web客户端(如Firefox
)访问Web服务器。
Web客户端可能会发出它不认可证书发行者的警告。这种情况适用自签名证书。要求Web客户端
绕过证书认证。(对于Firefox,请选择“I Understand the Risks” [我了解风险]、“Add Exception” [
添加例外]和“Confirm Security Exception”[确认安全例外]。)
网页重写
把所有80端口的请求全部重定向由https来处理
<Virtualhost *:80>
ServerName www0.example.com
RewriteEngine on
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
</Virtualhost>
Example:
vim /etc/httpd/conf.d/login.conf
<VirtualHost *:443>
servername login.westos.com
Documentroot /var/www/login
Customlog "logs/login.log" combined
SSLEngine on
SSLCertificateFile /etc/pki/tls/private/apache.example.com.crt
SSLCertificateKeyFile /etc/pki/tls/private/apache.example.com.key
</Vitualhost>
<Directory "/var/www/login">
require all granted
</Directory>
<VirtualHost *:80>
servername login.westos.com
RewriteEngine on
Rewriterule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
</VirtualHost>
systemctl restart httpd
CGI
通用网关接口(CGI)是网站上放置动态内容的最简单的方法。CGI脚本可用于许多目
的,但是谨慎控制使用哪个CGI脚本以及允许谁添加和运行这些脚本十分重要。编写质量差的CGI
脚本可能为外部攻击者提供了破坏网站及其内容安全性的途径。因此,在Web服务器级别和
SELinux策略级别,都存在用于限制CGI脚本使用的设置。
Example:
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>
# ll -dZ /var/www/cgi-bin/
drwxr-xr-x. root root system_u:object_r:httpd_sys_script_exec_t:s0 /var/www/cgi-bin/