Bị cấm 403 khi cố gắng truy cập máy chủ web Apache 2.4.7 trong trình duyệt


9

Khi tôi truy cập máy chủ web Apache bằng localhost từ cùng một máy chủ web, nó sẽ hiển thị trang mặc định của Apache2 Ubuntu.

Nhưng khi tôi truy cập máy chủ web Apache bằng 192.168.0.2 , nó sẽ báo lỗi 403 Cấm (Cấm bạn không có quyền truy cập / trên máy chủ này).

Chi tiết máy chủ web

  • Ubuntu 14.04 LTS
  • Phiên bản Apache 2.4.7

Lệnh sở hữu

www-data sudo adduser ftpuser www-data
sudo chown -R www-data:ftpuser /var/www
sudo chmod -R g+rwX /var/www

Trong tập tin etc / apache2 / apache2.conf

ServerName 192.168.0.2

<Directory/>
    AllowOverride All
    Require all granted
</Directory>

Trong tập tin etc / apache2 / port.conf

NameVirtualHost *:80
Listen *:80

Máy chủ ảo cho một trang web

<VirtualHost *:80>
    ServerName mysite
    DocumentRoot /var/www/mysite
    <Directory /var/www/mysite>
        Options None FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>    
</VirtualHost>

Tôi cần cài đặt gì ở nơi nào? Xin vui lòng giúp đỡ...


Tôi sẽ ném ra ServerName 192.168.0.2dòng lệnh ServerName nên có tên như www.server.com chứ không phải số IP. Tôi nghĩ rằng điều này có thể giải quyết vấn đề. Đối với Tên máy chủ, bạn nên nhập tên của máy chủ nếu bạn có nó. ServerName cho phép lưu trữ ảo dựa trên tên, cho phép có nhiều trang web hơn trên cùng một IP.
không ai vào

@nobody, đã xóa nó khỏi tập tin nhưng vẫn không thành công.
K Ahir

Câu trả lời:


7

1. Bạn nên định cấu hình tệp / etc / hosts của mình như thế:

127.0.0.1   localhost
127.0.0.1   test-site
127.0.1.1   my-hostname
# The following lines are desirable for IPv6 capable hosts. etc...

Đâu test-sitelà "localhost" thứ hai. Và my-hostnamelà "Tên máy chủ hệ thống" được định nghĩa trong /etc/hostname.


2. Bạn nên xác định và kích hoạt Máy chủ ảo (VH):

Có một HTTP VH mặc định. Nó được đặt trong /etc/apache2/sites-available/. Tên tệp là 000-default.conf. Bạn phải chỉnh sửa nó (bạn có thể đổi tên nó, nếu bạn muốn hoặc tạo một số tệp .conf khác, dựa trên nó) và sau đó bạn phải kích hoạt nó.

Bạn có thể kích hoạt thủ công thông qua việc tạo "liên kết tượng trưng, ​​mềm mại":

sudo ln -s /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-enabled/

Hoặc bạn có thể sử dụng công cụ Apache2 có tên a2ensite , công cụ này cũng tương tự:

sudo a2ensite 000-default.conf

Giả sử có 3 Máy chủ ảo , SSL được kích hoạt và tên miền riêng đã đăng ký (ví dụ: SOS.info):

/etc/apache2/sites-available/http.SOS.info.conf
/etc/apache2/sites-available/https.SOS.info.conf

Và một cái được tạo ra cho mục đích của chủ đề này:

/etc/apache2/sites-available/http.test-site.conf

Nội dung của 2 VH đầu tiên là:

$ cat /etc/apache2/sites-available/http.SOS.info.conf

<VirtualHost *:80>    
    ServerName SOS.info
    ServerAlias www.SOS.info
    ServerAdmin admin@SOS.info

    # Redirect Requests to SSL
    Redirect permanent "/" "https://SOS.info/"

    ErrorLog ${APACHE_LOG_DIR}/http.SOS.info.error.log
    CustomLog ${APACHE_LOG_DIR}/http.SOS.info.access.log combined       
</VirtualHost>

Điều này chuyển hướng tất cả các yêu cầu HTTP đến HTTPS.

$ cat /etc/apache2/sites-available/https.SOS.info.conf

<IfModule mod_ssl.c>    
    <VirtualHost _default_:443>    
        ServerName SOS.info
        ServerAlias www.SOS.info
        ServerAdmin admin@SOS.info

        DocumentRoot /var/www/html  

        SSLEngine on    
        SSLCertificateFile /etc/ssl/certs/SOS.info.crt
        SSLCertificateKeyFile /etc/ssl/private/SOS.info.key
        SSLCertificateChainFile /etc/ssl/certs/SOS.info.root-bundle.crt
        #etc..
    </VirtualHost>    
</IfModule>

Đây là HTTPS VH.

Nội dung của hai tệp này có thể được đăng trong một tệp, nhưng trong trường hợp này, việc quản lý ( a2ensite/ a2dissite) của chúng sẽ khó khăn hơn.


Máy chủ ảo thứ ba là cái được tạo ra cho mục đích của chúng tôi :

$ cat /etc/apache2/sites-available/http.test-site.conf

<VirtualHost *:80>
    ServerName test-site
    ServerAlias test-site.SOS.info

    DocumentRoot /var/www/test-site
    DirectoryIndex index.html

    ErrorLog ${APACHE_LOG_DIR}/test-site.error.log
    CustomLog ${APACHE_LOG_DIR}/test-site.access.log combined

    <Directory /var/www/test-site>
        # Allow .htaccess 
        AllowOverride All
        Allow from All
    </Directory>    
</VirtualHost>

3. Với cấu hình này, bạn nên truy cập:

http://localhost     # pointed to the directory of the mine Domain 
https://localhost    # iin our case: /var/www/html (SOS.info), but you should get an error, because the SSL certificate

http://SOS.info      # which redirects to https://SOS.info
https://SOS.info     # you should have valid SSL certificate

http://www.SOS.info  # which is allied to http://SOS.info and redirects to https://SOS.info
https://www.SOS.info # which is allied to https://SOS.info

Trên ví dụ chính bạn nên truy cập và :

http://test-site           # pointed to the directory /var/www/test-site
http://test-site.SOS.info  # which is allied to http://test-site

Cố gắng mở trang web trong trình duyệt web hoặc chỉ thử (trong thiết bị đầu cuối) bằng các lệnh tiếp theo:

$ curl -L http://test-site/index.html
$ curl -L http://test-site.SOS.info/index.html

Tất nhiên, bạn cần phải có một số index.htmltrang trong DocumentRoot của họ :)



Tôi sẽ để lại ghi chú tiếp theo bởi lý do của sư phạm :)


4. Bạn cần cấu hình đúng `/ etc / apache2 / apache2.conf`.

Ii là ý tưởng tốt để dành thời gian để cải thiện bảo mật máy chủ của bạn. Các hướng dẫn này là về cấu hình bảo mật: 12 . Tại đây bạn có thể nhận được chứng chỉ SSL miễn phí. Các trang web này sẽ giúp bạn kiểm tra tiến trình của bạn: thứ 1thứ 2 .

Theo hướng dẫn bảo mật /etc/apache2/apache2.conftập tin ở trên phải trông giống như:

Mutex file:${APACHE_LOCK_DIR} default

PidFile ${APACHE_PID_FILE}

Timeout 60

#KeepAlive Off
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5

HostnameLookups Off

ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn

IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf

Include ports.conf

<Directory />
    Options None FollowSymLinks 
    AllowOverride None
    Require all denied
</Directory>

<Directory /var/www/>
    Options None FollowSymLinks 
    AllowOverride None
    Require all granted
</Directory>

AccessFileName .htaccess
<FilesMatch "^\.ht">
    Require all denied
</FilesMatch>

LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

IncludeOptional conf-enabled/*.conf
IncludeOptional sites-enabled/*.conf

# Hide Server type in the http error-pages 
ServerSignature Off
ServerTokens Prod

# Etag allows remote attackers to obtain sensitive information 
FileETag None

# Disable Trace HTTP Request
TraceEnable off

# Set cookie with HttpOnly and Secure flag.
# a2enmod headers
Header edit Set-Cookie ^(.*)$ $1;HttpOnly;Secure

# Clickjacking Attack
Header always append X-Frame-Options SAMEORIGIN

# CX-XSS Protection
Header set X-XSS-Protection "1; mode=block"

# Disable HTTP 1.0 Protocol
RewriteEngine On
RewriteCond %{THE_REQUEST} !HTTP/1.1$
RewriteRule .* - [F]

# Change the server banner @ ModSecurity 
# Send full server signature so ModSecurity can alter it
ServerTokens Full
# Alter the web server signature sent by Apache
<IfModule security2_module>
    SecServerSignature "Apache 1.3.26"
</IfModule>
Header set Server "Apache 1.3.26"
Header unset X-Powered-By

# Hde TCP Timestamp
#   gksu gedit /etc/sysctl.conf
#   >> net.ipv4.tcp_timestamps = 0
# Test: sudo hping3 SOS.info -p 443 -S --tcp-timestamp -c 1

# Disable -SSLv2 -SSLv3 and weak Ciphers
SSLProtocol all -SSLv2 -SSLv3
SSLHonorCipherOrder on
SSLCipherSuite "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA EECDH EDH+aRSA !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS !RC4"

5. Thiết lập Tường lửa.

Để cho phép / từ chối truy cập bên ngoài vào máy chủ web của bạn, bạn có thể sử dụng UFW (Tường lửa không biến đổi):

sudo ufw allow http
sudo ufw allow https

Để chỉ cho phép tcpsử dụng giao thức:

sudo ufw allow http/tcp
sudo ufw allow https/tcp

Bạn có thể sử dụng và số cổng trực tiếp:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

Chỉ trong trường hợp bạn có thể tải lại "bảng quy tắc":

sudo ufw reload

Bạn có thể sử dụng và giao diện GUI của UFW, được gọi là gufw .

sudo apt update
sudo apt install gufw
gufw &

Chọn Officehồ sơ. Nó sẽ thiết lập: Status:ON, Incoming:DenyOutgoing:Allowvà thêm các quy tắc của bạn.


6. Nếu bạn có bộ định tuyến đừng quên chuyển tiếp một số cổng:

Nếu bạn có bộ định tuyến và bạn muốn máy chủ web của mình có thể truy cập được từ Internet , đừng quên thêm một số chuyển tiếp cổng. Một cái gì đó như thế này .


Tệp 000-default.conf đã có trong thư mục / etc / apache2 / sites-enable /. Vì vậy, tôi vẫn nên kích hoạt nó bằng cách sử dụng lệnh trên? Làm ơn cho tôi biết.
K Ahir

Nếu nó đã có sẵn, bạn không cần phải sử dụng chúng.
pa4080

Có thể bạn sẽ tìm thấy những lý do của lỗi này /var/log/apache2/error.log.
pa4080

Tôi đã cập nhật bình luận của tôi.
pa4080

Nhận thông báo lỗi này ... [Thứ Sáu ngày 12 tháng 8 17: 18: 37.224182 2016] [mpm_prefork: thông báo] [pid 4335] AH00169: bị bắt SIGTERM, tắt [Thứ Sáu 12 17: 18: 40.679317 2016] [mpm_prefork: thông báo] [pid 4571] AH00163: Apache / 2.4.7 (Ubuntu) PHP / 5.5.9-1ubfox4.19 được định cấu hình - tiếp tục các hoạt động bình thường [Thứ Sáu ngày 12 tháng 8 17: 18: 40.679382 2016] [lõi: thông báo] [pid 4571] AH00094 : Dòng lệnh: '/ usr / sbin / apache2'
K Ahir

3

Vui lòng thay đổi quyền sở hữu thư mục nơi bạn đang phục vụ các tệp của mình bằng cách sử dụng lệnh:

sudo chown -R www-data:www:data <directory_where_you_serve_files_from>

Xin lỗi vì không đề cập đến trong câu hỏi của tôi nhưng tôi đã chỉ định quyền sở hữu cho nhóm và người dùng cụ thể cho thư mục / var / www.
K Ahir

0

Tôi phải liên kết bạn với câu trả lời này , nơi giải quyết vấn đề của tôi.

Trước hết, thêm quyền vào thư mục:

sudo chmod -R 775 /var/www

Sau đó thêm văn bản này:

<Directory /var/www/html>
  AllowOverride All
</Directory>

Đến cuối tập tin này:

/etc/apache2/sites-available/000-default.conf
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.