安装Nginx:
centos 6.9 使用yum 安装 Nginx1.12.1
[root@jiufengjing ~]# wget http://nginx.org/download/nginx-1.14.0.tar.gz ##下载
Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。
[root@jiufengjing ~]# yum -y install pcre pcre-devel zlib zlib-devel openssl-devel openssl gcc gcc-c #安装依赖包
其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。
[root@jiufengjing ~]# useradd -M -s /sbin/nologin nginx ##创建用户
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
[root@jiufengjing ~]# tar zxf nginx-1.14.0.tar.gz -C /usr/src/ ##解压
Nginx官网:http://nginx.org/
[root@jiufengjing ~]# cd /usr/src/nginx-1.14.0/
Nginx中文手册:http://www.nginx.cn/doc/
[root@jiufengjing nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module &&make
一、安装前准备
&&make install ##编译安装
1.安装PCRE
Nginx常用编译选项说明
Nginx 模块必须在编译时就进行选择,目前不支持在运行时进行模块选择。在编译时,可以使用 ./configure --help查看编译帮助,会提供相关的编译选项。需要注意的一点就是大部分编译选项都有默认值,可以指定也可以不指定。另外编译选项一般以 --with开头的都是默认不会编译的,你需要用到什么功能就可以指定其编译选项;另外以 --without开头的选项表示这些模块都是默认会编译的,如果手动指定使用以 --without开头的选项表示你将会关闭此功能模块,慎用。
1)Nginx在编译时,运行期间与路径相关的各种参数
--prefix=PATH #指向安装目录
--sbin-path=PATH #指向(执行)程序文件(nginx)
--conf-path=PATH #指向配置文件(nginx.conf)
--error-log-path=PATH #指向错误日志目录
--http-log-path=PATH #指定访问复制目录
--pid-path=PATH #指向pid文件(nginx.pid)
--lock-path=PATH #指向lock文件(nginx.lock)(安装文件锁定,防止安装文件被别人利用,或自己误操作。)
--builddir=PATH #指向编译目录
--http-client-body-temp-path=PATH #设定http客户端请求临时文件路径
--http-proxy-temp-path=PATH #设定http代理临时文件路径
--http-fastcgi-temp-path=PATH #设定http fastcgi临时文件路径
--http-uwsgi-temp-path=PATH #设定http uwsgi临时文件路径
--http-scgi-temp-path=PATH #设定http scgi临时文件路径
2)一些权限、http、mail及依赖库模块
--user=USER #指定程序运行时的非特权用户
--group=USER #指定程序运行时的非特权用户组
--with-rtsig_module #启用rtsig模块支持(实时信号)
--with-file-aio #启用file aio支持(一种APL文件传输格式)
--with-ipv6 #启用ipv6支持
--with-select_module #启用select模块支持(一种轮询模式,不推荐在高载环境下使用)禁用:--without-select_module
--with-poll_module #启用poll模块支持(功能与select相同,与select特性相同,为一种轮询模式,不推荐在高载环境下使用)
--with-http_ssl_module #启用支持https请求,需已安装openssl
--with-http_realip_module #启用ngx_http_realip_module支持(这个模块允许从请求标头更改客户端的IP地址值,默认为关)
--with-http_addition_module #启用ngx_http_addition_module支持(作为一个输出过滤器,支持不完全缓冲,分部分响应请求)
--with-http_xslt_module #启用ngx_http_xslt_module支持(过滤转换XML请求)
--with-http_image_filter_module #启用支持传输JPEG/GIF/PNG 图片过滤,默认为不启用,gd库要用到
--with-http_sub_module #启用ngx_http_sub_module支持(允许用一些其他文本替换nginx响应中的一些文本)
--with-http_dav_module #启用ngx_http_dav_module支持(增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法)
--with-http_flv_module #启用ngx_http_flv_module支持(提供寻求内存使用基于时间的偏移量文件)
--with-http_gzip_static_module #启用ngx_http_gzip_static_module支持(在线实时压缩输出数据流)
--with-http_random_index_module #启用ngx_http_random_index_module支持(从目录中随机挑选一个目录索引)
--with-http_secure_link_module #启用ngx_http_secure_link_module支持(计算和检查要求所需的安全链接网址)
--with-http_degradation_module #启用ngx_http_degradation_module支持(允许在内存不足的情况下返回204或444码)
--with-http_stub_status_module #启用ngx_http_stub_status_module支持(获取nginx自上次启动以来的工作状态)
--with-http_perl_module #启用ngx_http_perl_module支持(该模块使nginx可以直接使用perl或通过ssi调用perl)
--with-mail #启用POP3/IMAP4/SMTP代理模块支持
--with-mail_ssl_module #启用ngx_mail_ssl_module支持
--add-module= #启用外部模块支持
--with-cpu-opt= #指定编译的CPU,可用的值为: pentium,opteron, amd64, sparc32, sparc64, ppc64等
--with-pcre #启用pcre库(默认会自动去找rpm包安装的pcre库文件以及模块)
--with-pcre= #指向pcre库文件目录(如果是源码安装就需要指定,rpm包安装就不需要指定了)
--with-pcre-opt= #在编译时为pcre库设置附加参数
--with-perl_modules_path= ##设定perl模块路径
--with-perl= #设定perl库文件路径
--with-debug #启用debug日志
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。
默认启用的选项(等同于默认启用的模块)
--without-http #该模块用于http server功能
--without-http-cache #该模块用于http cache功能
--without-http_charset_module #该模块用于重新编码web页面,但只能是一个方向--服务器端到客户端,并且只有一个字节的编码可以被重新编码
--without-http_gzip_module #该模块同-with-http_gzip_static_module功能一样
--without-http_ssi_module #该模块提供了一个在输入端处理处理服务器包含文件(SSI)的过滤器,目前支持SSI命令的列表是不完整的
--without-http_userid_module #该模块用来处理用来确定客户端后续请求的cookies
--without-http_access_module #该模块提供了一个简单的基于主机的访问控制。允许/拒绝基于ip地址
--without-http_auth_basic_module #该模块是可以使用用户名和密码基于http基本认证方法来保护你的站点或其部分内容
--without-http_autoindex_module #该模块用于自动生成目录列表,只在ngx_http_index_module模块未找到索引文件时发出请求
--without-http_geo_module #该模块用于创建一些变量,其值依赖于客户端的IP地址
--without-http_map_module #该模块用于使用任意的键/值对设置配置变量
--without-http_referer_module #该模块用来过滤请求,拒绝报头中Referer值不正确的请求
--without-http_rewrite_module #该模块允许使用正则表达式改变URI
--without-http_proxy_module #该模块用于代理服务器
--without-http_fastcgi_module #该模块允许Nginx与FastCGI进程交互,并通过传递参数来控制FastCGI进程工作,FastCGI一个常驻型的公共网关接口
--without-http_uwsgi_module #该模块启用uwsgi协议,uWSGI服务器相关
--without-http_scgi_module #该模块用来启用SCGI协议支持,SCGI协议是CGI协议的替代,它是一种应用程序与HTTP服务接口标准
--without-http_memcached_module #该模块用来提供简单的缓存,以提高系统效率
--without-http_limit_wns9778.com,zone_module #该模块可以针对条件,进行会话的并发连接数控制
--without-http_limit_req_module #该模块允许你对于一个地址进行请求数量的限制用一个给定的session或一个特定的事件
--without-http_empty_gif_module #该模块在内存中常驻了一个1*1的透明GIF图像,可以被非常快速的调用)
--without-http_browser_module #该模块用来创建依赖于请求报头的值
--without-http_split_clients_module #该模块用来基于某些条件划分用户。条件如:ip地址、报头、cookies等等
--without-http_upstream_ip_hash_module #该模块用于负载均衡
--without-mail_pop3_module #该模块用于pop3协议,邮局协议
--without-mail_imap_module #该模块用于邮件的imap协议,邮件接收协议
--without-mail_smtp_module #该模块用于smtp协议,邮件发送协议
[root@jiufengjing nginx-1.14.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ ###创建软链接,优化命令搜索路径
[root@jiufengjing nginx-1.14.0]# ls -l /usr/local/sbin/nginx
lrwxrwxrwx 1 root root 27 Aug 15 03:09 /usr/local/sbin/nginx -> /usr/local/nginx/sbin/nginx
[root@jiufengjing nginx-1.14.0]# cd
[root@jiufengjing ~]# nginx -t ##检查配置
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@jiufengjing ~]# nginx ##启动服务
[root@jiufengjing ~]# netstat -utpln |grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13592/nginx: master
使用nginx服务启动脚本:
[root@jiufengjing ~]# vim /etc/init.d/nginx
(1)#!/bin/bash
# chkconfig: 35 99 20
# description: Nginx Server Control Script
NP="/usr/local/nginx/sbin/nginx"
NPF="/usr/local/nginx/logs/nginx.pid"
case "$1" in ##$1表示第一位置变量,$0表示脚本本身
start)
$NP;
if [ $? -eq 0 ]
then
echo "nginx is starting!! "
fi
;;
stop)
kill -s QUIT $(cat $NPF)
if [ $? -eq 0 ]
then
echo "nginx is stopping!! "
fi
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $NPF)
if [ $? -eq 0 ]
then
echo "nginx config file is reload! "
fi
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
:wq
(2)#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start()
{
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop()
{
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}
reload()
{
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart()
{
stop
start
}
configtest()
{
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
[root@jiufengjing ~]# chkconfig --add nginx ##添加系统服务
[root@jiufengjing ~]# chmod x /etc/init.d/nginx ##授权
[root@jiufengjing ~]# /etc/init.d/nginx restart ##重启验证
nginx is stopping!!
nginx is starting!!
[root@jiufengjing ~]# netstat -utpln |grep nginx ##查看nginx监听端口
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 14036/nginx: master
访问验证
[root@jiufengjing ~]# elinks --dump 192.168.100.102
-dump:将HTML文档以纯文本的方式打印到标准输出设备;
Welcome to
nginx!
If you see this page, the
nginx web server is successfully installed and
working. Further
configuration is required.
For online documentation and
support please refer to [1]nginx.org.
Commercial support is
available at [2]nginx.com.
Thank you for using nginx.
References
Visible
links
1.
http://nginx.org/
2.
http://nginx.com/
PCRE被广泛使用在许多开源软件之中,最著名的莫过于Apache HTTP服务器和PHP脚本语言、R脚本语言,此外,正如从其名字所能看到的,PCRE也是perl语言的缺省正则库。
[root@001 ~]# yum install pcre-devel pcre -y
2.安装
OpenSSL 是一个安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。#nginx加密主要是依赖openssl
[root@001 ~]# yum install openssl openssl-devel -y
3.安装编译器
GCC(GNU Compiler Collection,GNU编译器套件),是由 GNU 开发的编程语言编译器。它是以GPL许可证所发行的自由软件,也是 GNU计划的关键部分。GCC原本作为GNU操作系统的官方编译器,现已被大多数类Unix操作系统(如Linux、BSD、Mac OS X等)采纳为标准的编译器,GCC同样适用于微软的Windows。GCC是自由软件过程发展中的著名例子,由自由软件基金会以GPL协议发布。
[root@001 ~]# yum install gcc gcc-c -y
[root@001 ~]# yum install wget -y
4.添加用户
[root@001 ~]# useradd nginx -s /sbin/nologin -M
(关闭防火墙及selinux)
二、安装 Nginx
[root@001 ~]# wget http://nginx.org/download/nginx-1.12.1.tar.gz
[root@001 ~]# tar xf nginx-1.12.1.tar.gz -C /usr/local/src/
[root@001 ~]# cd !$
[root@001 src]# cd nginx-1.12.1
[root@001 nginx-1.12.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
--配置参数说明:
--prefix=/usr/local/nginx #安装路径
--user=nginx #进程用户
--group=nginx #进程组
--with-http_ssl_module #激活状态信息
--with-http_stub_status_module #激活ssl功能 ---更多参数详解见文章尾部
--查看编译参数使用 -V:
[root@001 sbin]# ./nginx -V
nginx version: nginx/1.12.1
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
[root@001 nginx-1.12.1]# make && make install
[root@001 nginx-1.12.1]# cd /usr/local/nginx/sbin/
[root@001 sbin]# ./nginx #启动
#无报错则编译安装成功
三、将nginx加入到系统环境变量及service方式管理服务
1.将nginx加入到系统环境变量
[root@001 sbin]# vim /etc/profile #在/etc/profile 中加入如下内容
export NGINX_HOME=/usr/local/nginx
export PATH=$PATH:$NGINX_HOME/sbin
[root@001 sbin]# source /etc/profile #执行 source /etc/profile ,使配置文件生效。
[root@001 sbin]# nginx -s stop #停止服务
[root@001 sbin]# nginx #启动服务
2.创建脚本把Nginx加为系统服务使其内使用(service nginx start/stop/restart)
[root@001 sbin]# vim /etc/init.d/nginx
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx #/注意你安装nginx是否这个路径
nginx_config=/usr/local/nginx/conf/nginx.conf #/注意你安装nginx是否这个路径
nginx_pid=/log/nginx/nginx.pid #/注意你安装nginx是否这个路径
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL
[root@001 sbin]# chmod -R 755 /etc/init.d/nginx #现在就可以使用service来关闭或开启服务
=========================到这Nginx安装完成 =========================
四、部署基于域名、ip、端口测试站点
Nginx目录下的目录说明:
1.创建两个站点目录
[root@001 ~]# mkdir /usr/local/nginx/html/www
[root@001 ~]# mkdir /usr/local/nginx/html/bbs
2.编辑Nginx 配置文件
[root@001 ~]# egrep -v "#|^$" /usr/local/nginx/conf/nginx.conf.default > /usr/local/nginx/conf/nginx.conf #去掉配置文件注释与空行
3.基于域名站点目录
[root@001 conf]# vim /usr/local/nginx/conf/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.test.com; #站点域名
location / {
root html/www; #站点目录
index index.html index.htm;
}
}
添加:
server {
listen 80;
server_name bbs.test.com; #站点域名
location / {
root html/bbs; #站点目录
index index.html index.htm;
}
}
}
[root@001 html]# /usr/local/nginx/sbin/nginx -t #检查配置文件是否有错
[root@001 html]# /usr/local/nginx/sbin/nginx -s reload #重新加载配置文件
Nginx 配置文件详解:http://www.cnblogs.com/imweihao/p/7486668.html
[root@001 html]# echo "www.test.com">> www/index.html #添加测试页面
[root@001 html]# echo "bbs.test.com">> bbs/index.html #添加测试页面
[root@001 html]# vim /etc/hosts #修改hosts配置文件
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.222 www.test.com #添加
192.168.1.222 bbs.test.com #添加
[root@001 sbin]# curl www.test.com
www.test.com #测试页面正常
[root@001 sbin]# curl bbs.test.com
bbs.test.com #测试页面正常
3.基于IP或端口的虚拟测试站点
[root@001 conf]# ip addr help
Usage: ip addr {add|change|replace} IFADDR dev STRING [ LIFETIME ]
[ CONFFLAG-LIST]
ip addr del IFADDR dev STRING
ip addr {show|flush} [ dev STRING ] [ scope SCOPE-ID ]
[ to PREFIX ] [ FLAG-LIST ] [ label PATTERN ]
IFADDR := PREFIX | ADDR peer PREFIX
[ broadcast ADDR ] [ anycast ADDR ]
[ label STRING ] [ scope SCOPE-ID ]
SCOPE-ID := [ host | link | global | NUMBER ]
FLAG-LIST := [ FLAG-LIST ] FLAG
FLAG := [ permanent | dynamic | secondary | primary |
tentative | deprecated | CONFFLAG-LIST ]
CONFFLAG-LIST := [ CONFFLAG-LIST ] CONFFLAG
本文由wns9778.com发布于计算机教程,转载请注明出处:wns9778.comcentos 6.9 编译安装 Nginx1.12.1
关键词: wns9778.com