CentOS 7.2 配置Apache服務(httpd)--上篇

CentOS 7.2 配置Apache服務(httpd)--上篇

原創 2016年10月31日 15:39:36

1、Apache簡介

  • Apache HTTP Server(簡稱Apache)是Apache軟件基金會的一個開放源代碼的網頁服務器軟件,能夠在大多數電腦操做系統中運行,因爲其跨平臺和安全性(儘管不斷有新的漏洞被發現,但因爲其開放源代碼的特色,漏洞總能被很快修補。所以總合來講,其安全性仍是至關高的。)。被普遍使用,是最流行的Web服務器軟件之一。它快速、可靠而且可經過簡單的API擴充,將Perl/Python等解釋器編譯到服務器中。
  • 軟件圖標 
    這裏寫圖片描述

2、安裝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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

這裏寫圖片描述

3、支持Perl

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

這裏寫圖片描述

4、支持PHP

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

這裏寫圖片描述

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

這裏寫圖片描述

5、支持Ruby

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

這裏寫圖片描述

6、支持Python

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

這裏寫圖片描述

七、支持Userdir

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

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

八、設置虛擬主機

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

九、建立SSL證書

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

十、配置SSL

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

這裏寫圖片描述

十一、啓用基自己份驗證

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

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

十二、基本Auth + PAM

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

這裏寫圖片描述
這裏寫圖片描述

1三、使用WebDAV

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

這裏寫圖片描述

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

這裏寫圖片描述

相關文章
相關標籤/搜索