linux下apache2更换目录

修改apache2的默认文档目录(默认是在/var/www) 
修改命令:sudo gedit /etc/apache2/sites-enabled/000-default 
在文档中找到 DocumentRoot 在后面修改你要放置网页文件的目录 
修改完后重启apache2服务器即可,重启命令: sudo /etc/init.d/apache2 restart

在linux下开发html、php等程序时,默认要到/var/www目录下才能工作,而/var/www目录必须要有超级用户权限才能访问,还得改这个目录的权限。是不是想着要是能添加一个自己的工作目录就好了,例如:/home/konghy/www。这里介绍一种实现方法,我的apache版本为:Server version: Apache/2.4.7 (Ubuntu)。

1. 打开/etc/apache2/ports.conf文件添加一个端口,例如添加8080端口,则在该文件中添加 Listen 8080

$ sudo vi /etc/apache2/ports.conf

如下所示:

# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf

Listen 80
Listen 8080

<IfModule ssl_module>
    Listen 443 
</IfModule>

<IfModule mod_gnutls.c>
    Listen 443 
</IfModule>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet


2. 复制/etc/apache2/sites-available目录下的000-default.conf文件: 

$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/my.conf

然后将VirtualHost *:80改为VirtualHost *:8080
将DocumentRoot /var/www/html 改为自己的目录,例如:DocumentRoot /home/konghy/www

3. 将my.conf 软链到sites-enabled:

$ sudo ln -/etc/apache2/sites-available/my.conf /etc/apache2/sites-enabled/my.conf


4. 重启apache服务

sudo service apache2 restart


5.  在 /home/konghy/www 目录下建立一个测试页面index.html,在浏览器中打开:http://localhost:8080/ 如果显示正常,则配置结束。

6. 如果页面无法正常显示,并提示 403  Forbidden 错误:You don't have permission to access / on this server.
解决办法: 打开/etc/apache2/apache2.conf文件,添加一下内容:

<Directory /home/konghy/www>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>


7. 为了保证apache由权限访问你所配置的目录,可以将apache用户添加到自己的用户组中,apache的默认用户名为www-data,修改方法为:

$ sudo usermod --G konghy www-data

konghy为当前用户的用户组

转载http://konghy.blog.163.com/blog/static/2405390462015022515167/

原文地址:https://www.cnblogs.com/tirmer/p/8615141.html