1.Apache的安装
yum install httpd -y
yum install httpd-manual 安装apache的手册
systemctl start httpd 开启apache服务
systemctl enable httpd 设置自动启动
firewall-cmd --permanent --add-service=http
firewall-cmd --reload
1.或者关闭防火墙,笔者这里选择关闭火墙
2.或图形界面添加firewall-config
http://172.25.254.148
2.apache基本信息
1)apache的默认发布文件
index.html
可以把他换成淘宝
先下载一个淘宝网页
scp -r 淘宝网\ -\ 淘!我喜欢* root@172.25.254.148:/var/www/html/ 发送到apache服务器
改名字
访问测试:
systemctl restart httpd.service
如果是别的网站有乱码,改解码方式:
vim /etc/httpd/conf/httpd.conf
316 AddDefaultCharset OFF
2)apache的配置文件
/etc/httpd/conf/httpd.conf
/etc/httpd/conf.d/*.conf
3)apache的默认发布目录
/var/www/html
4)apache的默认端口
80
*****
更改端口vim /etc/httpd/conf/httpd.conf
42 Listen 8080
systemctl restart httpd.service
netstat -antlupe | grep httpd
tcp6 0 0 :::8080 :::* LISTEN 0
95632 31848/httpd
加入防火墙
图形界面permanet public-ports-add-8080
options reload
命令行
firewall-cmd --permanent --add-port=8080/tcp
success
*如果想改成6666等不属于selinux给httpd的
semanage port -l | grep http
http_cache_port_t tcp 8080, 8118, 8123, 10001-10010 (这是selinux给http的端口)
http_cache_port_t udp 3130
http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t tcp 5988
pegasus_https_port_t tcp 5989
setenforce=0
或更改端口安全上下文
semanage port -a -t http_port_t -p tcp 6666
5)默认安全上下文
httpd_sys_content_t
6)程序开启默认用户
apache
7)apache日志
/etc/httpd/logs/*
3.apache的基本配置
1)修改默认发布文件
vim jack.html
vim /etc/httpd/conf/httpd.conf
164 DirectoryIndex jack.html
systemctl restart httpd
测试:
2)修改默认发布目录
mkdir -p /westos/web/html
1.当selinux是disable状态
vim /etc/httpd/conf/httpd.conf
120 DocumentRoot "/westos/web/html"
<Directory "/westos/web/html">
Require all granted
</Directory>
systemctl restart httpd
2.当selinux是enforcing状态
vim /etc/httpd/conf/httpd.conf
120 DocumentRoot "/westos/web/html"
<Directory "/westos/web/html">
Require all granted
</Directory>
systemctl restart httpd
更改文件安全上下文:
semanage fcontext -a -t httpd_sys_content_t '/westos(/.*)?'
restorecon -RvvF /westos
***********************
3)apache的访问控制
1.设定ip的访问
vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html/admin"> ##允许所有人访问admin目录但是拒绝66主机
Order Allow,Deny
Allow from All
Deny from 172.25.254.66
</Directory>
<Directory "
/var/www/html/admin"> ##只允许66主机访问admin目录
Order Deny,Allow
Allow from 172.25.254.66
Deny from All
</Directory>
*如果顺序写错会被后写的覆盖
2.设定用户的访问
htpasswd -cm http_userlist admin c表示创建,名字存在不加c(第二个用户就不用加c)
vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html/admin">
AuthUserFile /etc/httpd/http_userlist ##用户认证文件
AuthName "Please input your name and password !!" ##用户认证提示信息
AuthType basic ##认证类型
Require valid-user ##认证用户,认证文件以通过
[Require user admin] ##只允许认证文件中admin用户访问,二写一
</Directory>
4)apache语言支持
php html cgi 都支持
1)html语言默认支持
2)php语言
vim index.php
<?php
phpinfo();
?>
yum install php -y
systemctl restart httpd
3)cgi语言
mkdir /var/www/html/cgi
在cgi目录下
vim index.cgi
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;
vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html/cgi">
Options +ExecCGI
AddHandler cgi-script .cgi
</Directory>
修改安全上下文
semanage fcontext -a -t httpd_sys_script_exec_t '/var/www/html/cgi(/.*)?'
restorecon -RvvF /var/www/html/cgi/
restorecon reset /var/www/html/cgi context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:httpd_sys_script_exec_t:s0
systemctl restart httpd
或在/var/www/cgi-bin/index.cgi
chmod +x index.cgi
可以运行
4.apache的虚拟主机
1.定义
可以让我们的一台apache服务器在被访问不同域名的时候显示不同的主页
2.建立测试页
mkdir /var/www/virtual/music.westos.com/html -p
mkdir /var/www/virtual/news.westos.com/html -p
echo "music.westos.com's page" >virtual/music.westos.com/html/index.html
echo "news.westos.com's page" >virtual/news.westos.com/html/index.html
3.配置
vim /etc/httpd/conf.d/default.conf ##指定域名的访问都访问default(与httpd.conf类似)
<Virtualhost _default_:80> ##虚拟主机开启的端口
DocumentRoot "/var/www/html" ##虚拟主机的默认发布目录
CustomLog "logs/default.log" combined ##虚拟主机日志
</Virtualhost>
vim /etc/httpd/conf.d/news.conf ##指定域名news.westos.com的访问到指定默认发布目录中
<Virtualhost *:80>
ServerName "news.westos.com"
DocumentRoot "/var/www/virtual/news.westos.com/html"
CustomLog "logs/news.log" combined
</Virtualhost>
<Directory "/var/www/virtual/news.westos.com/html"> ##默认发布目录的访问授权
Require all granted
</Directory>
vim /etc/httpd/conf.d/music.conf
<Virtualhost *:80>
ServerName "music.westos.com"
DocumentRoot "/var/www/virtual/music.westos.com/html"
CustomLog "logs/music.log" combined
</Virtualhost>
<Directory "/var/www/virtual/music.westos.com/html">
Require all granted
</Directory>
4.测试
在浏览器所在主机中
vim /etc/hosts
172.25.254.148 www.westos.com news.westos.com music.westos.com