Ubuntu14配置nginx虚拟主机

来源:互联网 发布:react js 阮一峰 编辑:程序博客网 时间:2024/05/16 13:03
一、建立项目目录
# 在 /home/share/ 目录下创建一个test项目目录,并编辑一个子目录 index.php 文件


sudo mkdir -p  /home/share/test




# 新建一个主页 index.php文件

sudo vim /home/share/test/index.php

test为项目根目录

二、配置文件

# 创建 server 虚拟主机配置文件

sudo vim /etc/nginx/sites-available/test.com


server {        listen 80;        listen [::]:80;        server_name test.com;        root /home/share/test;#配到index.php访问的时候直接可以test.com#配到test必须test.com/index.php访问#跟下面默认访问文件配置相关        index index.html index.htm index.nginx-debian.html;        location / {            try_files $uri $uri/ =404;            fastcgi_split_path_info ^(.+\.php)(/.+)$;            fastcgi_pass unix:/var/run/php5-fpm.sock;            fastcgi_index index.php;            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;            include fastcgi_params;        }}


#因为nginx的主配置文件nginx.conf会自动应用sites-enabled中的配置文件

#所以我们需要在sites-enabled中创建一个软链接到 /etc/nginx/sites-enabled/ 目录下,

sudo ln-s /etc/nginx/sites-available/test.com /etc/nginx/sites-enabled/


使配置文件生效


【直接在sites-enabled建立虚拟主机文件也可以】


三、更改 hosts 
# 添加your_ip_address test.com
我是在windows下 Windows\System32\drivers\etc


四、重启 
sudo /etc/init.d/nginx restart


重新加载nginx使配置生效 
$ sudo /etc/init.d/nginx reload


五、访问windows浏览器访问
http://test.com/index.php
原创粉丝点击