您的位置:首页 > 运维架构 > Linux

centos服务器下更改网站根目录

2016-01-07 13:26 477 查看
  作为一个web前端的新手,花了几天买了服务器注册了域名,然后几天的焦头烂额终于网站可以运作了。满心欢喜的把自己做的网页放到了服务器上,可是还是出现了不少问题,在配置Apache的时候花了不少时间。好了,讲一下今天的主题,如何更改Apache访问网站的根目录呢?

  找到目录/etc/httpd/conf/下的httpd.conf,这是Apache的配置文件,我们要对Apache进行配置:

  提醒一下,httpd.conf下,文字前面有“#”的都是无效的,这些内容是用来进行一些必要的解释,其实仔细读一下这些内容你就大体知道某些配置是用来做什么的了。

  如果想要增加端口号,因为apache默认是80端口,找到 Listen 80,把想要增加新的端口直接添加到Listen 80后就可以了, 如

Listen 80
Listen 8081


  然后在你的IP地址后面加上:80或者8081就可以访问你的网站了,同时如果没有为不同的端口指定目录,它们都访问DocumentRoot,默认在/var/www/html下,修改目录:

  apache默认访问根目录由DocumentRoot决定,在配置文件中找到:

DocumentRoot "/var/www/html"


  将它改成你想要设置的目录,比如

DocumentRoot "/home/www/html"


  但 仅仅修改目录是不行的,你还要对目录进行授权,如果不给权限还是不能访问的。配置中的这一段内容就是告诉你要给目录权限才能访问。当然,默认的权限已经是所有人都可以访问,我们只需在换一下目录就可以了:

#
# This should be changed to whatever you set DocumentRoot to.
#
<Directory "/home/www/html">       //这里的目录对应网站的根目录,只改这一句。授权开始

#
# Possible values for the Options directive are "None", "All",
# or any combination of:
#   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important.  Please see
# http://httpd.apache.org/docs/2.2/mod/core.html#options # for more information.
#
Opt
4000
ions Indexes FollowSymLinks

#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
#   Options FileInfo AuthConfig Limit
#
AllowOverride None

#
# Controls who can get stuff from this server.
#
Order allow,deny
Allow from all              //默认所有人都可以访问

</Directory>                   //授权结束


  如果你对Apache做了一下配置,可是还是无权访问,出现了“you don’t have permission to access / on this server”,为什么呢?

  既然配置文件没有问题,那就需要考虑http.conf文件中指定的用户和组的访问权限。

在配置文件中找到以下:

# If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.
#
# User/Group: The name (or #number) of the user/group to run httpd as.
#  . On SCO (ODT 3) use "User nouser" and "Group nogroup".
#  . On HPUX you may not be able to use shared memory as nobody, and the
#    suggested workaround is to create a user www and use that user.
#  NOTE that some kernels refuse to setgid(Group) or semctl(IPC_SET)
#  when the value of (unsigned)Group is above 60000;
#  don't use Group #-1 on these systems!
#
User apache
Group apache


  有道翻译:如果您希望httpd为不同的用户或组运行,你必须首先运行httpd的根源,然后切换。

用户/组:运行用户/组的名称。它通常是良好的实践为运行httpd创建一个专用的用户和组,与大多数系统服务。

  好了,有用的就这两句,User apache Group apache

  改成Apache有访问权限的group或者是user,这个就要根据root权限用户的操作来看了。

  比如有用户组:www和用户ccc,就改成 User ccc Group www

这样,网站就能正常访问啦!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息