CentOS 7.2配置Apache服務httpd(上)_Linux

來源:互聯網
上載者:User

一、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變成AllAllowOverride All# line 164: 添加只能使用目錄名稱訪問的檔案名稱DirectoryIndex index.html index.cgi index.php# add follows to the end# server's response header(安全性)ServerTokens Prod# keepalive is ONKeepAlive On[root@linuxprobe ~]# systemctl start httpd[root@linuxprobe ~]# systemctl enable httpd[3] 如果Firewalld正在運行,請允許HTTP服務。,HTTP使用80 / TCP[root@linuxprobe ~]# firewall-cmd --add-service=http --permanentsuccess[root@linuxprobe ~]# firewall-cmd --reloadsuccess[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] 安裝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.conf247: 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/perlprint "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 

四、支援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>

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

五、支援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.conf247: 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/rubyprint "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 

六、支援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.conf247: 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 pythonprint "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

七、支援Userdir

啟用userdir,使用者可以使用此設定建立網站

[1] 配置 httpd.[root@linuxprobe ~]# vi /etc/httpd/conf.d/userdir.conf# line 17: comment out#UserDir disabled# line 24: uncommentUserDir 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>

瀏覽器訪問:http://linuxprobe.org/~wang/,出現如下介面

八、設定虛擬機器主機

配置虛擬機器主機以使用多個網域名稱。
以下樣本在網域名稱為[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

九、建立SSL認證

建立自己的SSL認證。但是,如果您使用您的伺服器作為業務,最好購買和使用來自Verisigh的正式認證等。

[root@linuxprobe ~]# cd /etc/pki/tls/certcert.pem certs/  [root@linuxprobe ~]# cd /etc/pki/tls/certs/[root@linuxprobe certs]# make server.keyumask 77 ; \/usr/bin/openssl genrsa -aes128 2048 > server.keyGenerating 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.keyEnter pass phrase for server.key:writing RSA key[root@linuxprobe certs]# make server.csrumask 77 ; \/usr/bin/openssl req -utf8 -new -key server.key -out server.csrYou are about to be asked to enter information that will be incorporatedinto 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 blankFor 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' attributesto be sent with your certificate requestA challenge password []:  #預設An optional company name []:  #預設#[root@linuxprobe certs]# openssl x509 -in server.csr -out server.crt -req -signkey server.key -days 3650Signature oksubject=/C=CN/ST=Shanghai/L=Shanghai/O=LinuxProbe/OU=DevOps/CN=linuxprobe.org/emailAddress=root@linuxprobe.orgGetting Private key

十、配置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: 改變SSLProtocolSSLProtocol -All +TLSv1 +TLSv1.1 +TLSv1.2# line 100: 改成剛剛建立的server.crtSSLCertificateFile /etc/pki/tls/certs/server.crt# line 107: 改成剛剛建立的server.keySSLCertificateKeyFile /etc/pki/tls/certs/server.key[root@www ~]# systemctl restart httpd

[2] 如果Firewalld正在運行,請允許HTTPS服務。 HTTPS使用443 / TCP

[root@www ~]# firewall-cmd --add-service=https --permanentsuccess[root@www ~]# firewall-cmd --reloadsuccess

[3] 使用Web瀏覽器通過HTTPS從用戶端電腦訪問測試頁。下面的樣本是Fiorefix。顯示以下螢幕,因為認證是自己建立的,但它沒有ploblem,繼續下一步。

十一、啟用基本驗證

啟用基本驗證以限制特定網頁的訪問

[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 wangNew password: # set passwordRe-type new password: # confirmAdding 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]中添加的使用者回答

十二、基本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/,並與作業系統上的使用者進行身分識別驗證。

十三、使用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 newDavLockDB "/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 wangNew password:   # set passwordRe-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”。

[5]在“設定名稱”欄位中輸入任何名稱,並在“URI”欄位中輸入[伺服器名稱/ webdav目錄],並輸入使用者名稱和密碼

[7]配置添加如下,點擊它串連到伺服器。

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

[9] 到webdav目錄下建立測試目錄和檔案

[root@linuxprobe tmp]# cd /home/webdav/[root@linuxprobe webdav]# mkdir linuxprobe[root@linuxprobe webdav]# mkdir linuxcool[root@linuxprobe webdav]# touch vdevops.txt[root@linuxprobe webdav]# touch linuxcool.txt

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.