《初入linux》--第二十部分-Apache服务器的几个实用技巧
生活随笔
收集整理的這篇文章主要介紹了
《初入linux》--第二十部分-Apache服务器的几个实用技巧
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最后新添加Apache HTTPS 的虛擬主機設置 和 網站重寫設置
檢測網站后臺技術
首先,linux自帶的curl命令可以查看一些網站的后臺服務器情況,其中包括
具體命令:
curl -I xxx.com ? ? ? //檢測某網站使用后臺技術
例:檢測www.baudu.com
[root@mazha1 桌面]# curl -I www.baidu.com HTTP/1.1 200 OK Server: bfe/1.0.8.18 Date: Thu, 08 Dec 2016 11:22:50 GMT Content-Type: text/html Content-Length: 277 Last-Modified: Mon, 13 Jun 2016 02:50:08 GMT Connection: Keep-Alive ETag: "575e1f60-115" Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform Pragma: no-cache Accept-Ranges: bytesApache具體使用方法介紹
1.安裝http服務
yum install httpd -y
2.默認web訪問目錄為
? /var/www/html
? 默認訪問文件為:
? index.html
3.主配置文件
/etc/httpd/conf/httpd.conf
注:查看apache幫助
安裝 yum?install httpd-mannul
4.在主配置文件中可以更改 默認訪問目錄,默認訪問文件和訪問端口
更改默認訪問目錄:
DocumentRoot "/www/html"更改默認訪問文件:
<IfModule dir_module>#DirectoryIndex index.htmlDirectoryIndex mypage.htmlindex.html test </IfModule>更改訪問端口:
#Listen 12.34.56.78:80 Listen 805.selinux設置
開啟selinux時,在更改默認訪問目錄后需要添加安全上下文標簽:
ls -Z??? 查看源目錄的安全上下文標簽
[root@server www]# ls -Z drwxr-xr-x. root root system_u:object_r:httpd_sys_script_exec_t:s0cgi-bin drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 html修改新目錄安全上下文標簽
[root@server www]# semanage fcontext -a -t httpd_sys_content_t'/www/html(/.*)?'6.拒絕或允許訪問名單:
<Directory "/www/html">Require all grantedorder Deny,Allow //order表示拒絕或允許讀取順序,有可能導致拒絕名單失效Deny from AllAllow from 172.25.254.2 </Directory>7.特定用戶訪問(帳號密碼登陸)
(1).修改配置文件
<Directory "/www/html">#Require all grantedAllowOverride All //打開認證功能Authuserfile /etc/httpd/httpdpwd //帳號信息文件Authname "給我一個吻~啊,可以不可以~" //提示信息Authtype basic //驗證方式#Require user mazha Require valid-user //允許訪問用戶列表 </Directory>(2)使用htpasswd命令創(chuàng)建賬戶文件
[root@server httpd]# htpasswd -cm httpdpwd mazha New password: Re-type new password: Adding password for user mazha[root@server httpd]# htpasswd -m httpdpwd mz New password: Re-type new password: Adding password for user mz
[root@server httpd]# cat httpdpwd mazha:$apr1$fAd3kKEj$fiZZopfPVnI3NYucER9z9/ mz:$apr1$UpdHVUIk$dTfUQ5ptc49/N0SNdAJgR/
(3)重啟服務
systemctl ? restart ? ?httpd
8.指定多個不同的默認目錄對應不同域名
? 1).修改主配置文件:
??
# Load config files in the"/etc/httpd/conf.d" directory, if any.IncludeOptional conf.d/*.conf //默認為開啟的? 2).在/etc/httpd/conf.d中配置默認目錄文件,以.conf結尾
? ?
[root@server conf.d]# cat index.conf <Virtualhost _default_:80>Documentroot /www/htmlCustomlog "logs/index.log"combined </Virtualhost> <Directory "/www/html">Require all granted </Directory>? 3).配置分默認目錄:
?read 目錄:
[root@server conf.d]# cat read.conf <Virtualhost *:80>Servername read.mazha.comDocumentroot/www/virt/read/htmlCustomlog"logs/read.log" combined </Virtualhost> <Directory "/www/virt/read/html">Require all granted </Directory>? music目錄:
[root@server conf.d]# cat music.conf <Virtualhost *:80>Servername music.mazha.comDocumentroot/www/virt/music/htmlCustomlog"logs/music.log" combined </Virtualhost> <Directory "/www/virt/music/html">Require all granted </Directory>? 4).添加dns
注:需要將分域名servername寫入DNS服務器或者本地/etc/hosts文件中?9.使用https協議并制作證書
? ?(1)需要安裝兩個服務:
? ? ?mod_ssl??? //使apache支持https協議
? ? ?crypto-utils? //生成ca證書
? ?(2)使用 genkey xxx.zzz.com?生成自己證書
按照提示信息填寫即可
? ?(3)配置/etc/httpd/conf.d/ssl.conf
? ?
SSLCertificateFile /etc/pki/tls/certs/www.mazha.com.crtSSLCertificateKeyFile /etc/pki/tls/private/www.mazha.com.key? ??使用剛生成的證書
? ?(4)重啟服務,瀏覽
10.虛擬主機的https設置:
首先virtulhost要打開443端口,
并且授權證書
設置如下:
vim /etc/httpd/conf.d/xxx.conf<Virtualhost *:80>Servername news.mazha.comDocumentroot/var/www/virt/news/htmlCustomlog"logs/news.log" combined </Virtualhost> <Directory "/var/www/virt/news/html">Require all granted </Directory> <Virtualhost *:443>Servername news.mazha.comDocumentroot/var/www/virt/news/htmlCustomlog "logs/news-443.log"combinedSSLEngine on SSLCertificateFile/etc/pki/tls/certs/www.mazha.com.crtSSLCertificateKeyFile/etc/pki/tls/private/www.mazha.com.key </Virtualhost>注:證書的制作參見上一部分
11.網頁重寫功能:
輸入http://www.xxx.com?進入https://www.xxx.com,更加安全
<Virtualhost *:80>Servername news.mazha.comRewriteengine onRewriterule ^(/.*)$https://%{HTTP_HOST}$1 [redirect=301] </Virtualhost> <Directory "/var/www/virt/news/html">Require all granted </Directory> <Virtualhost *:443>Servername news.mazha.comDocumentroot/var/www/virt/news/htmlCustomlog"logs/news-443.log" combinedSSLEngine onSSLCertificateFile/etc/pki/tls/certs/www.mazha.com.crtSSLCertificateKeyFile/etc/pki/tls/private/www.mazha.com.key </Virtualhost>
12.apache還默認可以識別php和cgi兩種文件
php:直接安裝php,重啟apache
默認文件為:index.php
cgi:
? 1)創(chuàng)建/var/www/html/cgi
? 查看幫助文檔,修改分配置文件:
??
? 2)修改/var/www/html/cgi安全上下文
?
semanage fcontext -a -t httpd_sys_script_exec_t '/var/www/html/cgi(/.*)?' restorecon -RvvF /var/www/html/cgi3) ?在/var/www/html/cgi下編寫cgi腳本
?
例:
4)腳本給可執(zhí)行權限:
chmod +x index.cgi
?
總結
以上是生活随笔為你收集整理的《初入linux》--第二十部分-Apache服务器的几个实用技巧的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 支配树
- 下一篇: linux firefox源码修改主页