修改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 -s /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 -a -G konghy www-data
konghy為當前用戶的用戶組
轉載http://konghy.blog.163.com/blog/static/2405390462015022515167/