尝试在浏览器中访问 Apache 2.4.7 Web 服务器时出现禁止 403 错误

K A*_*hir 10 apache2

当我使用localhost从同一台 Web 服务器 PC访问 Apache Web 服务器时,它显示 Apache2 Ubuntu 默认页面。

但是当我使用192.168.0.2访问 Apache Web 服务器时,它给出了 403 Forbidden 错误(Forbidden You don't have permission to access / on this server)。

网络服务器详情

  • Ubuntu 14.04 LTS
  • Apache 版本 2.4.7

所有权命令

www-data sudo adduser ftpuser www-data
sudo chown -R www-data:ftpuser /var/www
sudo chmod -R g+rwX /var/www
Run Code Online (Sandbox Code Playgroud)

etc/apache2/apache2.conf文件中

ServerName 192.168.0.2

<Directory/>
    AllowOverride All
    Require all granted
</Directory>
Run Code Online (Sandbox Code Playgroud)

etc/apache2/port.conf文件中

NameVirtualHost *:80
Listen *:80
Run Code Online (Sandbox Code Playgroud)

一个网站的虚拟主机

<VirtualHost *:80>
    ServerName mysite
    DocumentRoot /var/www/mysite
    <Directory /var/www/mysite>
        Options None FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>    
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

我需要在哪个地方做哪些设置?请帮忙...

pa4*_*080 9

1. 你应该像这样配置你的 /etc/hosts 文件

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...
Run Code Online (Sandbox Code Playgroud)

test-site第二个“本地主机”在哪里。并且my-hostname/etc/hostname.


2. 您应该定义并启用虚拟主机(VH):

有一个默认的 HTTP VH。它被放置在/etc/apache2/sites-available/. 文件名为000-default.conf. 你必须编辑它(你可以重命名它,如果你愿意,或者基于它制作一些其他的 .conf 文件),然后你必须启用它。

您可以通过创建“软符号链接”来手动启用它:

sudo ln -s /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-enabled/
Run Code Online (Sandbox Code Playgroud)

或者您可以使用名为a2ensite 的Apache2 工具,它的作用相同:

sudo a2ensite 000-default.conf
Run Code Online (Sandbox Code Playgroud)

假设有 3 个虚拟主机,启用了 SSL 和注册的私有域(例如 SOS.info):

/etc/apache2/sites-available/http.SOS.info.conf
/etc/apache2/sites-available/https.SOS.info.conf
Run Code Online (Sandbox Code Playgroud)

还有一个是为这个主题的目的而创建的:

/etc/apache2/sites-available/http.test-site.conf
Run Code Online (Sandbox Code Playgroud)

前 2 个 VH 的内容是:

$ 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>
Run Code Online (Sandbox Code Playgroud)

这将所有 HTTP 请求重定向到 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>
Run Code Online (Sandbox Code Playgroud)

这是 HTTPS VH。

这两个文件的内容可以放在一个文件中,但是在这种情况下它们的管理(a2ensite/ a2dissite)会比较困难。


第三个虚拟主机是为了我们的目的而创建的

$ 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>
Run Code Online (Sandbox Code Playgroud)

3. 使用此配置,您应该访问:

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
Run Code Online (Sandbox Code Playgroud)

在主要示例中,您应该访问和

http://test-site           # pointed to the directory /var/www/test-site
http://test-site.SOS.info  # which is allied to http://test-site
Run Code Online (Sandbox Code Playgroud)

尝试在 Web 浏览器中打开该站点,或者尝试(在终端中)使用以下命令:

$ curl -L http://test-site/index.html
$ curl -L http://test-site.SOS.info/index.html
Run Code Online (Sandbox Code Playgroud)

当然,你需要index.html在他们的 DocumentRoot 中有一些页面:)



由于迂腐,我将留下下一个笔记:)


4. 你需要正确配置`/etc/apache2/apache2.conf`。

ii 花一些时间来提高服务器的安全性是个好主意。这些手册是关于安全配置的:1st2nd在这里您可以获得免费的 SSL 证书。这些网站将帮助您检查您的进度:1st2nd

根据上面的安全手册/etc/apache2/apache2.conf文件必须看起来像:

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"
Run Code Online (Sandbox Code Playgroud)

5. 设置防火墙。

要允许/拒绝外部访问您的 Web 服务器,您可以使用UFW(简单防火墙):

sudo ufw allow http
sudo ufw allow https
Run Code Online (Sandbox Code Playgroud)

只允许使用tcp协议:

sudo ufw allow http/tcp
sudo ufw allow https/tcp
Run Code Online (Sandbox Code Playgroud)

您可以直接使用和端口号:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
Run Code Online (Sandbox Code Playgroud)

以防万一您可以重新加载“规则表”:

sudo ufw reload
Run Code Online (Sandbox Code Playgroud)

您可以使用 UFW 的 GUI 界面,称为gufw

sudo apt update
sudo apt install gufw
gufw &
Run Code Online (Sandbox Code Playgroud)

选择Office配置文件。它将为:Status:ONIncoming:DenyOutgoing:Allow添加你的规则。


6. 如果你有路由器,不要忘记转发一些端口:

如果您有一个路由器并且您希望您的 Web 服务器可以从 Internet 访问,请不要忘记添加一些端口转发。像这样的东西。