Apache 虚拟主机 VirtualHost 配置

虚拟主机 (Virtual Host) 是在同一台机器搭建属于不同域名或者基于不同 IP 的多个网站服务的技术. 可以为运行在同一物理机器上的各个网站指配不同的 IP 和端口, 也可让多个网站拥有不同的域名.

Apache 是世界上使用最广的 Web 服务器, 从 1.1 版开始支持虚拟主机. 本文将讲解在不同服务器 (Redhat Enterprise Linux, Ubuntu Linux, Windows) 上使用 Apache 搭建虚拟主机来搭建多个网站.

 

主旨

本文旨在让读者知道如何在同一台机器上搭建多个网站, 并附带一些使用技巧. 以操作为主, 不会过多谈及原理.

目标

本文是写给拥有一定的服务器配置和管理技能, 工作中需要同时维护多个网站的网站主, 网站开发者和网络管理员. 如果你是互联网公司的配管工程师, 对计算机服务器原理和操作十分熟悉, 请忽视本文, 你不会在上面找到太多有价值的东西.

Redhat Enterprise Linux

Redhat Enterprise Linux (包括 CentOS Linux), 是使用最广的 Linux 服务器, 大量的网站应用都部署在其上.

1. 打开文件 /etc/httpd/conf/httpd.conf, 搜索 VirtualHost example, 找到代码如下:

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#
#<VirtualHost *:80>
#    ServerAdmin webmaster@dummy-host.example.com
#    DocumentRoot /www/docs/dummy-host.example.com
#    ServerName dummy-host.example.com
#    ErrorLog logs/dummy-host.example.com-error_log
#    CustomLog logs/dummy-host.example.com-access_log common
#</VirtualHost>

2. 仿照例子, 添加一段代码来指定某一域名的网站.

#
# DocumentRoot 是网站文件存放的根目录
# ServerName 是网站域名, 需要跟 DNS 指向的域名一致
#
<VirtualHost *:80>
    ServerAdmin zhao.wuz@alibaba-inc.com
    DocumentRoot /var/www/httpdocs/demo_aliluna_com
    ServerName demo.aliluna.com
    ErrorLog logs/demo.aliluna.com-error.log
    CustomLog logs/demo.aliluna.com-access.log common
</VirtualHost>

3. 重启 httpd 服务, 执行以下语句.

service httpd restart

Ubuntu Linux

Ubuntu 在 Linux 各发行版中, 个人用户数量最多的. 很多人在本机和虚拟机中使用. 但 Ubuntu 和 Redhat 的 VirtualHost 设置方法不相同.

1. 打开目录 /etc/apache2/sites-available/, 发现 default 和 default-ssl 两个文件, 其中 default 是 http 虚拟主机服务的配置文件, default-ssl 是配置 https 服务使用的. 可以复制一份 default 文件. 并修改配置文件名, 文件名必须与域名一致 (如: demo.aliluna.com)

2. 打开新建的配置文件, 修改 DocumentRoot, ServerName 和对应的配置目录. 例子如下:

#
# DocumentRoot 是网站文件存放的根目录
# ServerName 是网站域名, 需要跟 DNS 指向的域名一致
#
<VirtualHost *:80>
    ServerAdmin zhao.wuz@alibaba-inc.com
    DocumentRoot /var/www/httpdocs/demo_aliluna_com
    ServerName demo.aliluna.com
    ErrorLog ${APACHE_LOG_DIR}/demo.aliluna.com-error.log
    CustomLog ${APACHE_LOG_DIR}/demo.aliluna.com-access.log combined
</VirtualHost>

3. 通过 a2ensite 激活虚拟主机配置

sudo a2ensite demo.aliluna.com

4. 打开目录 /etc/apache2/sites-enabled/, 你会发现所有激活的虚拟主机, 可以通过 a2dissite 进行注销

sudo a2dissite demo.aliluna.com

5. 重启 Apache 服务, 激活虚拟主机

sudo /etc/init.d/apache2 restart

Windows

Windows 是市场占有率最高的 PC 操作系统, 也是很多人的开发环境. 其 VirtualHost 配置方法与 Linux 上有些差异, 以下方式适合原生 Apache, XAMPP 和 WAMP 套件.

1. 打开目录 {Apache2 安装目录}\conf\extra\, 找到 httpd-vhosts.conf 文件.

2. 仿照例子, 添加一段代码来指定某一域名的网站.

#
# DocumentRoot 是网站文件存放的根目录
# ServerName 是网站域名, 需要跟 DNS 指向的域名一致
#
<VirtualHost *:80>
    ServerAdmin zhao.wuz@alibaba-inc.com
    DocumentRoot "D:/workspace/php/demo_aliluna_com"
    ServerName demo.aliluna.com
    ErrorLog "logs/demo.aliluna.com-error.log"
    CustomLog "logs/demo.aliluna.com-access.log" common
</VirtualHost>

3. 打开 httpd.conf 文件, 添加如下语句.

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

4. 重启 Apache 服务.

Smarty error: unable to read resource,Smarty的错误 : 无法读取资源

一般出现这种错误是smarty配置的问题,路径没有配置好!如下面这段代码是将会出现这样的错误:

Warning: Smarty error: unable to read resource: “templates\smtl.html” inF:\wamp\www\shangage\smarty\Smarty.class.phpon line1095,无法读取资源,即无法读取smtl.html模版文件,

<?php
 define(‘base_path’,$_SERVER['DOCUMENT_ROOT']);//定义服务器的绝对路径
 define(‘smarty_path’,'\shangage\smarty\\’);//定义smarty目录的绝对路径
 require base_path.smarty_path.’Smarty.class.php’;//加载Smarty类库文件
 $smarty = new Smarty;//实例化一个smarty对象
 //定义个目录的路径
 $smarty->template_dir = base_path.smarty_path.’tmeplates\\’;
 $smarty->compile_dir = base_path.smarty_path.’templates_c\\’;
 $smarty->config_dir = base_path.smarty_path.’config\\’;
 $smarty->cache_dir = base_path.smarty_path.’cache\\’;
 
 $arr = array(1=>’杨’,2=>’君’,3=>’华’,4=>’杨君华’);
 $smarty->assign(‘title’,'smarty的第一次使用’);
 $smarty->assign(‘arr’,$arr);
 $smarty->display(‘templates\smtl.html’)
?>

把templates\smtl.html改写成..\templates\smtl.html,就可以调用模版文件。输出结果为:

 还可以把代码写成如下:

<?php
 define(‘base_path’,$_SERVER['DOCUMENT_ROOT']);//定义服务器的绝对路径
 define(‘smarty_path’,‘/shangage/smarty/’);//定义smarty目录的绝对路径
 require base_path.smarty_path.’Smarty.class.php’;//加载Smarty类库文件
 $smarty = new Smarty;//实例化一个smarty对象
 //定义个目录的路径
 $smarty->template_dir = base_path.smarty_path.‘tmeplates/’;
 $smarty->compile_dir = base_path.smarty_path.‘templates_c/’;
 $smarty->config_dir = base_path.smarty_path.‘config/’;
 $smarty->cache_dir = base_path.smarty_path.‘cache/’;
  
 $arr = array(1=>’杨’,2=>’君’,3=>’华’,4=>’杨君华’);
 $smarty->assign(‘title’,'smarty的第一次使用’);
 $smarty->assign(‘arr’,$arr);
 $smarty->display(‘../templates/smtl.html’)
?>

注意:正斜杠 /     与反斜杠  \   是一样的,但是要注意:反斜杠 \  在PHP中为转义字符,没有意义。如要输出单引号 ‘   则应该写成 \ ‘,因此 \\ ‘  实际上就是  / ‘ 。注意,模版文件要与调用模版文件的字符集编码一样,否则也会出现上面的错误!

 

iPhone loadView 和 viewDidLoad的区别

loadView 和 viewDidLoad 是 iPhone 开发中肯定要用到的两个方法。 他们都可以用来在视图载入的时候初始化一些内容。 但是他们有什么区别呢?

 

viewDidLoad 方法只有当 view 从 nib 文件初始化的时候才被调用。viewDidLoad 用于初始化,加载时用到。

 

loadView 方法在控制器的 view 为 nil 的时候被调用。 此方法用于以编程的方式创建 view 的时候用到。loadView 是使用代码生成视图的时候,当视图第一次载入的时候调用的方法。用于使用(写)代码来实现控件。用于使用代码生成控件的函数。如:

 

- ( void ) loadView {

UIView *view = [ [ UIView alloc] initWithFrame:[ UIScreen

mainScreen] .applicationFrame] ;

[ view setBackgroundColor:_color] ;

self.view = view;

[ view release] ;

}

 

你在控制器中实现了 loadView 方法,那么你可能会在应用运行的某个时候被内存管理控制调用。 如果设备内存不足的时候, view 控制器会收到 didReceiveMemoryWarning 的消息。 默认的实现是检查当前控制器的 view 是否在使用。 如果它的 view 不在当前正在使用的 view hierarchy 里面,且你的控制器实现了 loadView 方法,那么这个 view 将被 release, loadView 方法将被再次调用来创建一个新的 view。

iOS5打开系统setting页面

OS5的SDK现在可以直接打开setting页面了,以往的SDK则不可以,当时我还花了大量时间去实现,结果不了了之。 如果你想打开Location Services的setting页面,代码如下:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"]];

如果想打开Twitter的设置:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=TWITTER"]];

如果想打开蓝牙的设置: [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=General&path=Bluetooth"]];

当然你还可以打开应用的设置: [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=Apps&path=Your+App+Display+Name"]];

Your+App+Display+Name是什么,你应懂的。 不过我试这个的时候没有打开应用的setting,反而打开的是系统setting页,不知道为何。

Android开发之旅:环境搭建及HelloWorld

引言

本系列适合0基础的人员,因为我就是从0开始的,此系列记录我步入Android开发的一些经验分享,望与君共勉!作为Android队伍中的一个新人的我,如果有什么不对的地方,还望不吝赐教。

在开始Android开发之旅启动之前,首先要搭建环境,然后创建一个简单的HelloWorld。本文的主题如下:

  • 1、环境搭建
    • 1.1、JDK安装
    • 1.2、Eclipse安装
    • 1.3、Android SDK安装
    • 1.4、ADT安装
    • 1.5、创建AVD
  • 2、HelloWorld

1、环境搭建

1.1、JDK安装

如果你还没有JDK的话,可以去这里下载,接下来的工作就是安装提示一步一步走。设置环境变量步骤如下:

继续阅读

数据类型/对象类型介绍(1)NSString

字符串是程序设计最常用的数据类型之一了。在Mac/iPhone编程中,苹果为我们提供了一个不同的字符串类型NSString。有别与普通的String为数据类型,NSString其实是一个对象类型。NSString是NSObject(Cocoa Foundation的基础对象)的子类,所以具有NSObject的所有特性,好的和好的… ….

小常识:
NS是Cocoa类对象类型的前缀,来源于史蒂夫-乔布斯被苹果开除那段时间建立的公司NeXT.
@是Cocoa元素的前缀,很多地方我们会看到,比如接下来...

1, 创建一个NSString对象

简单方法:

NSString *aString = @"我是个NS字符串!";  //除了引号外加@, 没别的区别

*上面的不需要操心内存管理哟~

复杂一点儿:(需要内存管理的)

NSString *aString = [[NSString alloc] initWithFormat:@"这也是个NS字符串!"];

*initWithFormat是其中一个初始化方法,常用的还有

//从一个文件读取需要的内容
- (id)initWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error
//从一个地址读取需要的内容
- (id)initWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error

*以上方法得到的NSString的retain值为1,所以记得release掉阿~~

2,使用一个NSString对象

NSString *aString = @"This is a NSString";

NSLog(aString);   //在控制台输出信息,该函数方法需要一个NSString对象作为参数 

NSLog("这样不会好使的");

NSLog(@"这样就会好使拉~");

再比如设置一个UIView的标题:

[UIView setTitle:aString];

[UIView setTitle:@"标题"];

UIView.title = aString;

3,释放NSString

[aString release]; //对象将被系统释放掉咯

*记得不要释放直接用 = @”xxx” 的NSString对象哟,系统会管的~

4,快速使用一个NSString

NSLog([NSString stringWithFormat:@"一个NS字符串"]);

//这种快速方法返回的是一个retain为1,autorelease的对象,不需要操心它的内存管理

5,常用方法

我喜欢NSString的地方就在于很多方法非常方便,比如:

nString = [aString substringToIndex:4]; //nString将得到aString中的前四个字符

6,小结

暂时能记起来的就这么些了,有机会再补充,希望大家NS的愉快~