Linux中Apacheweb服務

技術小甜發表於2017-11-17

一.apache的定義:企業中常用的web服務,用來提供http://(超文字傳輸協議)

二.apache的安裝部署

#安裝(配置好yum源)

yum install httpd -y
Linux中Apache web服務

yum install httpd-manual
Linux中Apache web服務

systemctl start httpd
Linux中Apache web服務

systemctl enable httpd
Linux中Apache web服務

systemctl stop firewalld.service
Linux中Apache web服務

systemctl disable firewalld.service 
Linux中Apache web服務

測試: http://172.25.254.160/
Linux中Apache web服務
http://172.25.254.160/manual/
Linux中Apache web服務

三.apache的基本資訊

主配置目錄: /etc/httpd/conf

主配置檔案: /etc/httpd/conf/httpd.conf

子配置目錄: /etc/httpd/conf.d/

子配置檔案: /etc/httpd/conf.d/.conf

預設釋出目錄: /var/www/html

預設釋出檔案: index.html

預設埠: 80

預設安全上下文:httpd_sys_content_t

程式開啟預設使用者:apache

apache日誌: /etc/httpd/logs/

檢視安全上下文: ls -Z /var/www/
Linux中Apache web服務

檢視埠:ss -anutlpe | grep httpd

預設埠為80
Linux中Apache web服務

修改預設埠:

vim /etc/httpd/conf/httpd.conf 

42 Listen 80 ##修改預設埠為8080
Linux中Apache web服務
Linux中Apache web服務

重啟服務
Linux中Apache web服務

修改後的埠為8080
Linux中Apache web服務

改回預設埠:80

修改配置檔案vim /etc/httpd/conf/httpd.conf 

42 Listen 80
Linux中Apache web服務
Linux中Apache web服務

重啟服務
Linux中Apache web服務

檢視埠資訊80
Linux中Apache web服務

修改預設釋出檔案:

120 DocumentRoot “/www/html”

121 <Directory “/www”>

122 Require all granted

123</Directory>
Linux中Apache web服務

cd /var/www/html

編輯預設釋出目錄vim index.html
Linux中Apache web服務
Linux中Apache web服務

測試:
Linux中Apache web服務

把預設釋出目錄名稱修改不能訪問
Linux中Apache web服務
Linux中Apache web服務

加上檔名才可訪問
Linux中Apache web服務

在配置檔案中寫入,當預設目錄為空時即訪問
Linux中Apache web服務

配置檔案中將預設目錄在前,預設先訪問前面的內容
Linux中Apache web服務

Linux中Apache web服務
Linux中Apache web服務
Linux中Apache web服務

測試:
Linux中Apache web服務
Linux中Apache web服務

預設釋出目錄裡寫入內容
Linux中Apache web服務

Linux中Apache web服務
Linux中Apache web服務
Linux中Apache web服務
Linux中Apache web服務

semanage fcontext -a -t httpd_sys_content_t `/www(/.*)?` ##更改安全下文

restorecon -RvvF /www/ ##重新整理
Linux中Apache web服務
Linux中Apache web服務

測試:
Linux中Apache web服務

四.apache的虛擬主機

[root@localhost conf.d]# cd /var/www/html/

[root@localhost html]# ls

index.html test.html

[root@localhost html]# vim index.html 

<h1> www.html.com <h2>

[root@localhost html]# mkdir /var/www/virtual/linux1.html.com/html -p

[root@localhost html]# mkdir /var/www/virtual/linux2.html.com/html -p

vim /var/www/virtual/linux1.html.com/html/index.html

<h1> linux1.html.com </h1>
Linux中Apache web服務

vim /var/www/virtual/linux1.html.com/html/index.html

<h1> linux1.html.com </h1>
Linux中Apache web服務

[root@localhost html]# mkdir /var/www/virtual/linux1.html.com/html -p

[root@localhost html]# mkdir /var/www/virtual/linux2.html.com/html -p
Linux中Apache web服務

vim /var/www/virtual/linux1.html.com/html/index.html

<h1> linux1.html.com </h1>

Linux中Apache web服務
Linux中Apache web服務

vim /var/www/virtual/linux2.html.com/html/index.html

<h1> linux2.html.com </h1>
Linux中Apache web服務
Linux中Apache web服務

[root@localhost conf.d]# vim /etc/httpd/conf.d/adefault.conf 

<VirtualHost default:80>

DocumentRoot “/var/www/html”

CustomLog “logs/www.html.com.log” combined

</VirtualHost>
Linux中Apache web服務
Linux中Apache web服務

[root@localhost conf.d]#vim /etc/httpd/conf.d/linux1.conf 

<VirtualHost :80>

ServerName linux1.html.com #指定站點名稱

DocumentRoot “/var/www/virtual/linux1.html.com/html/” #站點預設釋出目錄

CustomLog “logs/linux1.html.com.logs” combined

</VirtualHost>

<Directory “/var/www/virtual/linux1.html.com/html/”>

Require all granted

</Directory>
Linux中Apache web服務
Linux中Apache web服務

[root@localhost conf.d]# vim /etc/httpd/conf.d/linux2.conf 

<VirtualHost 
:80>

ServerName linux2.html.com

DocumentRoot “/var/www/virtual/linux2.html.com/html/”

CustomLog “logs/linux2.html.com.logs” combined

</VirtualHost>

<Directory “/var/www/virtual/linux2.html.com/html/”>

Require all granted

</Directory>

![](Linux中Apache web服務
Linux中Apache web服務

測試:

在測試機中做好本地解析

vim /etc/hosts

172.25.254.160 linux1.html.com linux2.html.com www.html.com html.com
Linux中Apache web服務
Linux中Apache web服務

測試:
Linux中Apache web服務
Linux中Apache web服務
Linux中Apache web服務

五.apache的內部訪問控制

1.針對與主機的訪問

2.[root@localhost conf.d]# mkdir /var/www/html/test

[root@localhost conf.d]# vim /var/www/html/test/index.html

<h1> hello world </h1>

Linux中Apache web服務
Linux中Apache web服務

測試:
Linux中Apache web服務

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

<VirtualHost default:80>

DocumentRoot “/var/www/html”

CustomLog “logs/www.html.com.log” combined

</VirtualHost>

<Directory “/var/www/html/test”>

Order deny,allow

Allow from all ##列表讀取順序,後讀取的內容會覆蓋先讀取內容的重複部分

Deny from 172.25.254.0/24

</Directory>
Linux中Apache web服務
Linux中Apache web服務

[root@localhost conf.d]# systemctl restart httpd.service 
Linux中Apache web服務

測試:

拒絕172.25.254.0/24

Linux中Apache web服務

2.使用者方式的訪問控制

[root@localhost conf.d]# htpasswd -cm /etc/httpd/userpass admin

New password: 

Re-type new password: 

Adding password for user admin

[root@localhost conf.d]# cat /etc/httpd/userpass 
Linux中Apache web服務
Linux中Apache web服務

[root@localhost conf.d]# htpasswd -m /etc/httpd/userpass admin1#再次新增使用者時,去掉c

New password: 

Re-type new password: 

Adding password for user admin1
Linux中Apache web服務

[root@localhost conf.d]# cat /etc/httpd/userpass

Linux中Apache web服務

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

rectory “/var/www/html/admin”>

AuthUserFile /etc/httpd/userpass

AuthName “Please input your name and password”

AuthType basic

Require user admin

</Directory>

Linux中Apache web服務

Linux中Apache web服務

[root@localhost conf.d]# mkdir /var/www/html/admin

[root@localhost conf.d]# vim /var/www/html/admin/index.html

<h1> admin </h1>
Linux中Apache web服務
Linux中Apache web服務

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

測試:

需要輸入密碼
Linux中Apache web服務

輸入密碼正確後
Linux中Apache web服務

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

<Directory “/var/www/html/admin”>

AuthUserFile /etc/httpd/userpass

AuthName “Please input your name and password”

AuthType basic

#Require user admin

Require valid-user

    #        </Directory>

Linux中Apache web服務
Linux中Apache web服務

[root@localhost conf.d]# systemctl restart httpd.service 
Linux中Apache web服務

六。apache支援的語言

1.html

2.php

[root@localhost html]# vim index.php

<?php

phpinfo();

?>

Linux中Apache web服務
Linux中Apache web服務

[root@localhost html]# yum install php
Linux中Apache web服務

[root@localhost conf.d]# vim /etc/httpd/conf/httpd.conf 
Linux中Apache web服務

Linux中Apache web服務

[root@localhost conf.d]# systemctl restart httpd.service 
Linux中Apache web服務

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

163 DirectoryIndex index.php index.html test.html 

測試:
Linux中Apache web服務

3.cgi

安裝的manual裡面有cgi語言的模板

[root@localhost cgi]# vim index.cgi

#!/usr/bin/perl

#print “Content-type: text/html

“;

#print “date“;
Linux中Apache web服務
Linux中Apache web服務

給該目錄加上可執行許可權
Linux中Apache web服務

[root@localhost cgi]# semanage fcontext -a -t httpd_sys_script_exec_t `/var/www/html/cgi(/.*)?` #更改安全上下文

[root@localhost cgi]# restorecon -RvvF /var/www/html/cgi/

Linux中Apache web服務

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

<Directory “/var/www/html/cgi”>

Options +ExecCGI

AddHandler cgi-script .cgi

</Directory>
Linux中Apache web服務
Linux中Apache web服務

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

測試:
Linux中Apache web服務

七.https

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

[root@localhost conf.d]# yum install crypto-utils.x86_64 
Linux中Apache web服務
Linux中Apache web服務
Linux中Apache web服務
Linux中Apache web服務

檢視安裝過程中生成了什麼檔案

rpm -ql crypto-utils.x86_64

生成認證genkey www.westos.com
Linux中Apache web服務

next

選擇1024位元組,加密方式
Linux中Apache web服務

填寫先關資訊獲得認證
Linux中Apache web服務

生成金鑰
Linux中Apache web服務

加密字元
Linux中Apache web服務
Linux中Apache web服務

vim /etc/httpd/conf.d/ssl.conf

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

108 SSLCertificatekey:wq

File /etc/pki/tls/private/www.westos.com.key
Linux中Apache web服務

把剛才獲得的認證寫入配置檔案
Linux中Apache web服務

Linux中Apache web服務

獲得證照
Linux中Apache web服務

八。設定https虛擬主機並設定網頁重寫

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

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

Linux中Apache web服務
Linux中Apache web服務

<h1> login.html.com </h1>

[root@localhost conf.d]# vim /etc/httpd/conf.d/login.conf

<VirtualHost :443>

ServerName login.html.com

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

CustomLog “logs/login.logs” 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/html/login.html.com/html”>

Require all granted

</Directory>

<VirtualHost 
:80>

ServerName login.html.com

RewriteEngine on

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

</VirtualHost>

^(/.)$ ##客戶在瀏覽器位址列中輸入的所有字元

https:// ##強制客戶加密訪問

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

$1 ##“$1”表示 ^(/.
)$的值

[redirect=301] ##臨時重寫,302永久轉換
Linux中Apache web服務
Linux中Apache web服務

測試:

輸入內容都能跳到https
Linux中Apache web服務

本文轉自Uniqueh51CTO部落格,原文連結: http://blog.51cto.com/13363488/2043565,如需轉載請自行聯絡原作者


相關文章