前提是虚拟机安装好LNMP环境,教程我的其它博客有写,下文所有路径都是基于我的LNMP环境。
首先打开Nginx的配置文件:
cd /usr/local/nginx/conf
vi nginx.conf
一个站点对应一个server段,在第一个server段后添加:
server {
listen 80;
server_name www.helloworld.com helloworld.com;
location / {
root html/helloworld;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root html/helloworld;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server_name是要配置的域名,多个域名用空格隔开。
location下的root是网站目录,我这里示范在html根目录下添加一个helloworld目录,如果是php站点,记得两个location下的root都要改成主页所在的路径。
保存退出重启Nginx。
进入/etc,打开hosts文件,将域名指向本地:
cd /etc
vi hosts
至此,网站在虚拟机中的配置完成,接下来到Win10主机上配置。
打开hosts(文件位置在C:\WINDOWS\system32\drivers\etc),将虚拟机中的域名指向虚拟机的IP地址:
然后就可以在主机的浏览器上通过域名访问虚拟机上配置的站点了。