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

当我从同一个Web服务器PC使用localhost访问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  AllowOverride All Require all granted  

etc / apache2 / port.conf文件中

 NameVirtualHost *:80 Listen *:80 

一个网站的虚拟主机

  ServerName mysite DocumentRoot /var/www/mysite  Options None FollowSymLinks AllowOverride None Require all granted   

我需要在哪个地方进行哪些设置? 请帮忙…

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是第二个“localhost”。 而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 

前2个VH的内容是:

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

  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  

这个将所有HTTP请求重定向到HTTPS。

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

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

这是HTTPS VH。

这两个文件的内容可以发布在一个文件中,但在这种情况下,他们的管理( a2ensite / a2dissite )将更加困难。


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

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

  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  # Allow .htaccess AllowOverride All Allow from All   

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 

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



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


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

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

根据以上安全手册/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  Options None FollowSymLinks AllowOverride None Require all denied   Options None FollowSymLinks AllowOverride None Require all granted  AccessFileName .htaccess  Require all denied  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  SecServerSignature "Apache 1.3.26"  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:Deny and Outgoing:Allow并添加您的规则。


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

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

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

 sudo chown -R www-data:www:data