安装nginx及其依赖
安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装
yum install -y gcc gcc-c++
PCRE pcre-devel 安装
yum install -y pcre pcre-devel
zlib 安装
yum install -y zlib zlib-devel
OpenSSL 安装
yum install -y openssl openssl-devel
nginx_upstream_jvm_route
nginx_upstream_jvm_route 是一个nginx的扩展模块,用来实现基于 Cookie 的 Session Sticky 的功能。
yum install -y pcre-devel zlib-devel openssl-devel
下载上述源码包
将其统一上传到服务器指定路径下,如/home/softinstall/nginx
配置服务并设置开机启动
编写服务脚本
#!/bin/sh
#
# chkconfig: 35 80 20
# description: nginx Servlet Container
#
#
# match these values to your environment:
# Source function library.
. /etc/rc.d/init.d/functions
RETVAL=0
prog="nginx"
NGINX=/usr/local/nginx
start() {
$NGINX/sbin/nginx
echo $"Starting $prog: "
return $RETVAL
}
stop() {
$NGINX/sbin/nginx -s stop
echo $"Stopping $prog: "
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status $prog
RETVAL=$?
;;
restart)
stop
start
;;
*)
echo "nginx service is;Usage: $0 {start|stop|restart}"
;;
esac
exit $RETVAL
执行如下指令
chmod 755 /etc/rc.d/init.d/nginx
chkconfig nginx on
重启和启动服务
查看nginx
进入目录操作nginx
nginx执行文件在/usr/local/nginx/sbin/nginx
//进入到sbin目录
cd /usr/local/nginx/sbin/
./nginx //启动
./nginx -s stop //快速停止
./nginx -s reload //重新加载配置文件
快速退出
/nginx -s stop
配置基于域名的虚拟主机
一、创建站点目录
mkdir -p / www/ {web/ ,blog/}
二、配置站点首页文件
echo "welcom to pengqinxin 's web-server ">>/www/web/index.html
echo "welcom to pengqinxin 's blog-server ">>/www/blog/index.html
三、配置基于域名的虚拟主机
cd /usr/ local/nginx/ conf/
vim nginx.conf
server{
listen 80;
server_name www.yyy.com;
location / {
root /www / web;
index index.html index.htm;
}
}
server{
listen 80;
server_name blog.yyy.com ;
location / {
root /www/ blog;
index index.html index.htm
}
}
四、配置自己电脑hosts文件
C:\Windows\System32\drivers\etc
选择hosts文件,以管理员模式打开
在最后面加上自己的IP地址和自己添加的域名,保存退出