1. 程式人生 > >運維學習之Apache的配置、訪問控制、虛擬主機和加密訪問https

運維學習之Apache的配置、訪問控制、虛擬主機和加密訪問https

linux

apache

一、安裝Apache服務

yum install httpd -y

systemctl start httpd

systemctl stop firewalld

systemctl enable httpd

systemctl disable firewalld

二、apache信息

1.apache的默認發布文件

index.html

在默認發布文件內寫入要展示的內容,在瀏覽器中輸入本機http://172.25.254.126可以看到內容

技術分享

技術分享

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

修改/westos/html/westos文件內容

技術分享

因為selinux為enforcing,需要修改新建目錄的安全上下文,再刷新配置

技術分享

重啟服務後訪問到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>

order行按順序執行,前者優先級大於後者

技術分享

<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>

生成用戶認證密匙

技術分享

編輯配置文件如下

技術分享

設定admin用戶可以登陸

技術分享

設定admin1用戶不能登陸

技術分享

admin1不能登陸

技術分享

4.apache語言支持

php html cgi

html語言默認支持

php語言

vim index.php

<?php

phpinfo();

?>

yum install php -y

systemctl restart httpd

安裝php

技術分享

在默認發布目錄下添加index.php文件,寫入以下測試內容

技術分享

技術分享

測試成功!

技術分享


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

cgi的發布文件編輯

技術分享

給index.cgi加上可執行權限

技術分享

修改default.conf文件

技術分享

修改安全上下文

技術分享

測試成功!

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

創建news.westos.com和sports.westos.com目錄,編輯兩個發布文件如下

技術分享

技術分享

技術分享

技術分享

技術分享

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>

建立default.conf news.conf sports.conf三個文件作為訪問文件

技術分享

default.conf中的文件內容

技術分享

news.conf中的文件內容

技術分享

sports.conf中的文件內容

技術分享

重啟服務,在26主機中的/etc/hosts配置文件中寫入本地解析如下

技術分享

4.測試

在瀏覽器所在主機中

vim /etc/hosts

172.25.254.100 www.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

genkey www.westos.com 生成證書

技術分享

填寫信息技術分享

選擇不發送證書信息

技術分享

不加密密鑰

技術分享技術分享證書文件生成,檢查http端口443,確定防火墻可以通過端口

技術分享

測試,證書信息變為自己設定的內容

技術分享

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永久重定向 ##

## ##

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

配置文件中添加生成的證書和密鑰,配置各項和http加密訪問有關的項

技術分享

創建新的發布文件

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

在真機本地解析中加入login.westos.com

技術分享

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

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

技術分享

六、搭建簡易論壇(安裝包法)

安裝所需的mariadb數據庫,安裝php插件,安裝Apache,安裝php-mysql服務

技術分享

解壓壓縮包到/var/www/html下

技術分享

修改readme文件中要求的文件權限為777,開啟數據庫服務

技術分享

在瀏覽器中輸入172.25.254.126/upload/install進入安裝服務

技術分享

檢測全部通過

技術分享

填寫信息

技術分享

論壇安裝成功,可以用管理員身份進入論壇

技術分享












運維學習之Apache的配置、訪問控制、虛擬主機和加密訪問https