ubuntu apache 下配置node.js 虚拟域名

系统环境:

1
2
3
4
ubuntu 14.04 64位
apache 2.4.7
node.js 0.13.0
项目路径在:/var/www/node

 

配置步骤:

如果还没装node.js,就点击参考

1,apache设置:

新建配置文件

vim /etc/apache2/sites-available/nodejs.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<VirtualHost *:80>
ServerName nodejs.hongzhao.com
DocumentRoot /var/www/node
<Directory /var/www/node/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
RewriteEngine on
RewriteRule /(.*) http://localhost:8124/$1 [P,L]
ErrorLog /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/access.log combined
</VirtualHost>

保存后执行:

1
2
3
4
sudo a2ensite nodejs
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo /etc/init.d/apache2 restart

2,hosts添加虚拟域名:

vim /etc/hosts

最后一行添加

1
127.0.1.1 nodejs.hongzhao.com

 

3,node.js配置

vim /var/www/node/hello.js

1
2
3
4
5
6
7
8
9
10
var sys = require('sys'),
http = require('http'),
port = 8124;
http.createServer(function(request, response) {
response.writeHead(200, {
'Content-Type': 'text/html'
});
response.end(request.url + ' - Hello node.js\n');
}).listen(port);
sys.puts('Server listening on port ' + port);

保存后执行:

1
2
cd /var/www/node
node hello.js &

4,浏览器访问: http://nodejs.hongzhao.com/

会出现:/ - Hello node.js

说明配置成功了

 

坚持原创技术分享,您的支持将鼓励我继续创作!