一、nginx介绍
nginx是一个支持高并发连接和反向代理功能的轻量级的web服务器。
nginx下载地址
[[email protected] ~]# wget -p http://nginx.org/download/nginx-1.12.2.tar.gz
二、安装nginx
1.安装nginx依赖包
[[email protected] ~]# yum -y install make zlib zlib-devel gcc-c++ pcre pcre-devel openssl openssl-devel
2.解压nginx安装包
[[email protected] soft]# tar -xf nginx-1.12.1.tar.gz -C /usr/local/
3.创建www用户组
[[email protected] ~]# groupadd www
4.创建www用户并指定用户组为www,www用户不能登录,不创建www家目录
[[email protected] ~]# useradd -g www -s /sbin/nologin -M
5.预编译nginx
[[email protected] nginx-1.12.1]# # ./configure --prefix=/usr/local/nginx-1.12 --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
6.编译并安装
[[email protected] ~]# make && make install
7.查看以上命令是否正确执行
[[email protected] ~]# echo $?
8.安装nginx目录创建软链接
[[email protected] ~]# ln -s /usr/local/nginx-1.12 /usr/local/nginx
9.nginx重启、nginx关闭、nginx平滑重启、nginx查看配置
[[email protected] ~] /usr/local/nginx -s reopen [[email protected] ~] /usr/local/nginx -s stop [[email protected] ~] /usr/local/nginx -s reload [[email protected] sbin]# ./nginx -V nginx version: nginx/1.12.1 built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013 TLS SNI support enabled configure arguments: --prefix=/application/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module
三、虚拟主机
一台物理服务器划分成多个“虚拟”的服务器,每一个虚拟主机都可以有独立的域名和独立的目录。nginx的虚拟主机就是通过nginx.conf主配置文件,中的server节点指定的。想要设置多个虚拟主机,配置多个server节点即可。
1.将nginx主配置文件中的注释和空格去除
[[email protected] nginx]# egrep -v ‘^$|#‘ conf/nginx.conf.default > conf/nginx.conf
2.配置基于域名的虚拟主机
[[email protected] conf]# sed -n ‘5,27p‘ nginx.conf http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name blog.ping.cn; location / { root html/blog/; index index.html index.htm; } } server { listen 80; server_name bbs.ping.cn; location / { root html/bbs/; index index.html index.htm; } }