本篇內容介紹了“CentOS7.2配置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] 安裝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
四、支持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.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
六、支持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
七、支持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>
瀏覽器訪問: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/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
十、配置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]例如,在目錄[/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]中添加的用戶回答
十二、基本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 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客戶端,從以下網站? ,下載后,安裝并啟動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
“CentOS7.2配置Apache服務實例分析”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。