ubuntu下设置apache虚拟站点及hosts开发PHP

ubuntu 下设置apache的配置文件与windows有所不同。


windows下设置apache只需要修改httpd.conf或是conf/extra/httpd-vhosts.conf都可以。


但是在ubuntu下, apache的配置文件httpd.conf是空的,里面竟然没有内容!!!


在/etc/apache2/apache2.conf里可以看到有很多include,而这些才是真正的配置文件。关于这个解释,网上有更详细的说明,请自行查阅。


下面,开始真正的操作实习。


一、建立站点目录文件夹。

     1、首先创建  /home/username/mysite/  目录做为网站的虚拟目录。

     *username即您的用户名;mysite即虚拟目录的名称;(以下含意相同),根据实际情况修改。

     2、在目录下建立相应的index.php、erro.log、access.log等基本文件(也可以只建立最简单的index.php打印一条语句)。

二、修改系统hosts

    修改hosts需要root权限

username@userhost:~$ sudo gedit /etc/hosts
[sudo] password for username: 
输入密码(如果您设置了的话,并且建议为root权限设置密码)
添加下面这段地址转向:

127.0.1.1	www.yoursite.com
*www.yoursite.com代表您要设置的转向的域名(下同)。

三、精简设置apache虚拟站点配置文件

    1、创建站点配置文件内容

#在终端输入下列命令创建配置文件(other为配置文件名称)
username@userhost:~$ sudo gedit /etc/apache2/sites-available/other


#配置文件内容示例如下:
<VirtualHost *>
# 在ServerName后加上你的网站名称
ServerName www.yoursite.com
# 如果你想多个网站名称都取得相同的网站,可以加在ServerAlias后加上其他网站别名。
# 别名间以空格隔开。
ServerAlias ftp.yoursite.com mail.yoursite.com
# 在ServerAdmin后加上网站管理员的电邮地址,方便别人有问题是可以联络网站管理员。
ServerAdmin webmaster@yoursite.com
# 在DocumentRoot后加上存放网站内容的目录路径(用户的个人目录)
DocumentRoot  /home/username/mysite/ 
<Directory  /home/username/mysite/ >
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
Allow from all
</Directory>
ErrorLog /home/username/mysite/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog  /home/username/mysite/access.log combined
ServerSignature On
</VirtualHost>

#如果你的服务器有多个IP,而不同的IP又有着不一样的虚拟用户的话,可以修改成:
#<VirtualHost IP地址[:端口]>
#...
#</VirtualHost>



    2、创建站点启用文件

#终端下输入命令,创建配置文件(other为配置文件名称)                   

   username@userhost:~$ sudo gedit /etc/apache2/sites-enabled/other


#配置文件内容示例如下:

<VirtualHost *:80>  
        ServerName www.yoursite.com  
  
        DocumentRoot /home/username/mysite/
        <Directory /home/username/mysite/>  
                Options ExecCGI FollowSymLinks  
                AllowOverride all  
                allow from all  
                Order allow,deny  
        </Directory>  
        ErrorLog /var/log/apache2/error-鍩熷悕.log  #此处因编码问题而出错,可以根据您个人的机器修正它。
</VirtualHost>

    3、连接文件

sudo ln -s /etc/apache2/sites-available/other /etc/apache2/sites-enabled/other

     4、检测apache配置文件是否正确

 sudo apache2ctl configtest
#如果出现: Syntax OK 表示语法正确,

     5、重启apache2

sudo /etc/init.d/apache2  restart

   OK,全部完成,现在在浏览器测试站点是否配置成功,因为一遍做,一边记录输入该笔记,可能有些疏漏,请大家多多提出宝贵意见。

关于apache配置里 

<Directory /home/username/mysite/> 
一段,是否要在路径上加" ",请参考下面5L的评论,感谢   ff781778304指出的问题。

    

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值