Apacheweb服務(wù)-創(chuàng)新互聯(lián)

第九單元

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比蘭考網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式蘭考網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋蘭考地區(qū)。費(fèi)用合理售后完善,10多年實(shí)體公司更值得信賴。

Apache web服務(wù)

一 Apache安裝

1 yum install httpd -y                  ###安裝apache軟件包###

2 systemctl start httpd                 ###開啟服務(wù)###

3 systemctl enable httpd.service        ###開機(jī)自動(dòng)開啟服務(wù)###

4 systemctl stop firewalld.service      ###關(guān)閉防火墻###

5 systemctl disable firewalld.service   ###開機(jī)自動(dòng)關(guān)閉###

6 netstat -antlp   | grep httpd         ###查看監(jiān)聽端口###

二 apache基本信息

1 apache的默認(rèn)發(fā)布目錄

index.html

2 apache的配置文件

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

ServerRoot "/etc/httpd"             ###用于指定Apache的運(yùn)行目錄###

Listen 80                                    ###監(jiān)聽端口###

User apache                         ###運(yùn)行apache程序的用戶和組###

Group apache

ServerAdmin root@localhost                   ###管理員郵箱###

DocumentRoot "/var/www/html"             ###網(wǎng)頁(yè)文件的存放目錄###

<Directory "/var/www/html">    ##<Directory>語句塊自定義目錄權(quán)限##

Require all granted

</Directory>

ErrorLog "logs/error_log"                ###錯(cuò)誤日志存放位置###

AddDefaultCharset UTF-8                  ###默認(rèn)支持的語言###

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

DirectoryIndex index.html                ###默認(rèn)主頁(yè)名稱###

/etc/httpd/conf.d/*.conf                  ###子配置文件###

3 apache的默認(rèn)發(fā)布目錄

/var/www/html

4 apache的默認(rèn)端口

80

Apache web服務(wù)

三 apache的基本配置

1 )默認(rèn)文件的修改

1 vim     /var/www/html/index.html           ###編寫默認(rèn)文件###

內(nèi)容:

<h2> hello world </h2>

2 vim     /var/www/html/ westos.html         ###編寫默認(rèn)文件###

內(nèi)容:

<h2> westos linux </h2>

3 vim     /etc/httpd/conf/httpd.con

168 <IfModule dir_module>

169     DirectoryIndex westos.html index.html   ###默認(rèn)westos.html為默認(rèn)文件,如果westos.html不存在,則默認(rèn)文件為index.html#####

170 </IfModule>

4  systemctl restart httpd.service      ###重新啟動(dòng)服務(wù)###

測(cè)試:

登入 172.25.254.112 查看顯示的內(nèi)容是index.html還是westos.html

 Apache web服務(wù)

如果將westos.html文件刪除

 Apache web服務(wù)

過程如下:

[root@mariadb mysqladmin]# cd /var/www/html/

[root@mariadb html]# ls

admin  cgi  mysqladmin

[root@mariadb html]# vim index.html

Apache web服務(wù)

[root@mariadb html]# vim westos.html

[root@mariadb html]# vim /etc/httpd/conf/httpd.conf

Apache web服務(wù)

[root@mariadb html]# systemctl restart httpd.service

[root@mariadb html]# rm -fr westos.html

2 )默認(rèn)目錄的修改

當(dāng)selinux是disabled的時(shí)候:

1 mkdir /westos/www/test -p   ###建立一個(gè)目錄作為默認(rèn)目錄###

2 vim /westos/www/test/westos.html   ###編寫默認(rèn)文件###

內(nèi)容:

<h2> westos's page </h2>

3 vim /etc/httpd/conf/httpd.conf

121 DocumentRoot "/westos/www/test"     ###修改默認(rèn)目錄###

122

123 <Directory "/westos/www/test">      ###設(shè)置默認(rèn)目錄訪問權(quán)限####

124     Require all granted            ####允許所有人訪問####

125 </Directory>

4  systemctl restart httpd.service    ###重啟服務(wù)###

測(cè)試:

登入172.25.254.112查看內(nèi)容:

Apache web服務(wù)

過程如下:

[root@mariadb html]# mkdir /westos/www/test -p

[root@mariadb html]# vim /westos/www/test/westos.html

Apache web服務(wù)

[root@mariadb html]# vim /etc/httpd/conf/httpd.conf

Apache web服務(wù)

[root@mariadb html]# systemctl restart httpd.service

當(dāng)selinux是enforcing狀態(tài):

在添加下面兩步:

1 semanage fcontext -a -t httpd_sys_content_t '/westos(/.*)?'###修改安全上下文###

2 restorecon -RvvF /westos/    ###刷新###

3 )apache的訪問控制

設(shè)定ip的訪問:

1 mkdir /var/www/html/admin/

2 vim /var/www/html/admin/index.html

<h2> admin's page </h2>

3 vim /etc/httpd/conf/httpd.conf

<Directory "/var/www/html/admin">

        Order Allow,Deny                 ###允許所有人訪問admin目錄但只有78主機(jī)不能訪問###

        Allow from All

        Deny from 172.25.254.78

</Directory>

<<Directory "/var/www/html/admin">     ###只允許78訪問訪問admin目錄####

        Order Deny,Allow

        Allow from 172.25.254.78

        Deny from All

</Directory>

3 systemctl restart httpd.service       ###重啟服務(wù)###

過程如下:

[root@mariadb ~]# mkdir /var/www/html/admin/

[root@mariadb ~]# vim /var/www/html/admin/index.html

Apache web服務(wù)

[root@mariadb ~]# vim /etc/httpd/conf/httpd.conf

Apache web服務(wù)

[root@mariadb ~]# systemctl restart httpd.service

特定用戶的訪問:

1 htpasswd -cm /etc/httpd/accessuser admin   ###設(shè)定用戶,-c指創(chuàng)建,-m指定名稱,改命令為創(chuàng)建了一個(gè)用戶admin,該用戶信息存放在/etc/httpd/accessuser#####

2 htpasswd -m /etc/httpd/accessuser tom    ###創(chuàng)建用戶tom,第二次創(chuàng)建不用加-c,不然會(huì)將第一次創(chuàng)建的用戶信息覆蓋###

3 vim /etc/httpd/conf/httpd.conf

<Directory "/var/www/html/admin">

    AuthUserFile /etc/httpd/accessuser      ###用戶認(rèn)證文件###

    AuthName "please input your name and password !!"  ###用戶認(rèn)證提示信息###

    AuthType basic              ###認(rèn)證類型###

    Require user tom            ###認(rèn)證用戶,只有tom可以訪問,如果是Require valid-user 則認(rèn)證文件中的所有用戶###

</Directory>

過程如下:

[root@mariadb ~]# vim /etc/httpd/conf/httpd.conf

Apache web服務(wù)

[root@mariadb ~]# cd /etc/httpd/

[root@mariadb httpd]# htpasswd -cm /etc/httpd/accessuser admin

New password:

Re-type new password:

Adding password for user admin

[root@mariadb httpd]# cat /etc/httpd/accessuser

admin:$apr1$/2PFvsol$SDJa/.mb1dmWnjHzZEPu11

[root@mariadb httpd]# htpasswd -m /etc/httpd/accessuser tom

New password:

Re-type new password:

Adding password for user tom

[root@mariadb httpd]# cat /etc/httpd/accessuser

admin:$apr1$/2PFvsol$SDJa/.mb1dmWnjHzZEPu11

tom:$apr1$pZ1snUMw$Sd/oscb2DOr0j6aCTvoDB1

[root@mariadb httpd]# mkdir -p  /var/www/html/admin

[root@mariadb httpd]# systemctl restart httpd.service

測(cè)試:

登入172.25.254.112/admin

 Apache web服務(wù)

4)apache語言支持

php  html  cqi

默認(rèn)支持:html

php語言:

1 yum   install  php   -y

2 vim /var/www/html/index.php

<?php

        phpinfo();

?>

3 systemctl restart httpd.service

過程如下:

[root@mariadb html]# yum install php

[root@mariadb html]# vim /var/www/html/index.html

Apache web服務(wù)

[root@mariadb html]# systemctl restart httpd.service

Apache web服務(wù)

cgi語言:

1 yum install httpd-manual -y

2 mkdir /var/www/html/cgi

3 cd /var/www/html/cgi/

4 vim index.cgi

#!/usr/bin/perl

print "Content-type: text/html\n\n";

print `date`;

5 chmod +x index.cgi

6 vim /etc/httpd/conf/httpd.conf

179     DirectoryIndex  index.html index.cgi

135 <Directory "/var/www/html/cgi">

136     Options +ExecCGI

137     AddHandler cgi-script .cgi

138 </Directory>

[root@mariadb httpd]# yum install httpd-manual -y

Apache web服務(wù)

[root@mariadb httpd]# systemctl restart httpd.service

[root@mariadb httpd]# mkdir /var/www/html/cgi

[root@mariadb httpd]# touch /var/www/html/cgi/index.cgi

[root@mariadb httpd]# cd /var/www/html/cgi/

[root@mariadb cgi]# vim index.cgi

Apache web服務(wù)

[root@mariadb cgi]# chmod +x index.cgi

[root@mariadb cgi]# vim /etc/httpd/conf/httpd.conf

Apache web服務(wù)

[root@mariadb cgi]# systemctl restart httpd.service

測(cè)試:

登入172.25.254.112/cgi

 Apache web服務(wù)

三 虛擬主機(jī)

1 ) 定義:

可以讓我們的一臺(tái)aoache服務(wù)器在被訪問不同域名的時(shí)候顯示不同的主頁(yè),虛擬主機(jī)允許您從一個(gè)httpd服務(wù)器同時(shí)為多個(gè)網(wǎng)站提供服務(wù)

2 )建立測(cè)試頁(yè):

cd /var/www/

mkdir virtual

mkdir virtual/news.westos.com

mkdir virtual/money.westos.com

mkdir virtual/money.westos.com/html

mkdir virtual/news.westos.com/html

echo "money.westos.com's page" > virtual/money.westos.com/html/index.html

echo "news.westos.com's page" > virtual/news.westos.com/html/index.html

3 )配置

1 cd /etc/httpd/conf.d/                   ##在子配置文件里配置

2 vim default.conf                         ###沒有指定域名的訪問都訪問default

<virtualhost    _default_:80>              ####虛擬主機(jī)開啟的端口####

        DocumentRoot "/var/www/html"        ###虛擬主機(jī)的默認(rèn)發(fā)布目錄###

        CustomLog "logs/default.log" combined  ###虛擬主機(jī)日志###

</Virtualhost>

3 vim news.conf                             ###指定域名為news.westos.com的訪問###

<Virtualhost *:80>

        ServerName "news.westos.com"          ###指定服務(wù)器名稱###

        DocumentRoot "/var/www/virtual/news.westos.com/html"

        CustomLog "logs/news.log" combined

</Virtualhost>

<Directory "/var/www/virtual/news.westos.com/html">    ###默認(rèn)發(fā)布目錄訪問授權(quán)#

        Require all granted

</Directory>

4 vim money.conf                            ###指定域名為money.westos.com的訪問###

<Virtualhost *:80>

        ServerName "money.westos.com"

        DocumentRoot "/var/www/virtual/money.westos.com/html"

        CustomLog "logs/money.log" combined

</Virtualhost>

<Directory "/var/www/virtual/money.westos.com/html">

        Require all granted

</Directory>

5 systemctl restart httpd.service    ###重啟服務(wù)###

6 在在瀏覽器的主機(jī)上要進(jìn)行本地解析:

[root@foundation12 Desktop]# vim /etc/hosts

172.25.254.112 www.westos.com news.westos.com money.westos.com

測(cè)試:

分別登入www.westos.com , new.westos.com , money.westos.com

 Apache web服務(wù)

Apache web服務(wù)

Apache web服務(wù)

過程如下:

[root@server ~]# cd /var/www/                        ###建立測(cè)試頁(yè)###

[root@server www]# mkdir virtual

[root@server www]# ls

cgi-bin  html  virtual

[root@server www]# mkdir virtual/news.westos.com

[root@server www]# mkdir virtual/money.westos.com

[root@server www]# mkdir virtual/money.westos.com/html

[root@server www]# mkdir virtual/news.westos.com/html

[root@server www]# echo "money.westos.com's page" > virtual/money.westos.com/html/index.html

[root@server www]# echo "news.westos.com's page" > virtual/news.westos.com/html/index.html

[root@server conf]# cd /etc/httpd/conf.d/    ##在子配置文件里配置###

[root@server conf.d]# ls

autoindex.conf  php.conf  userdir.conf

manual.conf     README    welcome.conf

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

 Apache web服務(wù)

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

Apache web服務(wù)

[root@server conf.d]# cp news.conf  money.conf

[root@server conf.d]# vim money.conf

Apache web服務(wù)

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

[root@server conf.d]# ls

autoindex.conf  manual.conf  news.conf  README        welcome.conf

default.conf    money.conf   php.conf   userdir.conf

[root@server conf.d]# systemctl restart httpd.service    ###重啟服務(wù)###

在瀏覽器的主機(jī)上要進(jìn)行本地解析:

[root@foundation12 Desktop]# vim /etc/hosts

172.25.254.112 www.westos.com news.westos.com money.westos.com

 Apache web服務(wù)

4 )配置https

http的訪問是明文的訪問,https的訪問是加密的訪問。

https訪問的監(jiān)聽端口是443

1 netstat -antlpe | grep httpd               ###查看與httpd有關(guān)的端口有哪些##

2 yum install mod_ssl -y                     ###只有安裝了ssl才會(huì)有443端口###

Apache web服務(wù)

3 vim /etc/httpd/conf.d/ssl.conf             ###安裝號(hào)ssl后就會(huì)出現(xiàn)ssl.conf文件,文件內(nèi)容有443端口####

4 yum install crypto-utils.x86_64 -y         ###安裝生成自簽名證書的軟件包###

5 genkey www.westos.com                      ###調(diào)用genkey生成證書###

--> 記錄生成的證書和關(guān)聯(lián)的私鑰的位置

Apache web服務(wù)

--> 選擇合適的密鑰大小

 Apache web服務(wù)

--> 在生成隨機(jī)數(shù)時(shí)比較慢,敲鍵盤和移動(dòng)鼠標(biāo)可以加速

 Apache web服務(wù)

--> 拒絕向認(rèn)證機(jī)構(gòu)(CA)發(fā)送證書請(qǐng)求(CSR)

 Apache web服務(wù)

--> 拒絕加密私鑰

 Apache web服務(wù)

--> 為服務(wù)器提供合適的身份。Common Name必須與服務(wù)器的主機(jī)全名完全匹配(注意,任何逗號(hào)都應(yīng)使用前導(dǎo)反斜線[\]進(jìn)行轉(zhuǎn)義)

 Apache web服務(wù)

-->

6 vim   ssl.conf

 Apache web服務(wù)

7 vim login.conf

<VirtualHost    *:443>                        ###https的監(jiān)聽端口###

        ServerName "login.westos.com"

        DocumentRoot "/var/www/virtual/login.westos.com/html"

        CustomLog "logs/login.log" combined

SSLEngine on                 ###開啟https的功能###

SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt  ###證書###

        SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key ##秘鑰###

</VirtualHost>

<Directory "/var/www/virtual/login.westos.com/html">

        Require all granted

</Directory>

過程如下:

[root@localhost conf.d]# yum install mod_ssl.x86_64 -y

[root@localhost conf.d]# yum install crypto-utils.x86_64 -y

[root@localhost conf.d]# genkey www.westos.com

/usr/bin/keyutil -c makecert -g 512 -s "CN=www.westos.com, OU=linux, O=westos, L=xi'an, ST=Shannxi, C=CN" -v 1 -a -z /etc/pki/tls/.rand.3946 -o /etc/pki/tls/certs/www.westos.com.crt -k /etc/pki/tls/private/www.westos.com.key

cmdstr: makecert

cmd_CreateNewCert

command:  makecert

keysize = 512 bits

subject = CN=www.westos.com, OU=linux, O=westos, L=xi'an, ST=Shannxi, C=CN

valid for 1 months

random seed from /etc/pki/tls/.rand.3946

output will be written to /etc/pki/tls/certs/www.westos.com.crt

output key written to /etc/pki/tls/private/www.westos.com.key

Generating key. This may take a few moments...

Made a key

Opened tmprequest for writing

/usr/bin/keyutil Copying the cert pointer

Created a certificate

Wrote 486 bytes of encoded data to /etc/pki/tls/private/www.westos.com.key

Wrote the key to:

/etc/pki/tls/private/www.westos.com.key

[root@localhost conf.d]# ls

autoindex.conf  money.conf  README    tmprequest    welcome.conf

default.conf    news.conf   ssl.conf  userdir.conf

[root@localhost conf.d]# vim ssl.conf

[1]+  Stopped                 vim ssl.conf

[root@localhost conf.d]# fg

vim ssl.conf

[1]+  Stopped                 vim ssl.conf

[root@localhost conf.d]# fg

vim ssl.conf

[root@localhost conf.d]# systemctl restart httpd.service

[root@localhost conf.d]# netstat -antlpe | grep httpd

tcp6       0      0 :::443                  :::*                    LISTEN      0          97088      4088/httpd

tcp6       0      0 :::80                   :::*                    LISTEN      0          97074      4088/httpd

[root@localhost conf.d]# cp -p money.conf login.conf

[root@localhost conf.d]# mkdir /var/www/virtual/login.westos.com/html -p

[root@localhost conf.d]# vim /var/www/virtual/login.westos.com/html/index.html

[root@localhost conf.d]# vim login.conf

<VirtualHost    *:443>                        ###https的監(jiān)聽端口###

        ServerName "login.westos.com"

        DocumentRoot "/var/www/virtual/login.westos.com/html"

        CustomLog "logs/login.log" combined

SSLEngine on                          ###開啟https功能###

SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt##證書##

        SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key##密鑰##

</VirtualHost>

<Directory "/var/www/virtual/login.westos.com/html">

        Require all granted

</Directory>

[root@localhost conf.d]# systemctl restart httpd.service

測(cè)試:登入login.westos.com

5)網(wǎng)頁(yè)重寫

1 vim login.conf

<Virtualhost *:443>

        ServerName "login.westos.com"

        DocumentRoot "/var/www/virtual/login.westos.com/html"

        CustomLog "logs/login.log" combined

        SSLEngine on

SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt

        SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key

</Virtualhost>

<Directory "/var/www/virtual/login.westos.com/html">

        Require all granted

</Directory>

<Virtualhost *:80>                  ###網(wǎng)頁(yè)重寫實(shí)現(xiàn)自動(dòng)訪問https###

        ServerName login.westos.com

        RewriteEngine on

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

</Virtualhost>

^(/.*)$                  ###客戶主機(jī)在地址欄中寫入的所有字符,除了換行符###

https://                 ###定向成為的訪問協(xié)議###

%{HTTP_HOST}             ###客戶請(qǐng)求主機(jī)###

$1                       ###指^(/.*)$的值###

[redirect=301]           ###301指臨時(shí)重定向,302指永久重定向###

2 systemctl restart httpd.service

測(cè)試:

在客戶主機(jī)中添加解析

172.25.254.112      login.westos.com

訪問http://login.westos.com 會(huì)自動(dòng)跳轉(zhuǎn)到

https://login.westos.com

Apache web服務(wù)

Apache web服務(wù)

Apache web服務(wù)

Apache web服務(wù)

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

本文名稱:Apacheweb服務(wù)-創(chuàng)新互聯(lián)
URL分享:http://muchs.cn/article40/cosgeo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、品牌網(wǎng)站制作網(wǎng)站設(shè)計(jì)公司、定制網(wǎng)站、云服務(wù)器、小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名