超詳細 值得收藏 linux CentOS 7 配置Apache服務【轉發+新增】


一、Apache簡介

  • Apache HTTP Server(簡稱Apache)是Apache軟件基金會的一個開放源代碼的網頁服務器軟件,可以在大多數電腦操作系統中運行,由於其跨平台和安全性(盡管不斷有新的漏洞被發現,但由於其開放源代碼的特點,漏洞總能被很快修補。因此總合來說,其安全性還是相當高的。)。被廣泛使用,是最流行的Web服務器軟件之一。它快速、可靠並且可通過簡單的API擴充,將Perl/Python等解釋器編譯到服務器中。
  • 軟件圖標 
    這里寫圖片描述

二、安裝Apache httpd

  • 安裝httpd以配置Web服務器, HTTP使用80 / TCP
[1] 安裝 httpd.
[root@linuxprobe ~]# yum -y install httpd
# 刪除默認歡迎頁面
[root@linuxprobe ~]# rm -f /etc/httpd/conf.d/welcome.conf
[2] 配置httpd,將服務器名稱替換為您自己的環境
[root@linuxprobe ~]# vi /etc/httpd/conf/httpd.conf
# line 86: 改變管理員的郵箱地址
ServerAdmin root@linuxprobe.org
# line 95: 改變域名信息
ServerName www.linuxprobe.org:80
# line 151: none變成All
AllowOverride All
# line 164: 添加只能使用目錄名稱訪問的文件名
DirectoryIndex index.html index.cgi index.php
# add follows to the end
# server's response header(安全性)
ServerTokens Prod
# keepalive is ON
KeepAlive On
[root@linuxprobe ~]# systemctl start httpd
[root@linuxprobe ~]# systemctl enable httpd
[3] 如果Firewalld正在運行,請允許HTTP服務。,HTTP使用80 / TCP
[root@linuxprobe ~]# firewall-cmd --add-service=http --permanent
success
[root@linuxprobe ~]# firewall-cmd --reload
success
[4] 創建一個HTML測試頁,並使用Web瀏覽器從客戶端PC訪問它。如果顯示以下頁面,是正確的
[root@linuxprobe ~]# vi /var/www/html/index.html
<html>
<body>
<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">
Welcome access LinuxProbe.org,This is Test Page!
</div>
</body>
</html>

 

這里寫圖片描述

三、支持Perl

  • 啟用CGI執行並使用Perl腳本
 1 [1] 安裝Perl.
 2 [root@linuxprobe ~]# yum -y install perl perl-CGI
 3 [2] 默認情況下,在“/var/www/cgi-bin”目錄下允許CGI。  
 4 可以使用Perl Scripts放在目錄下。然而,它下面的所有文件都被處理為CGI。
 5 # 下面的設置是CGI的設置
 6 [root@linuxprobe ~]# grep -n "^ *ScriptAlias" /etc/httpd/conf/httpd.conf
 7 247: ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
 8 [3] 如果你想允許在其他目錄中的CGI,配置如下。  
 9 例如,在“/var/www/html/cgi-enabled”中允許。
10 [root@linuxprobe ~]# vi /etc/httpd/conf.d/cgi-enabled.conf
11 # create new
12 # processes .cgi and .pl as CGI scripts
13 <Directory "/var/www/html/cgi-enabled">
14     Options +ExecCGI
15     AddHandler cgi-script .cgi .pl
16 </Directory>
17 [root@linuxprobe ~]# systemctl restart httpd
18 [4] 如果SELinux被啟用,並且允許CGI在不是像上面[3]的默認目錄下,更改規則如下。
19 [root@linuxprobe ~]# chcon -R -t httpd_sys_script_exec_t /var/linuxprobe/html/cgi-enabled
20 [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_script_exec_t /var/www/html/cgi-enabled
21 [5] 創建一個CGI測試頁面,並使用Web瀏覽器從客戶端PC訪問它。如果顯示以下頁面,說明配置正確。
22 [root@linuxprobe ~]# vi /var/www/html/cgi-enabled/index.cgi
23 #!/usr/bin/perl
24 print "Content-type: text/html\n\n";
25 print "<html>\n<body>\n";
26 print "<div style=\"width: 100%; font-size: 40px; font-weight: bold; text-align: center;\">\n";
27 print "CGI Test Page";
28 print "\n</div>\n";
29 print "</body>\n</html>\n";
30 [root@linuxprobe ~]# chmod 705 /var/www/html/cgi-enabled/index.cgi 
 

這里寫圖片描述

四、支持PHP

  • 配置httpd以使用PHP腳本
 1 [1] 安裝PHP.
 2 [root@linuxprobe ~]# yum -y install php php-mbstring php-pear
 3 [root@linuxprobe ~]# vi /etc/php.ini
 4 # line 878: 取消注釋,設置時區
 5 date.timezone = "Asia/Shanghai"
 6 [root@linuxprobe ~]# systemctl restart httpd
 7 
 8 [2] 創建一個PHP測試頁面,並使用Web瀏覽器從客戶端PC訪問它。如果顯示以下頁面,它是確定。
 9 [root@linuxprobe ~]# vi /var/www/html/index.php
10 <html>
11 <body>
12 <div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">
13 <?php
14    print Date("Y/m/d");
15 ?>
16 </div>
17 </body>
18 </html>
 

這里寫圖片描述

1 [3] 創建phpinfo測試頁,確認是都開啟php支持
2 [root@linuxprobe ~]# echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php

 

這里寫圖片描述

五、支持Ruby

  • 配置httpd以將Ruby腳本用作CGI
 1 [1] 安裝Ruby.
 2 [root@linuxprobe ~]# yum -y install ruby
 3 
 4 [2] 默認情況下,在“/var/www/cgi-bin”目錄下允許CGI。  
 5 可以使用Perl Scripts放在目錄下。然而,它下面的所有文件都被處理為CGI。
 6 # 下面的設置是CGI的設置
 7 [root@linuxprobe ~]# grep -n "^ *ScriptAlias" /etc/httpd/conf/httpd.conf
 8 247: ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
 9 
10 [3] 如果你想允許在其他目錄中的CGI,配置如下。  
11 例如,在“/var/www/html/cgi-enabled”中允許。
12 [root@linuxprobe ~]# vi /etc/httpd/conf.d/cgi-enabled.conf
13 # create new
14 # processes .rb as CGI scripts
15 <Directory "/var/www/html/cgi-enabled">
16     Options +ExecCGI
17     AddHandler cgi-script .rb
18 </Directory>
19 [root@linuxprobe ~]# systemctl restart httpd
20 
21 [4] 如果SELinux被啟用,並且允許CGI在不是像上面[3]的默認目錄下,更改規則如下。
22 [root@linuxprobe ~]# chcon -R -t httpd_sys_script_exec_t /var/www/html/cgi-enabled
23 
24 [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_script_exec_t /var/www/html/cgi-enabled
25 
26 [5]     Create a CGI test page and access to it from client PC with web browser. It's OK if following page is shown.
27 [root@linuxprobe ~]# vi /var/www/html/cgi-enabled/index.rb
28 
29 #!/usr/bin/ruby
30 print "Content-type: text/html\n\n"
31 print "<html>\n<body>\n"
32 print "<div style=\"width: 100%; font-size: 40px; font-weight: bold; text-align: center;\">\n"
33 print "Ruby Script Test Page"
34 print "\n</div>\n"
35 print "</body>\n</html>\n" 
36 [root@linuxprobe ~]# chmod 705 /var/www/html/cgi-enabled/index.rb 
 

這里寫圖片描述

六、支持Python

  • 啟用CGI執行並使用Python腳本
 1 [1] 安裝python.
 2 [root@linuxprobe ~]# yum -y install python
 3 
 4 [2] 默認情況下,在“/var/www/cgi-bin”目錄下允許CGI。  
 5 可以使用Perl Scripts放在目錄下。然而,它下面的所有文件都被處理為CGI。
 6 # 下面的設置是CGI的設置
 7 [root@linuxprobe ~]# grep -n "^ *ScriptAlias" /etc/httpd/conf/httpd.conf
 8 247: ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
 9 
10 [3] 如果你想允許在其他目錄中的CGI,配置如下。  
11 例如,在“/var/www/html/cgi-enabled”中允許。
12 [root@linuxprobe ~]# vi /etc/httpd/conf.d/cgi-enabled.conf
13 # create new
14 # processes .py as CGI scripts
15 <Directory "/var/www/html/cgi-enabled">
16     Options +ExecCGI
17     AddHandler cgi-script .py
18 </Directory>
19 [root@linuxprobe ~]# systemctl restart httpd
20 
21 [4] 如果SELinux被啟用,並且允許CGI在不是像上面[3]的默認目錄下,更改規則如下。
22 [root@linuxprobe ~]# chcon -R -t httpd_sys_script_exec_t /var/www/html/cgi-enabled
23 [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_script_exec_t /var/www/html/cgi-enabled
24 
25 [5]     Create a CGI test page and access to it from client PC with web browser. It's OK if following page is shown.
26 [root@linuxprobe ~]# vi /var/www/html/cgi-enabled/index.py
27 
28 #!/usr/bin/env python
29 
30 print "Content-type: text/html\n\n"
31 print "<html>\n<body>\n"
32 print "<div style=\"width: 100%; font-size: 40px; font-weight: bold; text-align: center;\">\n"
33 print "Python Script Test Page"
34 print "\n</div>\n"
35 print "</body>\n</html>\n" 
36 
37 [root@linuxprobe ~]# chmod 705 /var/www/html/cgi-enabled/index.py

 

這里寫圖片描述

7、支持Userdir

  • 啟用userdir,用戶可以使用此設置創建網站
 1 [1] 配置 httpd.
 2 [root@linuxprobe ~]# vi /etc/httpd/conf.d/userdir.conf
 3 # line 17: comment out
 4 #UserDir disabled
 5 # line 24: uncomment
 6 UserDir public_html
 7 # line 31 - 35
 8 
 9 <Directory "/home/*/public_html">
10     AllowOverride All
11 # change
12 
13     Options None
14 # change
15 
16     Require method GET POST OPTIONS
17 </Directory>
18 [root@linuxprobe ~]# systemctl restart httpd
19 
20 [2] 創建一個測試頁,使用普通用戶通過客戶端PC與Web瀏覽器和訪問它,如果顯示以下頁面,就是正確的
21 [cent@linuxprobe ~]$ mkdir public_html
22 
23 [cent@linuxprobe ~]$ chmod 711 /home/cent
24 
25 [cent@linuxprobe ~]$ chmod 755 /home/cent/public_html
26 
27 [cent@linuxprobe ~]$ vi ./public_html/index.html
28 
29 <html>
30 <body>
31 <div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">
32 UserDir Test Page
33 </div>
34 </body>
35 </html>
 

瀏覽器訪問:http://linuxprobe.org/~wang/,出現如下界面 
這里寫圖片描述

8、設置虛擬主機

  • 配置虛擬主機以使用多個域名。 
    以下示例在域名為[linuxprobe.org],虛擬域名為[virtual.host(根目錄[/home/wang/public_html]]的環境中設置。 
    必須為此示例設置Userdir的設置
 1 [1] 配置虛擬主機
 2 [root@linuxprobe ~]# vi /etc/httpd/conf.d/vhost.conf
 3 # for original domain
 4 
 5 <VirtualHost *:80>
 6    DocumentRoot /var/www/html
 7    ServerName www.linuxprobe.org
 8 </VirtualHost>
 9 # for virtual domain
10 
11 <VirtualHost *:80>
12    DocumentRoot /home/cent/public_html
13    ServerName www.virtual.host
14    ServerAdmin webmaster@virtual.host
15    ErrorLog logs/virtual.host-error_log
16    CustomLog logs/virtual.host-access_log combined
17 </VirtualHost>
18 [root@linuxprobe ~]# systemctl restart httpd
19 
20 [2]創建測試頁並使用Web瀏覽器從客戶端計算機訪問它。如果顯示以下頁面,則是正確的:
21 [cent@linuxprobe ~]$ vi ~/public_html/virtual.php
22 <html>
23 <body>
24 <div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">
25 Virtual Host Test Page
26 </div>
27 </body>
28 </html>
29 [3]如果訪問測試時看不到相應頁面,可通過下面命令進行測試:
30 [root@linuxprobe ~]# yum -y install elinks^C
31 [root@linuxprobe ~]# elinks http://www.virtual.host/virtual.php
 

9、創建SSL證書

  • 創建自己的SSL證書。但是,如果您使用您的服務器作為業務,最好購買和使用來自Verisigh的正式證書等。
 1 [root@linuxprobe ~]# cd /etc/pki/tls/cert
 2 cert.pem  certs/    
 3 [root@linuxprobe ~]# cd /etc/pki/tls/certs/
 4 [root@linuxprobe certs]# make server.key
 5 umask 77 ; \
 6 /usr/bin/openssl genrsa -aes128 2048 > server.key
 7 Generating RSA private key, 2048 bit long modulus
 8 ...............................................................+++
 9 ....................................................................................................+++
10 e is 65537 (0x10001)
11 Enter pass phrase:
12 Verifying - Enter pass phrase:
13 [root@linuxprobe certs]# openssl rsa -in server.key -out server.key
14 Enter pass phrase for server.key:
15 writing RSA key
16 [root@linuxprobe certs]# make server.csr
17 umask 77 ; \
18 /usr/bin/openssl req -utf8 -new -key server.key -out server.csr
19 You are about to be asked to enter information that will be incorporated
20 into your certificate request.
21 What you are about to enter is what is called a Distinguished Name or a DN.
22 There are quite a few fields but you can leave some blank
23 For some fields there will be a default value,
24 If you enter '.', the field will be left blank.
25 -----
26 Country Name (2 letter code) [XX]:CN    #國家后綴
27 State or Province Name (full name) []:Shanghai  #
28 Locality Name (eg, city) [Default City]:Shanghai  #
29 Organization Name (eg, company) [Default Company Ltd]:LinuxProbe  #公司
30 Organizational Unit Name (eg, section) []:DevOps  #部門
31 Common Name (eg, your name or your server's hostname) []:linuxprobe.org  #主機名
32 Email Address []:root@linuxprobe.org  #郵箱
33 
34 Please enter the following 'extra' attributes
35 to be sent with your certificate request
36 A challenge password []:    #默認
37 An optional company name []:    #默認
38 #
39 [root@linuxprobe certs]# openssl x509 -in server.csr -out server.crt -req -signkey server.key -days 3650
40 Signature ok
41 subject=/C=CN/ST=Shanghai/L=Shanghai/O=LinuxProbe/OU=DevOps/CN=linuxprobe.org/emailAddress=root@linuxprobe.org
42 Getting Private key

 

10、配置SSL

 1 [1] 配置SSL.
 2 [root@linuxprobe ~]# yum -y install mod_ssl
 3 [root@linuxprobe ~]# vi /etc/httpd/conf.d/ssl.conf
 4 # line 59: 取消注釋
 5 DocumentRoot "/var/www/html"
 6 # line 60: 取消注釋,定義域名
 7 ServerName linuxprobe.org:443
 8 # line 75: 改變SSLProtocol
 9 SSLProtocol -All +TLSv1 +TLSv1.1 +TLSv1.2
10 
11 # line 100: 改成剛剛創建的server.crt
12 SSLCertificateFile /etc/pki/tls/certs/server.crt
13 # line 107: 改成剛剛創建的server.key
14 SSLCertificateKeyFile /etc/pki/tls/certs/server.key
15 [root@www ~]# systemctl restart httpd
16 
17 [2] 如果Firewalld正在運行,請允許HTTPS服務。 HTTPS使用443 / TCP
18 [root@www ~]# firewall-cmd --add-service=https --permanent
19 success
20 [root@www ~]# firewall-cmd --reload
21 success
22 [3] 使用Web瀏覽器通過HTTPS從客戶端計算機訪問測試頁。下面的示例是Fiorefix。顯示以下屏幕,因為證書是自己創建的,但它沒有ploblem,繼續下一步。
 

這里寫圖片描述

11、啟用基本身份驗證

  • 啟用基本身份驗證以限制特定網頁的訪問
 1 [1]例如,在目錄[/var/www/html/auth-basic]下設置基本身份驗證設置。
 2  [root@linuxprobe ~]# vi /etc/httpd/conf.d/auth_basic.conf
 3 # 創建新配置文件
 4 <Directory /var/www/html/auth-basic>
 5     AuthType Basic
 6     AuthName "Basic Authentication"
 7     AuthUserFile /etc/httpd/conf/.htpasswd
 8     require valid-user
 9 </Directory>
10 # 添加用戶:使用“-c”創建新文件(僅為初始注冊添加“-c”選項)
11 [root@linuxprobe ~]# htpasswd -c /etc/httpd/conf/.htpasswd wang
12 
13 New password: # set password
14 
15 Re-type new password: # confirm
16 
17 Adding password for user wang
18 [root@linuxprobe ~]# systemctl restart httpd
19 [root@linuxprobe ~]# mkdir /var/www/html/auth-basic
20 
21 [root@linuxprobe ~]# vi /var/www/html/auth-basic/index.html
22 # create a test page
23 
24 <html>
25 <body>
26 <div style="width: 100%; font-size: 40px; font-weight: bold; text-align: wanger;">
27 Test Page for Basic Auth
28 </div>
29 </body>
30 </html>
31 
32 [2] 使用Web瀏覽器從客戶端計算機訪問測試頁。然后需要認證,如下所示作為設置,用在[1]中添加的用戶回答

這里寫圖片描述

[3] 訪問成功 
這里寫圖片描述

12、基本Auth + PAM

  • 限制特定網頁上的訪問,並使用OS用戶通過SSL連接進行身份驗證
 1 [1] 創建證書,請參照上文所述。
 2 [2] 例如,在[/var/www/html/auth-pam]目錄下設置Basic Auth。
 3 # install from EPEL
 4 [root@linuxprobe ~]# yum --enablerepo=epel -y install mod_authnz_external pwauth
 5 [root@linuxprobe ~]# vi /etc/httpd/conf.d/authnz_external.conf
 6 # add to the end
 7 
 8 <Directory /var/www/html/auth-pam>
 9     SSLRequireSSL
10     AuthType Basic
11     AuthName "PAM Authentication"
12     AuthBasicProvider external
13     AuthExternal pwauth
14     require valid-user
15 </Directory>
16 
17 [root@linuxprobe ~]# mkdir /var/www/html/auth-pam
18 
19 [root@linuxprobe ~]# vi /var/www/html/auth-pam/index.html
20 # create a test page
21 
22 <html>
23 <body>
24 <div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">
25 Test Page for PAM Auth
26 </div>
27 </body>
28 </html>
29 
30 [root@linuxprobe ~]# systemctl restart httpd
31 [3]  在客戶端上使用Web瀏覽器訪問測試頁面https://linuxprobe.org/auth-pam/,並與操作系統上的用戶進行身份驗證。
 

這里寫圖片描述
這里寫圖片描述

13、使用WebDAV

  • 下面是使用SSL連接配置WebDAV設置的示例
 1 [1] 創建證書,請參照上文所述
 2 [2] 例如,創建一個目錄[webdav],它使得可以僅通過SSL連接到WebDAV目錄。
 3 [root@linuxprobe ~]# mkdir /home/webdav
 4 [root@linuxprobe ~]# chown apache. /home/webdav
 5 [root@linuxprobe ~]# chmod 770 /home/webdav
 6 [root@linuxprobe ~]# vi /etc/httpd/conf.d/webdav.conf
 7 # create new
 8 DavLockDB "/tmp/DavLock"
 9 Alias /webdav /home/webdav
10 <Location /webdav>
11     DAV On
12     SSLRequireSSL
13     Options None
14     AuthType Basic
15     AuthName WebDAV
16     AuthUserFile /etc/httpd/conf/.htpasswd
17     <RequireAny>
18         Require method GET POST OPTIONS
19         Require valid-user
20     </RequireAny>
21 </Location>
22 
23 #  添加用戶:使用“-c”創建新文件(僅為初始注冊添加“-c”選項)
24 [root@linuxprobe ~]# htpasswd -c /etc/httpd/conf/.htpasswd wang
25 New password:     # set password
26 Re-type new password:
27 Adding password for user wang
28 # **注意:用戶wang的htpasswd已經創建過,不需要重復創建**
29 [root@linuxprobe ~]# systemctl restart httpd
30 
31 [3]  如果啟用了SELinux,請更改以下規則。  
32 [root@linuxprobe ~]# chcon -R -t httpd_sys_rw_content_t /home/webdav
33 [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_rw_content_t /home/webdav
34 
35 [4]  這是PC上的WebDAV客戶端的設置(Windows 10)。
36 下載“CarotDAV”,這是一個免費的WebDAV客戶端,從以下網站⇒ http://www.rei.to/carotdav_en.html ,下載后,安裝並啟動CarotDAV,然后顯示以下屏幕,單擊“文件”按鈕並選擇“WebDAV”。 

 

這里寫圖片描述

1 [5]在“設置名稱”字段中輸入任何名稱,並在“URI”字段中輸入[服務器名稱/ webdav目錄],並輸入用戶名和密碼
 
        

這里寫圖片描述

 

1 [7]配置添加如下,點擊它連接到服務器。

 

這里寫圖片描述

1 [8] waring顯示如下,它的SSL證書沒有安裝在您的電腦上,它沒有ploblem,點擊“忽略”,然后去下一步。

 

這里寫圖片描述

1 [9] 到webdav目錄下創建測試目錄和文件
2 [root@linuxprobe tmp]# cd /home/webdav/
3 [root@linuxprobe webdav]# mkdir linuxprobe
4 [root@linuxprobe webdav]# mkdir linuxcool
5 [root@linuxprobe webdav]# touch vdevops.txt
6 [root@linuxprobe webdav]# touch linuxcool.txt
 

這里寫圖片描述

 

尊重他人勞動成果,看到的原文地址:http://blog.csdn.net/wh211212/article/details/52982917


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM