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


9

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

但是,当我使用192.168.0.2访问Apache Web服务器时,它显示403 Forbidden错误(Forbidden您没有访问此服务器上的权限)。

Web服务器详细信息

  • 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

etc / apache2 / apache2.conf文件中

ServerName 192.168.0.2

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

etc / apache2 / port.conf文件中

NameVirtualHost *:80
Listen *:80

一个网站的虚拟主机

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

我需要在什么地方进行哪些设置?请帮忙...


ServerName 192.168.0.2由于ServerName指令应具有www.server.com之类的名称,而不是IP号,因此我会抛出一行。我认为这可以解决问题。对于ServerName,应输入服务器的名称(如果有)。ServerName允许基于名称的虚拟主机,从而允许在同一IP上拥有更多网站。
没人

@nobody,已将其从文件中删除,但仍然没有成功。
阿希尔

Answers:


8

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...

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/

或者,您可以使用称为a2ensite的Apache2工具,使之相同:

sudo a2ensite 000-default.conf

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

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

出于本主题的目的而创建的一个:

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

前两个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>

这将所有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>

这是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>

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

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

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

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

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

当然,您需要index.html在其DocumentRoot中包含一些页面:)



由于脚手架的原因,我将留下下一个音符:)


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

最好花一些时间来提高服务器的安全性。这些手册与安全性配置有关: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"

5.设置防火墙。

要允许/拒绝外部访问Web服务器,可以使用UFW(非复杂防火墙):

sudo ufw allow http
sudo ufw allow https

要仅允许使用tcp协议:

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

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

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

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

sudo ufw reload

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

sudo apt update
sudo apt install gufw
gufw &

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


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

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


/ etc / apache2 / sites-enabled /文件夹中已经存在000-default.conf文件。所以我还是应该使用上面的命令启用它吗?请告诉我。
阿希尔

如果已经存在,则无需使用它们。
pa4080

也许您会在中找到此错误的原因/var/log/apache2/error.log
pa4080

我已经更新了我的评论。
pa4080

正在获取此错误消息... [2016年8月12日星期五:[mpm_prefork:notice] [pid 4335] AH00169:捕获了SIGTERM,并关闭了[2016年8月12日星期五:17:18:40.679317] [mpm_prefork:notice] [pid 4571] AH00163:已配置Apache / 2.4.7(Ubuntu)PHP / 5.5.9-1ubuntu4.19-恢复正常运行[2016年8月12日星期五17:18:40.679382] [core:notice] [pid 4571] AH00094 :命令行: '/ usr / sbin目录/ Apache2的'
ķ阿尔

3

请使用以下命令更改提供文件的目录的所有权:

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

抱歉,我的问题没有提及,但我已经为/ var / www文件夹的特定组和用户分配了所有权。
阿希尔

0

我应该将您链接到解决我的问题的答案

首先,向该文件夹添加权限:

sudo chmod -R 775 /var/www

然后添加以下文本:

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

到此文件末尾:

/etc/apache2/sites-available/000-default.conf
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.