标签:nginx服务器
搭建Nginx服务器(网站服务 代理服务)
rpm -q gcc gcc-c++
yum -y groupinstall "开发工具"
useradd nginx
yum -y install pcre-devel 依赖包
yum -y install zlib-devel 依赖包
yum -y install openssl-devel 安全认证包
tar -zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0
./configure --prefix=/usr/local/nginx --user=nginx --gourp=nginx
\--with-http_stub_status_module --with-http_ssl_module(开启认证)
make && make install
ls /usr/local/nginx/ 成功显示以下文件证明安装成功
conf html logs sbin
conf 配置文件: nginx.conf 主配置文件 nginx.conf.default 模版
html 网页目录
logs 日志文件存放的目录
sbin 存放启动NGINX服务的启动命令 nginx
启动nginx服务(默认监听80)
[root@squid nginx]# netstat -utnalp | grep :80 有http先停掉
[root@squid nginx]# /usr/local/nginx/sbin/nginx
[root@squid nginx]# netstat -utnalp | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 92
[root@squid nginx]# echo 123 > /usr/local/nginx/html/test.html 写网页文件
[root@squid nginx]# elinks --dump http://localhost/test.html 客户端测试
123
修改nginx服务端口:
mv nginx.conf nginx.conf.bak 备份配置文件
grep -v ‘^$\|#‘ nginx.conf.bak > nginx.conf 去除空行和注释行后到新配置文件
vim nginx.conf 修改 listen 8080;
/usr/local/nginx/sbin/nginx -s stop 停服务
/usr/local/nginx/sbin/nginx 启动服务
netstat -untlap | grep :8080
elinks --dump http://localhost:8080/test.html 指定端口测试
常用命令
[root@squid conf]# /usr/local/nginx/sbin/nginx -v 查看nginx版本
[root@squid conf]# /usr/local/nginx/sbin/nginx -V 查看nginx版本以及编安装详细信息
[root@squid conf]# /usr/local/nginx/sbin/nginx -t 测试配置文件是否正常
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx2.conf 指定配置文件启动服务
停止服务的方法
/usr/local/nginx/sbin/nginx -s stop
killall -9 nginx
kill -信号 pid号
常见信号:
TERM, INT 快速关闭
QUIT 从容关闭,关闭主进程及子进程
HUP 重载配置文件
USR1 重新打开日志文件
USR2 平滑升级可执行程序
重启服务很方便
kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
++++++++++++++++++++++++++++++++++
平滑升级(在线升级服务软件的版本)
tar -zxvf nginx-1.9.2.tar.gz
cd nginx-1.9.2
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
\--with-http_stub_status_module --with-http_ssl_module(开启认证)
make
cd /usr/local/nginx/sbin
mv nginx nginxold 备份旧的执行程序
cd nginx-1.9.2/objs
cp nginx /usr/local/nginx/sbin/ 拷贝新版本执行程序
cd nginx-1.9.2
make upgrade 执行升级
[root@squid conf]# /usr/local/nginx/sbin/nginx -v 查看nginx版本 升级完成
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
虚拟主机(一台服务器提高多个网站)
基于域名虚拟主机(根据客户端访问的主机名区分访问)
基于端口虚拟主机
基于ip地址虚拟主机
++++++++++++++++++++++++++++++++++++++++++++++
基于域名虚拟主机
(服务器)
mkdir /wwwdir
mkdir /bbsdir
echo www > /wwwdir/a.html
echo bbs > /bbsdir/a.html
[root@A conf]# /usr/local/nginx/sbin/nginx -s stop 先停止服务
grep -v ‘^$‘ nginx.conf.default | grep -v ‘#‘ > nginx.conf
vim nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.tarena.com; 修改为指定域名
location / {
root /wwwdir; 指定网页目录
index a.html; 指定默认首页文件
}
}
server {
listen 80;
server_name bbs.tarena.com;
location / {
root /bbsdir;
index a.html;
}
}
}
}
./nginx -t 测试配置文件配置正确
./nginx 启动服务
(客户端)测试
vim /etc/hosts
172.25.254.151(服务端IP) www.tarena.com www
172.25.254.151(服务端IP) bbs.tarena.com bbs
:wq
ping www.tarena.com
ping bbs.tarena.com
elinks --dump ghtp://www.tarena.com
elinks --dump http://bbs.tarena.com
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
基于端口的虚拟主机(服务器根据客户端访问的端口区分访问)
实验需求
http://www.tarena.com -> /usr/local/nginx/html
http://www.tarena.com:8080 -> /wwwdir
http://www.tarena.com:8090 -> /bbsdir
vim nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.tarena.com;
location / {
root html;
index index.html;
}
}
server {
listen 8080; 指定不同端口
#server_name www.tarena.com; 注销掉域名
location / {
root /wwwdir;
index a.html;
}
}
server {
listen 8090;
#server_name bbs.tarena.com;
location / {
root /bbsdir;
index a.html;
}
}
}
}
[root@A conf]# ../sbin/nginx -s stop
[root@A conf]# ../sbin/nginx
[root@A conf]# netstat -anptu |grep nginx
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 51193/nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 51193/nginx
tcp 0 0 0.0.0.0:8090 0.0.0.0:* LISTEN 51193/nginx
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
基于ip地址的虚拟主机
vim nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 1.0.0.254:8090;
#server_name www.tarena.com;
location / {
root /wwwdir;
index a.html;
}
}
server {
listen 1.0.0.100:80;
#server_name bbs.tarena.com;
location / {
root /bbsdir;
index a.html;
}
}
}
标签:nginx服务器
原文地址:http://liangzai818.blog.51cto.com/10003446/1762800