apache服務


anpache的安裝 

 

yum install httpd -y

systemctl start httpd

systemctl stop firewalld

systemctl enable httpd

systemctl disable firewalld

 


  apache信息  


 

1.apache的默認發佈文件

index.html

 

2.apache的配置文件

/etc/httpd/conf/httpd.conf

/etc/httpd/conf.d/*.conf

 

3.apache的默認發佈目錄

/var/www/html

 

4.apache的默認端口

80

 

################################

##### apache的基本配置 #########

################################

1.修改默認發佈文件

vim /etc/httpd/conf/httpd.conf

164     DirectoryIndex westos.html

systemctl restart httpd

2.修改默認發佈目錄

##當selinux是disable狀態

vim /etc/httpd/conf/httpd.conf

120 DocumentRoot "/westos/www/test"

<Directory "/westos/www/test">

        Require all granted

</Directory>

systemctl restart httpd

 

 

##當selinux是enforcing狀態

vim /etc/httpd/conf/httpd.conf

120 DocumentRoot "/westos/www/test"

<Directory "/westos/www/test">

        Require all granted

</Directory>

systemctl restart httpd

 

semanage fcontext -a -t httpd_sys_content_t '/westos(/.*)?'

restorecon -RvvF /westos

 

3.apache的訪問控制

##設定ip的訪問

vim /etc/httpd/conf/httpd.conf

<Directory "/var/www/html/admin">##允許所有人訪問admin目錄但是拒絕250主機

        Order Allow,Deny

        Allow from All

        Deny from 172.25.254.250

</Directory>

 

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

        Order Deny,Allow

        Allow from 172.25.254.250

        Deny from All

</Directory>

##設定用戶的訪問

htpasswd -m /etc/httpd/accessuser admin

vim /etc/httpd/conf/httpd.conf

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

        AuthUserFile /etc/httpd/accessuser##用戶認證文件

        AuthName "Please input your name and password !!"##用戶認證提示信息

        AuthType basic##認證類型

        Require valid-user##認證用戶,認證文件中所有用戶都可以通過

[Require user admin]##只允許認證文件中admin用戶訪問,二寫一

</Directory>

 

4.apache語言支持

php html cgi

 

html語言默認支持

 

php語言

vim index.php

<?php

phpinfo();

?>

yum install php -y

systemctl restart httpd

 

 

cgi語言

mkdir /var/www/html/cgi

vim index.cgi

#!/usr/bin/perl

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

print `date`;

 

vim /etc/httpd/conf/httpd.conf

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

Options +ExecCGI

AddHandler cgi-script .cgi

</Directory>

systemctl restart httpd

 

 

 

##################################

######## apache的虛擬主機 ########

##################################

 

1.定義

可以讓我們的一臺apache服務器在被訪問不同域名的時候顯示

不同的主頁

 

2.建立測試頁

mkdir  virtual/money.westos.com/html -p

mkdir  virtual/news.westos.com/html -p

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.配置

vim /etc/httpd/conf.d/default.conf##位指定域名的訪問都訪問default

<Virtualhost_default_:80>##虛擬主機開啓的端口

DocumentRoot "/var/www/html"##虛擬主機的默認發佈目錄

CustomLog "logs/default.log" combined##虛擬主機日誌

</Virtualhost>

 

vim /etc/httpd/conf.d/news.conf##指定域名news.westos.com的訪問到指定默認發佈目錄中

<Virtualhost *:80>

ServerName "news.westos.com"

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

CustomLog "logs/news.log" combined

</Virtualhost>

<Directory "/var/www/virtual/news.westos.com/html">##默認發佈目錄的訪問授權

Require all granted

</Directory>

 

4.測試

在瀏覽器所在主機中

vim /etc/hosts

172.25.254.100www.westos.com news.westos.com

 

###############################

##### 網頁加密訪問https #######

###############################

1.https定義

Hyper text transfer protocol over Secure socker layer

通過ssl

 

2.配置

yum install mod_ssl -y

yum install crypto-utils -y

genkey www.westos.com

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

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

vim /etc/httpd/conf.d/login.conf

<Virtualhost *:443>

        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>

<Virtualhost *:80>##網頁重寫實現自動訪問https

        ServerName login.westos.com

        RewriteEngine on

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

</Virtualhost>

 

##########################################################################

####

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

####

##^(/.*)$客戶主機在地址欄中寫入的所有字符,不包含換行符##

##https://定向成爲的訪問協議##

##%{HTTP_HOST}客戶請求主機##

##$1$1的值就表示^(/.*)$的值##

##[redirect=301]臨時重定向 302永久重定向##

####

##########################################################################

 

mkdir  /var/www/virtual/login.westos.com/html -p

vim /var/www/virtual/login.westos.com/html/index.html

 

systemctl restart httpd

 

測試:

在客戶主機中添加解析

vim /etc/hosts

172.25.254.100 login.westos.com

 

訪問http://login.westos.com 會自動跳轉到

https://login.westos.com 實現網頁數據加密傳輸

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章