Linux中Apache的管理及使用

Apache的作用

  • 在web被訪問時通常使用**http://**的方式
  • http:// 超文本傳輸協議
  • http:// 超文本傳輸協議提供軟件:
  • Apache
  • nginx
  • stgw
  • jfe
  • Tengine
    在這裏插入圖片描述

Apache的安裝

  • dnf install httpd.x86_64 -y
    在這裏插入圖片描述

Apache的啓動

  • systemctl enable --now httpd 開啓服務並設定服務位開機啓動
    在這裏插入圖片描述
  • firewall-cmd --list-all 查看火牆信息
  • firewall-cmd --permanent --add-service=http 在火牆中永久開啓http訪問
  • firewall-cmd --permanent --add-service=https 在火牆中永久開啓https訪問
  • firewall-cmd --reload 刷新火牆使設定生效
    在這裏插入圖片描述

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

Apache的基本配置

Apache端口修改

  • vim /etc/httpd/conf/httpd.conf
  • Listen 6666更改端口
  • netstat -antlupe | grep httpd 查看端口

在這裏插入圖片描述
在這裏插入圖片描述

  • 此時發現一個問題:將端口改爲8080後,無法訪問了,因爲防火牆沒有允許8080端口:
    在這裏插入圖片描述
  • firewall-cmd --permanent --add-port=8080/tcp添加8080端口
  • firewall-cmd --reload 重載火牆
    在這裏插入圖片描述
    在這裏插入圖片描述
  • 有一些端口設定之後會出現一些問題,比如設置Listen=6666
  • semanage port -l | grep http查看允許使用的端口
  • semanage port -a -t http_port_t -p tcp 8080添加允許使用的端口
  • systemctl restart httpd重啓服務
    在這裏插入圖片描述

默認發佈文件

  • vim /var/www/html/index.html(默認發佈文件)
  • 然後進去編輯,再用瀏覽器訪問,就可以看到編輯的頁面
    在這裏插入圖片描述
    在這裏插入圖片描述
  • vim /etc/httpd/conf/httpd.conf(更改默認發佈文件)
  • DirectoryIndex westos.html index.html
  • systemctl restart httpd
    在這裏插入圖片描述

默認發佈目錄

  • vim /etc/httpd/conf/httpd.conf
DocumentRoot "/westos/html"
<Directory "/westos/html">
 Require all granted
</Directory>//授權所有人可以訪問/westos/html

在這裏插入圖片描述

  • semanage fcontext -a -t httpd_sys_content_t '/westos(/.*)?'更改/westos的安全上下文,改成和默認目錄/var/www相同
  • restorecon -RvvF /westos/重載安全上下文
  • systemctl restart httpd 重啓服務
  • firefox http://192.168.1.21用瀏覽器訪問

Apache的訪問控制

  • 實驗素材
  • mkdir /var/www/html/westos
  • vim /var/www/html/westos/index.html
<h1>westosdir's page</h1>
  • firefox http://192.168.1.21/westos 訪問剛纔建立的文件夾
    在這裏插入圖片描述

1.基於客戶端ip的訪問控制

  • 設置ip白名單
<Directory "/var/www/html/westos">
 Order Deny,Allow
 Allow from 192.168.1.20
 Deny from All
</Directory>

在這裏插入圖片描述
-設置ip黑名單

<Directory "/var/www/html/westos">
 Order Allow,Deny
 Allow from All
 Deny from 192.168.1.20
</Directory>

在這裏插入圖片描述

2.基於用戶認證

  • vim /etc/httpd/conf/httpd.conf主配置文件
<Directory "/var/www/html/westos">
 AuthUserfile /etc/httpd/htpasswdfile ##指定認證文件(自己寫的)
 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 admin 生成認證文件,admin用戶是自己寫的
    注意:
  • /etc/httpd/htpasswdfile存在那麼在添加用戶時不要加-c參數,否則會覆蓋源文件內容
    在這裏插入圖片描述
  • 此時訪問192.168.1.21,需要用戶認證
    在這裏插入圖片描述

Apache的虛擬主機

  • mkdir -p /var/www/westos.com/{news,wenku}建立wenku news兩個目錄
  • vim /var/www/westos.com/wenku/index.html編輯wenku的發佈文件
wenku.westos.com's page
  • vim /var/www/westos.com/news/index.html編輯news的發佈文件
 news.westos.com's page
  • vim /var/www/html/index.html編輯默認的發佈文件
default westos's page

在這裏插入圖片描述

  • vim /etc/httpd/Vhost.conf
<VirtualHost _default_:80>
DocumentRoot "/var/www/html"
CustomLog logs/default.log combined 
</VirtualHost>
<VirtualHost *:80>
ServerName wenku.westos.com
DocumentRoot "/var/www/westos.com/wenku"
CustomLog logs/wenku.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName news.westos.com
DocumentRoot "/var/www/westos.com/news"
CustomLog logs/news.log combined
</VirtualHost>
  • 注意: customlog 後的logs/並不是 絕對路徑,而是相對路徑,本來是/etc/httpd/logs
    在這裏插入圖片描述
  • 測試:
  • 在瀏覽器所在主機中
  • vim /etc/hosts
192.168.1.21 www.westos.com wenku.westos.com news.westos.com
  • firefox http://www.westos.com
  • firefox http://wenku.westos.com
  • firefox http://news.westos.com
    在這裏插入圖片描述
    在這裏插入圖片描述
    在這裏插入圖片描述
  • 中途出現問題可以看apache的手冊
  • dnf install httpd-manual -y安裝手冊
  • firefox http://192.168.1.21/manual查看手冊,不過是全英文的,若看不懂的話,可直接在百度搜索apache中文手冊
    在這裏插入圖片描述
    在這裏插入圖片描述

Apache的語言支持

php

  • vim /var/www/html/index.php
<?php
	phpinfo();
?>#php的測試頁面

在這裏插入圖片描述

  • dnf install php -y安裝php
  • systemctl restart httpd 重啓httpd服務
  • firefox http://192.168.0.11/index.php
    在這裏插入圖片描述

cgi

  • mkdir /var/www/html/cgidir
  • vim /var/www/html/cgidir/index.cgi
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;##注意此時爲反單引號

在這裏插入圖片描述
在這裏插入圖片描述

  • vim /etc/httpd/conf.d/VirtHost.conf
<Directory "/var/www/html/cgidir">
Options +ExecCGI
AddHandler cgi-script .cgi
</Directory>

在這裏插入圖片描述

  • firefox http://192.168.0.11/cgidir/index.cgi
    在這裏插入圖片描述

Apache的加密訪問

  • openssl genrsa -out /etc/pki/tls/private/www.westos.com.key 2048 生成私鑰
  • 生成證書籤名文件
openssl req -new -key /etc/pki/tls/private/www.westos.com.key \ -out /etc/pki/tls/certs/www.westos.com.csr  ##生成證書籤名文件

在這裏插入圖片描述

  • 生成證書
openssl x509 -req -days 365 -in /etc/pki/tls/certs/www.westos.com.csr 
-signkey /etc/pki/tls/private/www.westos.com.key 
-out/etc/pki/tls/certs/www.westos.com.crt #生成證書

在這裏插入圖片描述

  • x509 證書格式
  • -req 請求
  • -in 加載簽證名稱
  • -signkey /etc/pki/tls/private/www.westos.com.key
  • vim /etc/httpd/conf.d/ssl.conf編輯證書文件
    在這裏插入圖片描述
  • firefox http://192.168.1.21 瀏覽器訪問並且查看證書
    在這裏插入圖片描述
  • vim /etc/httpd/conf.d/Vhost.conf
<VirtualHost *:80>
ServerName login.westos.com
RewriteEngine on
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 ##給用戶輸入的地址前面加上https://
</VirtualHost>
<VirtualHost *:443>
ServerName login.westos.com
DocumentRoot "/www/westos.com/login"
CustomLog logs/login.log combined
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt
SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key
</VirtualHost>
  • systemctl restart httpd
  • ^(/.*)$ 客戶地址欄中輸入的地址
  • %{HTTP_HOST} 客戶主機
  • $1 RewriteRule後面跟的第一串字符的值
    在這裏插入圖片描述
  • ***注意:***給路徑授權時可以在/etc/httpd/conf/httpd.conf,也可以在/etc/httpd/conf.d/Vhost.conf
  • 格式爲:
<Directory "路徑">
		Require all granted
</Directory>

Squid+Apache

squid 正向代理

  • 實驗環境:
  • 單網卡主機設定ip不能上網
  • 雙網卡主機設定ip1可以連接單網卡主機,設定ip2可以上網(ip和ip2網段相同,可以互通
  • 實驗效果:讓單網卡主機不能上網但瀏覽器可以訪問互聯網頁
  • 我設置的是單網卡主機IP=172.25.254.11
  • 雙網卡:IP1=192.168.1.30 IP2=172.25.254.13
  • 操作:
  • 在雙網卡主機中
  • dnf install squid -y
  • vim /etc/squid/squid.conf
59 http_access allow all
65 cache_dir ufs /var/spool/squid 100 16 256

在這裏插入圖片描述

  • systemctl restart squid
  • firewall-cmd --permanent --add-port=3128/tcp
  • firewall-cmd --reload
    在這裏插入圖片描述
  • 在單網卡專輯中選擇
  • NetWork Proxy(瀏覽器中設置)
  • 172.25.254.13 3128
    在這裏插入圖片描述
  • 測試:
  • 在單網卡主機中
  • ping www.baidu.com 不通
  • 在瀏覽器中訪問www.baidu.com可以

squid反向代理

  • 實驗環境:
  • 192.168.1.21 Apache服務器
  • 192.168.1.12 squid,沒有數據負責緩存
  • vim /etc/squid/squid.conf
http_port 80 vhost vport 
  • vhost 支持虛擬域名
  • vport 支持虛擬端口
  • 當192.168.1.30的80端口被訪問會從192.168.1.21的80端口緩存數據
cache_peer 192.168.1.21 parent 80 0 proxy-only

在這裏插入圖片描述

  • systemctl restart squid
  • 測試:
  • firefox http://192.168.1.30
  • 訪問看到的時192.168.1.21上的數據
    在這裏插入圖片描述
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章