ubuntu16.04lts禁止使用者透過IP訪問 [論壇 - Ubuntu基本設定]


正在瀏覽:   1 名遊客


 到底部   前一個主題   下一個主題  [無發表權] 請登錄或者註冊



ubuntu16.04lts禁止使用者透過IP訪問
會員一級
註冊日期:
2017/3/16 15:36
所屬群組:
已註冊使用者
等級: 1
HP : 0 / 11
MP : 2 / 207
EXP: 47
離線
各位好:
小弟最近剛從 14.04 LTS轉換至16.04 LTS。
我都會強制設定user不准透過ip訪問網站,所以在14.04 LTS中的apache2.conf寫入

<VirtualHost*:80>
ServerAdmin *****
DocumentRoot /var/www/
ServerName *****
</VirtualHost>
<VirtualHost*:80>
ServerName ****.com
DocumentRoot *****
</VirtualHost>
<VirtualHost*:80>
ServerName ****.com
DocumentRoot /var/www/
</VirtualHost>

升級至16.04LTS後發現這招不能用了,想請問各位是不是16.04 LTS有換另一種方式限制呢?

補充一下 只要加入之後重啟apache2都會出現以下錯誤:
failed because the control process exited with error code. See "systemctl status apache2.service" and "journalctl -xe" for details.

2017/6/6 15:04
應用擴展 工具箱
回覆: ubuntu16.04lts禁止使用者透過IP訪問
會員五級
註冊日期:
2012/4/22 10:50
所屬群組:
已註冊使用者
等級: 36
HP : 0 / 898
MP : 664 / 28692
EXP: 92
離線
fenghsinag 寫到:
各位好:
小弟最近剛從 14.04 LTS轉換至16.04 LTS。
我都會強制設定user不准透過ip訪問網站,所以在14.04 LTS中的apache2.conf寫入

<VirtualHost*:80>
ServerAdmin *****
DocumentRoot /var/www/
ServerName *****
</VirtualHost>
<VirtualHost*:80>
ServerName ****.com
DocumentRoot *****
</VirtualHost>
<VirtualHost*:80>
ServerName ****.com
DocumentRoot /var/www/
</VirtualHost>

升級至16.04LTS後發現這招不能用了,想請問各位是不是16.04 LTS有換另一種方式限制呢?

補充一下 只要加入之後重啟apache2都會出現以下錯誤:
failed because the control process exited with error code. See "systemctl status apache2.service" and "journalctl -xe" for details.



關於您提的需求「禁止使用者透過IP訪問」,我沒有實做過的經驗,所以也沒什麼相關設定的概念,
所以先提供我剛剛找資料的過程給您參考,以下找到文章的內容沒實際去測過,需要您自行確認測試。


我的環境是「Xubuntu 16.04 amd64 英文界面」

執行


$ dpkg -l apache2




顯示


Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
+++-=============================-===================-===================-=========
ii apache2 2.4.18-2ubuntu3.2 amd64 Apache HTTP Server



執行


$ apache2ctl -v



顯示


Server version: Apache/2.4.18 (Ubuntu)
Server built: 2017-05-05T16:32:00




所以要找「Apache 2.4.*」相關的文章。

然後用「apache2 deny ip」當關鍵字來查詢,
可以找到找到一篇官方的文件「Apache HTTP Server Version 2.4 Documentation / Access Control」。
剛剛的關鍵字,切到「所有中文網頁」,
可以找到一篇「Linux 技術手札 / Apache 限制 IP 存取」。


另外也可以使用「virtualhost apache 2.4」當關鍵字來查詢,

可以找到

* Apache Virtual Host documentation
* Name-based Virtual Host Support
* VirtualHost Examples
* ServerName Directive



以上提到的文章,我沒有實際去測試過,這部份就留給您去測試確認了,

也許您有嘗試出什麼心得,若您的時間和意願允許的話,可以分享出來,
提供給未來遇到相同問題的人,可以有個探索的起點。


感恩先!

================================================================================


關於

fenghsinag 寫到:
補充一下 只要加入之後重啟apache2都會出現以下錯誤:
failed because the control process exited with error code. See "systemctl status apache2.service" and "journalctl -xe" for details.



錯誤訊息的提示,我猜測也許是您的「apache2.conf」有錯誤,所以才產生的吧? <-- 純猜測。

實際來測試一下,將下面加入「/etc/apache2/apache2.conf」

fenghsinag 寫到:

<VirtualHost*:80>
ServerAdmin *****
DocumentRoot /var/www/
ServerName *****
</VirtualHost>
<VirtualHost*:80>
ServerName ****.com
DocumentRoot *****
</VirtualHost>
<VirtualHost*:80>
ServerName ****.com
DocumentRoot /var/www/
</VirtualHost>



然後執行


$ sudo /etc/init.d/apache2 restart



註:「restart (重新啟動)」 也可以改成 「reload (重新載入設定檔)」

顯示


[....] Restarting apache2 (via systemctl): apache2.serviceJob for apache2.service failed because the control process exited with error code. See "systemctl status apache2.service" and "journalctl -xe" for details.
failed!



執行


$ sudo service apache2 restart



顯示


Job for apache2.service failed because the control process exited with error code. See "systemctl status apache2.service" and "journalctl -xe" for details.



執行


$ sudo systemctl restart apache2.service



顯示


Job for apache2.service failed because the control process exited with error code. See "systemctl status apache2.service" and "journalctl -xe" for details.



執行


$ sudo apache2ctl restart



顯示


apache2: Syntax error on line 228 of /etc/apache2/apache2.conf: Expected </VirtualHost*:80> but saw </VirtualHost>
Action 'restart' failed.
The Apache error log may have more information.



而「systemctl status apache2.service」和「journalctl -xe」是指令,
您可以嘗試執行看看,看看會出現什麼訊息,

這跟「systemd」有關,一些討論案例,紀錄在「[索引] 開機流程」下面的「## init」那。

例如,可以執行下面指令


$ systemctl status apache2.service



對照原本的下法,執行下面指令


$ /etc/init.d/apache2 status



或是執行下面指令


$ service apache2 status




關於「systemctl」的用法,更多請參考

$ man systemctl


而「Apache」也有提供一個指令「apache2ctl」或「apachectl


執行


$ apache2ctl



顯示


Usage: /usr/sbin/apache2ctl start|stop|restart|graceful|graceful-stop|configtest|status|fullstatus|help
/usr/sbin/apache2ctl <apache2 args>
/usr/sbin/apache2ctl -h (for help on <apache2 args>)



例如,可以執行


$ apache2ctl status



若要瞭解「apache2」這個套件,有提供那些指令,可以執行下面指令


$ dpkg -L apache2 | grep bin | sort



顯示


/etc/apache2/conf-available/serve-cgi-bin.conf
/usr/lib/cgi-bin
/usr/sbin
/usr/sbin/a2disconf
/usr/sbin/a2dismod
/usr/sbin/a2dissite
/usr/sbin/a2enconf
/usr/sbin/a2enmod
/usr/sbin/a2ensite
/usr/sbin/a2query
/usr/sbin/apache2ctl
/usr/sbin/apachectl



若要瞭解「apache2」這個套件,有提供那些「Manpage」,可以執行下面指令


$ dpkg -L apache2 | grep '/man/man.*/' | sort



顯示


/usr/share/man/man1/a2query.1.gz
/usr/share/man/man8/a2disconf.8.gz
/usr/share/man/man8/a2dismod.8.gz
/usr/share/man/man8/a2dissite.8.gz
/usr/share/man/man8/a2enconf.8.gz
/usr/share/man/man8/a2enmod.8.gz
/usr/share/man/man8/a2ensite.8.gz
/usr/share/man/man8/apache2ctl.8.gz
/usr/share/man/man8/apachectl.8.gz



這個技巧紀錄在「這篇」,

而一些套件探索的討論案例,則是紀錄在「[索引] 套件操作實務」。


執行下面指令


$ apt-cache search apache



顯示


apache2 - Apache HTTP Server
apache2-bin - Apache HTTP Server (modules and other binary files)
apache2-data - Apache HTTP Server (common files)
apache2-dbg - Apache debugging symbols
apache2-dev - Apache HTTP Server (development headers)
apache2-doc - Apache HTTP Server (on-site documentation)
apache2-utils - Apache HTTP Server (utility programs for web servers)
...略...




執行下面指令


$ apt-cache showsrc apache2 | grep '^Binary:'



顯示


Binary: apache2, apache2-data, apache2-bin, apache2-utils, apache2-suexec-pristine, apache2-suexec-custom, apache2-doc, apache2-dev, apache2-dbg




執行下面指令


$ dpkg -l '*apache*'



顯示


Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
+++-=============================-===================-===================-======
ii apache2 2.4.18-2ubuntu3.2 amd64 Apache HTTP Server
un apache2-api-20120211 <none> <none> (no description available)
ii apache2-bin 2.4.18-2ubuntu3.2 amd64 Apache HTTP Server (modules and other binary files)
ii apache2-data 2.4.18-2ubuntu3.2 all Apache HTTP Server (common files)
ii apache2-dev 2.4.18-2ubuntu3.2 amd64 Apache HTTP Server (development headers)
ii apache2-doc 2.4.18-2ubuntu3.2 all Apache HTTP Server (on-site documentation)
un apache2-suexec-custom <none> <none> (no description available)
un apache2-suexec-pristine <none> <none> (no description available)
ii apache2-utils 2.4.18-2ubuntu3.2 amd64 Apache HTTP Server (utility programs for web servers)
un apache2.2-bin <none> <none> (no description available)
un apache2.2-common <none> <none> (no description available)
un dh-apache2 <none> <none> (no description available)
un libapache2-mod-apparmor <none> <none> (no description available)
un libapache2-mod-passenger <none> <none> (no description available)
ii libapache2-mod-php 1:7.0+35ubuntu6 all server-side, HTML-embedded scripting language (Apache 2 module)
un libapache2-mod-php5 <none> <none> (no description available)
un libapache2-mod-php5filter <none> <none> (no description available)
ii libapache2-mod-php7.0 7.0.18-0ubuntu0.16. amd64 server-side, HTML-embedded scripting language (Apache 2 module)
un libapache2-mod-phpfilter <none> <none> (no description available)
un mono-apache-server2 <none> <none> (no description available)
un mono-apache-server4 <none> <none> (no description available)




================================================================================

另外不見得要直接修改「/etc/apache2/apache2.conf」,

在「泛Ubuntu」的環境,
您也可以嘗試瞭解「/etc/apache2/conf-enabled」和「/etc/apache2/conf-available」的機制和用途,
跟這兩個資料夾相關的指令是「a2enconf」。


至於機制,您可以觀看「/etc/apache2/apache2.conf」開頭的註解說明。

在「/etc/apache2/apache2.conf」裡面其中也有一段指令如下,


# Include generic snippets of statements
IncludeOptional conf-enabled/*.conf




也可以閱讀「/usr/share/doc/apache2/README.Debian.gz 」的說明。

可以執行下面指令來閱讀


$ less /usr/share/doc/apache2/README.Debian.gz



而「less」可以改成「vi」或「view」或...

在「/etc/apache2/apache2.conf」,也可以找到下面兩段指令


# Include module configuration:
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf



也就是跟「/etc/apache2/mods-enabled/」和「/etc/apache2/mods-available/」這兩個資料夾相關
相關的指令則是「a2enmod」。



# Include the virtual host configurations:
IncludeOptional sites-enabled/*.conf



也就是跟「/etc/apache2/sites-enabled/」和「[color=blue]/etc/apache2/sites-available/」和這兩個資料夾相關
相關的指令則是「a2ensite」。


================================================================================

關於設定,也可以嘗試去瞭解「.htaccess」的用法。

================================================================================

一些討論的案例,紀錄在「[索引] Apache操作實務

以上提供參考

報告完畢


2017/6/6 16:11
應用擴展 工具箱
回覆: ubuntu16.04lts禁止使用者透過IP訪問
會員一級
註冊日期:
2017/3/16 15:36
所屬群組:
已註冊使用者
等級: 1
HP : 0 / 11
MP : 2 / 207
EXP: 47
離線
感謝這麼詳細的提供方向,這邊會在摸索一下!

2017/6/7 9:57
應用擴展 工具箱
回覆: ubuntu16.04lts禁止使用者透過IP訪問
會員五級
註冊日期:
2012/4/22 10:50
所屬群組:
已註冊使用者
等級: 36
HP : 0 / 898
MP : 664 / 28692
EXP: 92
離線
fenghsinag 寫到:
感謝這麼詳細的提供方向,這邊會在摸索一下!



歹勢拉,關於您原本提的這部份「禁止使用者透過IP訪問」我也沒相關設定的經驗,
目前不曉得要怎麼設定,才能達到您提的這個需求
所以也只能給您一些參考連結,剩下就靠您自行摸索了。

補充一下,

您也可以研究「/etc/apache2/sites-available/000-default.conf」這個檔案的設定,
裡面也有一些註解說明可以參考。



<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com

...略...




然後也可以使用「virtualhost apache 2.4」當關鍵字來查詢,
也可以找到一些官方文件「Apache Virtual Host documentation」。

上面「回覆#2」我有做了一些連結補充。

## Apache HTTP Server Version 2.4 Documentation

* Apache Core Features / ServerName Directive
* Apache Virtual Host documentation
* Name-based Virtual Host Support
* VirtualHost Examples


========================================================


以上提供參考

補充完畢



2017/6/7 14:44
應用擴展 工具箱
回覆: ubuntu16.04lts禁止使用者透過IP訪問
會員五級
註冊日期:
2012/4/22 10:50
所屬群組:
已註冊使用者
等級: 36
HP : 0 / 898
MP : 664 / 28692
EXP: 92
離線
剛用手機在查資料

用了「apache 禁止IP訪問」當關鍵字查詢,有查到一些文章,
尚未測試,先暫記,明天白天再來測試,確認是否可行。

* Apache設置禁止使用IP訪問網站的方法
* 配置 Apache 服務器禁止所有非法域名 訪問自己的服務器



2017/6/7 22:17
應用擴展 工具箱
回覆: ubuntu16.04lts禁止使用者透過IP訪問 (Apache 禁止IP直接訪問)
會員五級
註冊日期:
2012/4/22 10:50
所屬群組:
已註冊使用者
等級: 36
HP : 0 / 898
MP : 664 / 28692
EXP: 92
離線
參考上面「回覆 #5」找到的文章,提到的概念來設定,
來達到樓主提到的需求「Apache禁止使用者透過IP直接訪問」。


以下的測試的環境是「Xubuntu 16.04 amd64 英文界面」

原本預設已經有「/etc/apache2/sites-enabled/000-default.conf」這個設定。


===========================================================



產生一個新的檔案「/etc/apache2/sites-available/limit-ip.conf」(註: 檔案名稱沒有限定是limit-ip,可自訂)


$ sudo vi /etc/apache2/sites-available/limit-ip.conf




填入下面的內容(註: 下面的x.x.x.x請改成您Server的IP Address)


<VirtualHost *:80>
	ServerName x.x.x.x
	ServerAlias x.x.x.x

	<location />
		Order Deny,Allow
		Deny from all
	</location>
</VirtualHost>>



註: 上面這一段內容,也可以直接增加在「/etc/apache2/sites-enabled/000-default.conf」這個檔案裡面。


然後執行


$ sudo a2ensite limit-ip



或是執行


$ sudo a2ensite limit-ip.conf




就會產生「/etc/apache2/sites-enabled/limit-ip.conf」這個檔案
它是「symbolic link」到「/etc/apache2/sites-available/limit-ip.conf」這個檔案。

您可以執行下面指令確認


$ file /etc/apache2/sites-enabled/limit-ip.conf



顯示


/etc/apache2/sites-enabled/limit-ip.conf: symbolic link to ../sites-available/limit-ip.conf




然後執行下面指令,重起「Apache」。


$ sudo /etc/init.d/apache2 restart



註:「restart (重新啟動)」 也可以改成 「reload (重新載入設定檔)」




接下來用您的IP來觀看您的網頁


$ lynx x.x.x.x



或是


$ lynx http://x.x.x.x/



lynx 可以改成其他的瀏覽器,例如「firefox」,「chromium-browser」,「w3m」...。


也可以使用「curl」或「wget」之類的指令來測試


例如


$ curl http://x.x.x.x/



顯示


<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /
on this server.<br />
</p>
<hr>
<address>Apache/2.4.18 (Ubuntu) Server at x.x.x.x Port 80</address>
</body></html>




或是執行


$ curl -I http://x.x.x.x/



顯示


HTTP/1.1 403 Forbidden
Date: Thu, 08 Jun 2017 00:32:57 GMT
Server: Apache/2.4.18 (Ubuntu)
Content-Type: text/html; charset=iso-885




===========================================================

## 相關文件說明

* Ubuntu Server Guide / HTTPD - Apache2 Web Server


* Order Directive
* Deny Directive
* Allow Directive
* <Location> Directive
* ServerName Directive
* ServerAlias Directive


* Custom Error Responses
* ErrorDocument Directive


* Apache Module mod_rewrite

===========================================================

以上提供參考

報告完畢


2017/6/8 8:41
應用擴展 工具箱
回覆: ubuntu16.04lts禁止使用者透過IP訪問
會員一級
註冊日期:
2017/3/16 15:36
所屬群組:
已註冊使用者
等級: 1
HP : 0 / 11
MP : 2 / 207
EXP: 47
離線
謝謝各位給予的解答,目前最近還有點忙沒空去測試這區塊,哈哈

2017/6/16 9:13
應用擴展 工具箱


 [無發表權] 請登錄或者註冊


可以查看帖子.
不可發帖.
不可回覆.
不可編輯自己的帖子.
不可刪除自己的帖子.
不可發起投票調查.
不可在投票調查中投票.
不可上傳附件.
不可不經審核直接發帖.