基於linux的APACHE(web服務,指定ip和指定使用者訪問,虛擬主機,https加密認證,網頁重寫),支援的語言(php,cgi),正向和反向代理
阿新 • • 發佈:2019-01-26
APACHE Web服務
yum install httpd -y ###安裝httpd
systemctl start httpd ###開啟httpd服務
/var/www/html ###apache預設配置目錄
index.html ###apache預設主配置檔案
cd /var/www/html
vim index.html ###apache預設主配置檔案
寫入:hello world
netstat -antlupe | grep httpd ###檢視httpd的埠
在網站進行測試172.25.254.117
netstat -antlupe | grep httpd ###檢視httpd的埠
vim /etc/http/conf/httpd.conf
將listen80改為listen8080
在網站測試172.25.254.117:8080
mkdir -p /westos/html
cd /westos/html
vim index.html
寫入:westos'page
vim /etc/http/conf/httpd.conf
把119行,DocumentRoot "/var/www/html"註釋掉
寫入:從120行寫入,DocumentRoot "/westos/html"
<Directory "/westos">
require all granted
</Directory>
在網站進行測試172.25 .254.101
上面配置寫完後,系統只會讀預設配置檔案index.html,若想讀test.html還需要在主配置檔案中修改
vim test.html
寫入:westos'test'page
vim /etc/http/conf/httpd.conf
systemctl restart httpd
在剛才寫的加入一行:DirectoryIndex test.html
在網站進行測試172.25.254.117
mkdir /westos/html/linux
cd /westos/html/linux
vim index.html
寫入:/westos/html/linux'page
vim /etc/http/conf/httpd.conf
寫入:在DocumentRoot "/westos/html"下面
<Directory "/westos/html/linux">
DirectoryIndex index.html
</Directory>
systemctl restart httpd
在網站進行測試172.25.254.117/linux
設定指定ip訪問預設釋出目錄和檔案
cd /var/www/html mkdir westos
vim westos/index.html
寫入:westos
vim /etc/httpd/conf/httpd.conf
註釋掉以前寫的後寫入
<Directory "/var/www/html/westos">
Order Allow,Deny ###順序執行,先執行Allow,後執行Deny
Allow from All
Deny from 172.25.254.70 ##禁止172.25.254.70訪問,相當於黑名單
</Directory>
在網站測試:訪問172.25.254.117
在172.25.254.117上測試
在172.25.254.70上測試
vim /etc/httpd/conf/httpd.conf修改之前寫的
<Directory "/var/www/html/westos">
Order Deny,Allow ########順序執行,先執行Deny後執行Allow
Allow from 172.25.254.70
Deny from All #####先禁止所有ip訪問,在允許172.25.254.70訪問,相當於白名單
</Directory>
再測試時,只有172.25.254.70,可以訪問
在172.25.254.70上測試
設定指定使用者訪問預設釋出目錄檔案
cd /etc/httpd
htpasswd -cm apacheuser admin ## c-->create 建立,檔案已經存在不用加 c
htpasswd -m apacheuser natasha
cat apacheuser ###檢視使用者是否建立
1:vim /etc/httpd/conf/httpd.conf
註釋掉以前寫的後寫入
<Directory "/var/www/html/westos">
AuthUserFile /etc/httpd/apacheuser
AuthName "Please input user and password !!"
AuthType basic
Require user admin #####只允許admin使用者訪問
</Directory>
1:vim /etc/httpd/conf/httpd.conf
註釋掉以前寫的後寫入
<Directory "/var/www/html/westos">
AuthUserFile /etc/httpd/apacheuser
AuthName "Please input user and password !!"
AuthType basic
Require valid-user ####允許所有使用者訪問
</Directory>
這樣之前建立的admin,natasha的使用者都可以登入
APACHE的虛擬主機
vim /etc/hosts (在哪裡測試就在哪裡寫本地解析,我是在虛擬機器中)
寫入:172.25.254.117 www.westos.com news.westos.com music.westos.com
cd /etc/httpd conf.d/
ls
vim default.conf
mkdir /var/www/virtual/westos.com/news -p
mkdir /var/www/virtual/westos.com/music -p
ls
vim /var/www/virtual/westos.com/news/index.html
寫入:news'page
vim /var/www/virtual/westos.com/music/index.html
寫入:music'page
175 vim news.conf
寫入:<VirtualHost *:80>
ServerName news.westos.com
DocumentRoot "/var/www/virtual/westos.com/news"
CustomLog "logs/news.log" combined
</VirtualHost>
<Directory "/var/www/virtual/westos.com/news">
Require all granted
</Directory>
cp news.conf music.conf
vim music.conf
寫入:<VirtualHost *:80>
ServerName music.westos.com
DocumentRoot "/var/www/virtual/westos.com/music"
CustomLog "logs/music.log" combined
</VirtualHost>
<Directory "/var/www/virtual/westos.com/music">
Require all granted
</Directory>
在網站分別測試:www.westos.com
news.westos.com
music.westos.com
https加密認證
yum install mod_ssl crypto-utils -y
genkey www.westos.com ###生成證書和鑰匙
vim /etc/httpd/conf.d/ssl.conf
在100行後寫:SLCertificateFile /etc/pki/tls/certs/www.westos.com.crt
在107行後寫:SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key
https://172.25.254.117
點選Add exception
點選get certificate下載證書,點選左下角
點選鎖子,再點選more information,再點選view certificate檢視證書資訊
apache網頁重寫
記得在vim /etc/hosts中寫入172.25.254.117 login.westos.com 本地解析
cd /etc/httpd/conf.d
ls
vim login.conf
寫入:
<VirtualHost *:443>
ServerName login.westos.com
DocumentRoot "/var/www/virtual/westos.com/login"
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/westos.com/login">
Require all granted
</Directory>
<VirtualHost *:80>
ServerName login.westos.com
RewriteEngine on
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
</VirtualHost>
mkdir -p /var/www/virtual/westos.com/login
vim /var/www/virtual/westos.com/login/index.html
寫入:login'page
systemctl restart httpd
在網站輸入login.westos.com會自動切換到https訪問
apache支援php語言,cgi語言
cd /var/www/html/
ls
yum install php -y
vim index.php
寫入:<?php
phpinfo();
?>
vim /etc/httpd/conf/httpd.conf
找到這一行,在裡面加上 index.php
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
在網站測試172.25.254.117
systemctl restart httpd
mkdir cgi
ls
vim cgi/index.cgi
寫入:#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`
chmod +x cgi/index.cgi
在網站測試172.25.254.101/cgi/index.cgi,發現不會執行結0果
239 cd /etc/httpd/conf.d/
240 ls
241 vim default.conf
寫入:<VirtualHost _default_:80>
DocumentRoot /var/www/html
CustomLog "logs/default.log" combined
</VirtualHost>
<Directory "/var/www/html/cgi">
Options +ExecCGI
AddHandler cgi-script .cgi
DirectoryIndex index.cgi
</Directory>
242 systemctl restart httpd
再在網站測試172.25.254.101/cgi/index.cgi,結果會顯示
搭建論壇
261 cd /var/www/html/
262 ls
263 lftp 172.25.254.250
下載Discuz_X3.2_SC_UTF8.zip壓縮包
264 ls
265 unzip Discuz_X3.2_SC_UTF8.zip ##解壓
在網站輸入172.25.254.101/upload ##安裝論壇
chmod 777 /var/www/html/upload/ -R ##解決許可權問題
268 yum search php
269 yum install php-mysql.x86_64 -y
270 systemctl restart httpd
論壇搭建完成:
正向代理
在真機上(可上網的):
ping www.baidu.com
yum install squid -y ###安裝squid squid
vim /etc/squid/squid.conf ###編輯/etc/squid/squid.conf檔案
systemctl restart squid
在虛擬機器中:
開啟網頁找到edit裡的preferences裡的Advanced裡的Network
點選settings進行設定
在沒網的虛擬機器中開啟網頁,就可以瀏覽百度
反向代理
在desktop虛擬機器(172.25.254.117)上安裝httpd
yum install httpd ##安裝httpd
cd /var/www/html
vim index.html ###編輯index.html檔案
寫入:172.25.254.101
在server虛擬機器中(172.25.254.217):
yum install squid -y ###安裝squid
systemctl start squid
clear
cd /etc/squid/
ls
vim squid.conf ##編輯/etc/squid/squid.conf檔案
更改第56,59,60, 63行取消註釋就好
56 http_access allow all
57
58 # Squid normally listens to port 3128
59 http_port 80 vhost vport
60 cache_peer 172.25.254.101 parent 80 0 proxy-only
61
62 # Uncomment and adjust the following to add a disk cache directory.
63 cache_dir ufs /var/spool/squid 100 16 256
systemctl restart squid
在真機中
在網站測試172.25.254.201,檢視結果