在ubuntu 16.04中修改apache首页根目录及其他常见操作

一、修改根目录

(一)、如果有默认设置则:(enabled 启用)
打开/etc/apache2/sites-enabled/000-default.conf 找到 DocumentRoot 修改成相应的目录即可 ,比如 /var/www/html/你的目录;当然这里面也可以设置http跳转至https
(二)、如果无默认设置则:(available可用)
打开/etc/apache2/sites-available/000-default.conf 找到 DocumentRoot 修改成相应的目录即可 ,比如 /var/www/html/你的目录

二、修改首页文件名称

打开 /etc/apache2/mods-available/dir.conf 其中 DirectoryIndex
是则是设置首页文件名称的,比如 index.html index.cgi index.pl index.php index.xhtml index.htm 等 。其优先顺序按排序先后来。

三、修改完后记得重启:sudo service apache2 restartsystemctl restart apache2

四、其他常见操作

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at [no address given] to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

1、查看日志 tail -f /var/log/apache2/error.log
2、出现 If you are using a DSO version of mod_proxy, make sure the proxy submodules are included in the configuration using LoadModule 错误,使用 sudo a2enmod proxy_http 可解决
3、
Invalid command 'ProxyRequests', perhaps misspelled or defined by a module not included in the server configuration 运行一下
a2enmod proxy再重启即可
4、node.js 服务增加域名配置
在 /etc/apache2/sites-enabled 新建xxx.conf 文件,在文件里写入如下文件:

<VirtualHost *:80>
    ServerName xxx.com(即你的域名)
    ServerAlias xxx.com (即你的域名)
    ProxyRequests Off
      <Proxy *> 
        Order deny,allow
        Allow from all
       </Proxy>
   <Location />
    ProxyPass http://127.0.0.1:xxxx/  (即你的端口)
    ProxyPassReverse http://127.0.0.1:xxxx/ (即你的端口)
    </Location>
</VirtualHost>

5、查看apache版本
apachectl -v


Server version: Apache/2.4.37 (Ubuntu)
Server built:   2018-10-28T15:00:00

6、Apache 2.4统一增加响应头

https://httpd.apache.org/docs/current/mod/mod_headers.html

/etc/apache2/apache2.conf (httpd.conf跟这个是一样的) 增加如下


 <IfModule mod_headers.c>
 Header always set Referrer-Policy "no-referrer"
 </IfModule>

7、Apache 2.4 反向代理某个网站

# 启用代理模块
a2enmod proxy proxy_balancer  proxy_http 

/etc/apache2/sites-enabled 新建 取的易识别名字.conf

<VirtualHost *:80>
    #SSLEngine On
    SSLProxyEngine On (一定要开启 否则会报SSLProxyEngine 不可以错误)
    ServerName xxx.com(你自己的域名)
    ServerAlias xxx.com(你自己的域名)
    ProxyRequests Off (off表示开启反向代理,on表示开启正向代理)
      <Proxy *>
        Order deny,allow
        Allow from all
       </Proxy>
   <Location />
    ProxyPass https://search.yahoo.com/ (你想要代理的网站)
    ProxyPassReverse https://search.yahoo.com/ (你想要代理的网站)
    </Location>

    <IfModule mod_headers.c>
  Header  set Content-Security-Policy  "frame-ancestors *"  (设置或修改请求头)
 </IfModule>

</VirtualHost>

如果要支持https,记得复制上述文件 <VirtualHost *:80> 改成 <VirtualHost *:443> 并添加证书文件。如果有自动生成https的功能,其实可以不用管这些。下面为https的示例

<IfModule mod_ssl.c>
<VirtualHost *:443>
  SSLProxyEngine On
 ServerName 你的域名
    ServerAlias 你的域名
    ProxyRequests Off
      <Proxy *>
        Order deny,allow
        Allow from all
       </Proxy>
   <Location />
    ProxyPass https://www.baidu.com/
    ProxyPassReverse https://www.baidu.com/
    </Location>

      <IfModule mod_headers.c>
     Header  set X-Frame-Options  "allow-from *"
    </IfModule>
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/你的主域名/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/你的主域名/privkey.pem
</VirtualHost>
</IfModule>


如果你的Apache服务器没有启用mod_headers模块,你需要先启用它 : sudo service apache2 restart

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。