linux基礎3.9apache

1.安裝httpd服務

yum install httpd -y

yum install httpd-manual -y

systemctl start httpd 

systemctl enable httpd

ss -antlp |grep httpd    ##查看監聽端口

 

2.配置apache

/etc/httpd/conf/httpd.conf   ##apache主配置文件

ServerRoot "/etc/httpd"     ##用於指定Apache的運行目錄

Listen 80                    ##監聽端口

User apache                  ##運行apache程序的用戶和組

Group apache

DocumentRoot "/var/www/html" ##網頁文件的存放目錄

<Directory "/var/www/html"> <Directory>語句塊自定義目錄權限

Require all granted

</Directory>

ErrorLog "logs/error_log"   ##錯誤日誌存放位置

AddDefaultCharset UTF-8      ##默認支持的語言

IncludeOptional conf.d/*.conf ##加載其它配置文件

DirectoryIndex index.html    ##默認主頁名稱

 

3.虛擬主機

虛擬主機允許從一個httpd服務器同時爲多個網站提供服務。

[root@server4 ~]# cd /etc/httpd/conf.d/ 

[root@server4 conf.d]# vim default.conf

vim:

<Virtualhost _default_:80>  ##虛擬主機的塊

   DocumentRoot"/var/www/html"

   Customlog"log/defaule.log" combned

</Virtualhost>

<Directory "/var/www/html"> ##授權

    Require all granted

</Directory>

:wq

 

[root@server4 conf.d]# vim news.conf

vim:

<VirtualHost *:80>  ##虛擬主機的塊

   ServerName wwwX.example.com   ##指定服務器名稱

   ServerAlias serverX wwwXwwwX.example.com   ##用於匹配的空格分隔的名稱列表

   DocumentRoot /var/www/html  ##在<VirtualHost>塊內部,指定從中提供內容的目錄。

</VirtualHost>

<Directory "/var/www/html"> ##授權

    Require all granted

</Directory>

 

selinux標籤

semanage fcontext -l

semanage fcontext -a -t httpd_sys_content_t “/directory(/.*)?”

restorecon -vvFR /directory

 

4.Demo

建立網頁發佈目錄,並設置selinux標籤:

mkdir -p /srv/{default,www0.example.com}/www

echo "coming soon" > /srv/default/www/index.html

echo "www0" > /srv/www0.example.com/www/index.html

restorecon -Rv /srv/

 

創建虛擬主機配置文件:

cat /etc/httpd/conf.d/00-default-vhost.conf

 

<virtualhost _default_:80>

documentroot /srv/default/www

customlog "logs/default-vhost.log" combined

</virtualhost>

<directory /srv/default/www>

require all granted

</directory>

www.westos.org

cat 01-www0.example.com-vhost.conf

 

<virtualhost *:80>

servername www0.example.com

serveralias www0

documentroot /srv/www0.example.com/www

customlog "logs/www0.example.com.log" combined

</virtualhost>

<directory /srv/www0.example.com/www>

require all granted

</directory>

 

systemctl start httpd

systemctl enable httpd

 

5.配置基於用戶的身份驗證

示例:

用兩個賬戶創建Apache密碼文件:

[root@serverX ~]# htpasswd -cm /etc/httpd/.htpasswd bob

[root@serverX ~]# htpasswd -m /etc/httpd/.htpasswd alice

 

在Virtualhost塊中添加以下內容:

<Directory /var/www/html>

     AuthName “Secret Stuff”

     AuthType basic

     AuthUserFile/etc/httpd/.htpasswd

     Require valid-user

</Directory>

systemctl restart httpd

在Web瀏覽器上測試

 

6.自定義自簽名證書

  使用genkey實用程序(通過crypto-utils軟件包分發),生成自簽名證書及其關聯的私鑰。爲了簡化起見,genkey將在“正確”的位置(/etc/pki/tls目錄)創建證書及其關聯的密鑰。

yum install crypto-utils mod_ssl -y ##安裝服務

genkey server0.example.com   ##生成證書

在生成隨機數時比較慢,可以敲鍵盤和移動鼠標加快進度。

 

安裝證書及其私鑰

<1> 確保私鑰只被root用戶讀取

[root@server0 ~]# 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

<2> 編輯/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

<3> 重啓Web服務器。

[root@server0 ~]# systemctl restart httpd

<4> 使用https協議(https://serverX.example.com)通過Web客戶端(如Firefox

)訪問Web服務器。

Web客戶端可能會發出它不認可證書發行者的警告。這種情況適用自簽名證書。要求Web客戶端繞過證書認證。(對於Firefox,請選擇“I Understand the Risks” [我瞭解風險]、“Add Exception” [添加例外]和“Confirm Security Exception”[確認安全例外]。)

 

7.網頁重寫

所有80端口的請求全部重定向都由https來處理

<Virtualhost *:80>

    ServerName www0.example.com

    RewriteEngine on

    RewriteRule ^(/.*)$https://%{HTTP_HOST}$1 [redirect=301]

</Virtualhost>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章