配置Nginx简介
与Apache类似,Nginx支持多种虚拟机配置方式,如基于IP地址的虚拟主机和基于域名的虚拟主机配置,本篇博客主要以基于域名的虚拟主机配置为例。
安装Nginx服务器:https://blog.csdn.net/qq_46323758/article/details/116351553
配置Nginx服务器
一、配置IP地址
1、配置IP地址
ifconfig ens33 192.168.100.100
二、新建目录及配置文件
1、新建目录:在/usr/local/nginx/conf目录下新建目录vhost
cd /usr/local/nginx/conf
mkdir vhost
2、在vhost目录中新建虚拟主机配置
cd vhost
vim exam.conf
3、设置配置文件内容
server{
listen 192.168.100.100:80;
server_name http://exam.excesoft.com;
access_log /data/logs/http://exam.excesoft.com.log main;
error_log /data/logs/http://exam.excesoft.com.errror.log;
location /{
root /var/www/html/exam;
index index.html index.htm;
}
}
4、将虚拟主机配置文件包含到主文件中
vim nginx.conf
在nginx.conf文件末尾的最后一个花括号内部,增加下列语句:include vhost/*.conf
include vhost/*.conf;
}
三、创建日志文件
1、创建目录
mkdir /data/logs
2、创建日志文件
touch /data/logs/http://exam.excesoft.com.log
3、创建错误日志
touch /data/logs/http://exam.excesoft.com.error.log
四、进行Nginx服务器的测试
1、检查服务器配置
cd /usr/local/nginx/sbin/
./nginx -t
2、启动nginx服务器
./nginx
3、添加域名和IP地址的对应关系
vim /etc/hosts
在最后一行添加内容
192.168.100.100 http://exam.excesoft.com
五、创建目录及测试文件
1、创建目录
mkdir -p /var/www/html/exam
2、创建测试文件
vim /data/exam.excesoft.com/index.html
3、在index.html中输入
hello,this is index.html in /data/exam.excesoft.com/
4、启动浏览器,输入http://192.168.100.100,可看到Nginx服务器的主页显示内容为首页内容