Apache
- 1.Apache的作用
- 2.Apache的安裝及啟用
- 3.Apache的基本資訊
- 4.Apache的基本配置
-
- 1.Apache端口修改在檔案中改端口-預設為80:
- 2.預設釋出檔案
- 3.預設釋出目錄
- 6.Apache的通路控制
-
- 1.基于用戶端ip的通路控制
-
- (1)ip白名單
- (2)ip黑名單
- 2.基于使用者認證
- 7.Apache的虛拟主機
- 8.Apache的語言支援
-
- 1.php
- 2.cgi
- 3.wsgi
1.Apache的作用
在web被通路時通常使用http://的方式
http:// ##超文本傳輸協定
http:// 超文本傳輸協定提供軟體:
Apache
nginx
stgw
jfe
Tengine
2.Apache的安裝及啟用
dnf install httpd -y 安裝哈帕齊
firewall-cmd --permanent --add-service=http 在火牆中永久開啟http
firewall-cmd --permanent --add-service=https 在火牆中永久開啟https
firewall-cmd --reload 重新開機
systemctl enable --now httpd 打開httpd服務
vim /var/www/html/index.html 編輯檔案,然後可以在firefox中浏覽自己編輯的内容
3.Apache的基本資訊
服務名稱: httpd
配置檔案:
/etc/httpd/conf/httpd.conf ##主配置檔案
/etc/httpd/conf.d/*.conf ##子配置檔案
預設釋出目錄: /var/www/html
預設釋出檔案: index.html
預設端口: 80 #http
443 #https
使用者: apache
日志: /etc/httpd/logs
4.Apache的基本配置
1.Apache端口修改在檔案中改端口-預設為80:
vim /etc/httpd/conf/httpd.conf
Listen 8080
這裡我們改為8080
firewall-cmd --permanent --add-port=8080/tcp 防火牆允許8080端口通路
firewall-cmd --reload 重新加載
systemctl restart httpd 重新開機服務讀取
用火狐通路時:要指明端口
http://172.25.254.140:8080
2.預設釋出檔案
預設釋出檔案為/var/www/html/index.html
要改變預設釋出檔案則現在這個路徑下建立另外一個檔案xxx.html 這裡建立為westos.html
vim /etc/httpd/conf/httpd.conf
DirectoryIndex westos.html index.html 在/var/www/html 下建立一新的釋出檔案westos.html 在httpd.conf中調整優先級
systemctl restart httpd
3.預設釋出目錄
同理,要改變預設釋出目錄先建立一個目錄
mkdir /westos_apache
需要改變目錄的安全上下文:
ls -Zd /var/www/html/
semanage fcontext -a -t httpd_sys_content_t '/westos_apache(/.*)?'
restorecon -RvvF /westos_apache/
如圖:
vim /westos_apache/westos.html 編輯釋出檔案内容
vim /etc/httpd/conf/httpd.conf
在httpd配置檔案當中更改預設釋出目錄指向:
DocumentRoot "/westos_apache"
<Directory "/westos_apache">
Require all granted
</Directory>
systemctl restart httpd 重新開機服務
在火狐浏覽器通路,檢視效果
firefox http://172.25.254.140
6.Apache的通路控制
#實驗素材#
mkdir /var/www/html/westos
vim /var/www/html/westos/index.html
firefox http://172.25.254.140/westos
1.基于用戶端ip的通路控制
(1)ip白名單
ip白名單,隻讓指定ip通路
<Directory “/var/www/html/westos”>
Order Deny,Allow
Deny from All
Allow from 172.25.254.40
< /Directory>
如圖:
(2)ip黑名單
ip黑名單,隻拒絕指定ip的通路
<Directory “/var/www/html/westos”>
Order Allow,Deny
Allow from All
Deny from 172.25.254.40
< /Directory>
如圖:
2.基于使用者認證
使用者認證功可以加強通路的安全度
在/var/www/html/ 路徑下 編輯釋出檔案westos/index.html
vim /etc/httpd/conf/httpd.conf 改為預設通路家目錄
<Directory "/var/www/html/westos">
AuthUserfile /etc/httpd/.htpasswd ##指定認證檔案
AuthName "Please input your name and password" ##認證提示語
AuthType basic ##認證類型
Require user admin ##允許通過的認證使用者 2選1
# Require valid-user ##允許所有使用者通過認證 2選1
</Directory>
htpasswd -cm /etc/httpd/htpasswdfile user1 ##生成認證檔案
輸入使用者名和密碼
注意:
當/etc/httpd/htpasswdfile存在那麼在添加使用者時不要加-c參數否則會覆寫源檔案内容
htpasswd -cm /etc/httpd/htpasswdfile user1
htpasswd -m /etc/httpd/htpasswdfile admin
7.Apache的虛拟主機
在虛拟機中
mkdir -p /var/www/westos.com/{news,music}
echo "news.westos.com" >/var/www/westos.com/news/index.html
echo "music.westos.com" > /var/www/westos.com/music/index.html
|---->域名<-------|
vim /etc/httpd/conf.d/vhost.conf ##httpd子檔案中添加每一個連接配接的資訊
<VirtualHost _default_:80>
DocumentRoot "/var/www/html"
CustomLog logs/default.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName news.westos.com
DocumentRoot "/var/www/westos.com/news"
CustomLog logs/news.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName music.westos.com
DocumentRoot "/var/www/westos.com/music"
CustomLog logs/music.log combined
</VirtualHost>
在你要打開火狐的主機中進行解析
真機主機中 vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.254.140 www.westos.org music.westos.org news.westos.org
在浏覽器所在主機中
vim /etc/hosts
172.25.254.140 www.westos.com music.westos.ocm news.westos.com
測試:
firefox http://www.westos.com
firefox http://wenku.westos.com
firefox http://news.westos.com
8.Apache的語言支援
1.php
實驗前先在指定路徑建立号目錄檔案
mkdir /var/www/html/php
vim /var/www/html/php/index.php
内容如下:
<?php
phpinfo();
?>
dnf install php -y 下載下傳
systemctl restart httpd 重新開機
通路firefox: http://172.25.254.140/php/index.php
2.cgi
mkdir /var/www/html/cgi
vim /var/www/html/cgi/index.cgi
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;
給這個腳本加執行的權限:
chmod +x /var/www/html/cgi/index.cgi
安全上下文:
使cgi可以執行
semanage fcontext -a -t httpd_sys_script_exec_t '/var/www/html/cgi/index.cgi(/.*)?'
restorecon -RvvF /var/www/html/cgi/index.cgi
vim /etc/httpd/conf.d/vhosts.conf
<Directory "/var/www/html/cgidir">
Options +ExecCGI
AddHandler cgi-script .cgi
</Directory>
主機上解析cgi檔案 vim /etc/hosts
測試:
firefox http://172.25.254.140/cgi/index.cgi
3.wsgi
編寫wsgi的測試檔案
vim /var/www/html/wsgi/index.wsgi
def application(env, westos):
westos('200 ok',[('Content-Type', 'text/html')])
return [b'hello westos ahhahahahah!']
dnf install python3-mod_wsgi 下載下傳
systemctl restart httpd 重新開機
vim /etc/httpd/conf.d/vhost 編寫子配置檔案
<VirtualHost *:80>
ServerName wsgi.westos.org
WSGIScriptAlias / /var/www/html/wsgi/index.wsgi
</VirtualHost>
如圖: