NGINX由入门到精通:编译安装nginx
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NGINX由入门到精通:编译安装nginx相关的知识,希望对你有一定的参考价值。
一、环境准备
1、系统和内内核版本
[[email protected] ~]# cat /etc/redhat-release CentOS release 6.8 (Final) [[email protected] ~]# uname -r 2.6.32-642.el6.x86_64
2、主机名称和IP地址
[[email protected] ~]# hostname linux-node1.ourplm.com [[email protected] ~]# hostname -I 192.168.56.11
3、关闭防火墙和SELINUX
[[email protected] ~]# getenforce Disabled [[email protected] ~]# /etc/init.d/iptables status iptables: Firewall is not running.
4、使用阿里yum源
[[email protected] ~]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup [[email protected] ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo [[email protected] ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo [[email protected] ~]# yum repolist Loaded plugins: fastestmirror, security Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * epel: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com repo id repo name status base CentOS-6 - Base - mirrors.aliyun.com 6,696 epel Extra Packages for Enterprise Linux 6 - x86_64 12,355 extras CentOS-6 - Extras - mirrors.aliyun.com 64 updates CentOS-6 - Updates - mirrors.aliyun.com 959 repolist: 20,074
二、nginx安装
1、必要软件准备
(1)安装 pcre,是为了支持rewrite功能,如果你已经装了,请跳过这一步
[[email protected] ~]# yum install -y pcre pcre-devel
(2)安装openssl,是为了ssl的支持,如果不需要 ssl 支持,请跳过这一步
[[email protected] ~]# yum install -y openssl openssl-devel
2、安装nginx
(1)创建软件包存放目录
[[email protected] ~]# mkdir -p /server/tools
(2)下载nginx源码包
[[email protected] ~]# cd /server/tools [[email protected] tools]# wget -q http://nginx.org/download/nginx-1.8.1.tar.gz [[email protected] tools]# ll total 816 -rw-r--r-- 1 root root 833473 Jan 27 2016 nginx-1.8.1.tar.gz
(3)解压nginx源码包
[[email protected] tools]# tar xf nginx-1.8.1.tar.gz [[email protected] tools]# ll total 820 drwxr-xr-x 8 1001 1001 4096 Jan 26 2016 nginx-1.8.1 -rw-r--r-- 1 root root 833473 Jan 27 2016 nginx-1.8.1.tar.gz
(4)创建nginx用户
[[email protected] nginx-1.8.1]# useradd -rs /sbin/nologin nginx [[email protected] nginx-1.8.1]# id nginx uid=498(nginx) gid=498(nginx) groups=498(nginx)
(5)编译安装
[[email protected] tools]# cd nginx-1.8.1 [[email protected] tools]# ./configure --prefix=/usr/local/nginx-1.8.1 --user=nginx --group=nginx --with-http_ssl_module --with-http_spdy_module --with-http_stub_status_module --with-pcre
参数解释:
--user=nginx :指定程序运行时的用户
--group=nginx :指定程序运行时的用户组
--prefix=/usr/local/nginx-1.8.1 :安装路径
– with-http_stub_status_module :支持 nginx 状态查询,可以用来监控nginx
– with-http_ssl_module :支持https
– with-http_spdy_module :支持google的 spdy, 想了解请百度 spdy, 这个必须有 ssl 的支持
– with-pcre :为了支持 rewrite 重写功能,必须制定 pcre
提示:出现如下的内容,表明nginx configure完成
……………省略内容……………
Configuration summary
+ using system PCRE library
+ using system OpenSSL library
+ md5: using OpenSSL library
+ sha1: using OpenSSL library
+ using system zlib library
nginx path prefix: "/usr/local/nginx-1.8.1"
nginx binary file: "/usr/local/nginx-1.8.1/sbin/nginx"
nginx configuration prefix: "/usr/local/nginx-1.8.1/conf"
nginx configuration file: "/usr/local/nginx-1.8.1/conf/nginx.conf"
nginx pid file: "/usr/local/nginx-1.8.1/logs/nginx.pid"
nginx error log file: "/usr/local/nginx-1.8.1/logs/error.log"
nginx http access log file: "/usr/local/nginx-1.8.1/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
[[email protected] nginx-1.8.1]# make && make install
……………省略内容……………
make[1]: Leaving directory `/server/tools/nginx-1.8.1‘
提示:出现上面的内容,表示nginx安装完成
(6)去除nginx目录版本号
[[email protected] ~]# ln -s /usr/local/nginx-1.8.1/ /usr/local/nginx [[email protected] ~]# ll -d /usr/local/nginx* lrwxrwxrwx 1 root root 23 Mar 13 20:01 /usr/local/nginx -> /usr/local/nginx-1.8.1/ drwxr-xr-x 6 root root 4096 Mar 13 19:56 /usr/local/nginx-1.8.1
3、启动、关闭和重新加载配置文件
(1)查看nginx命令帮助
[[email protected] ~]# /usr/local/nginx/sbin/nginx -h nginx version: nginx/1.8.1 Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives] Options: -?,-h : this help -v : show version and exit -V : show version and configure options then exit -t : test configuration and exit -q : suppress non-error messages during configuration testing -s signal : send signal to a master process: stop, quit, reopen, reload -p prefix : set prefix path (default: /usr/local/nginx-1.8.1/) -c filename : set configuration file (default: conf/nginx.conf) -g directives : set global directives out of configuration file
参数翻译:
参数 | 含义 |
-?,-h | 帮助 |
-v | 查看nginx版本 |
-V | 查看nginx版本以及编译安装参数 |
-t | 检查nginx配置文件语法 |
-q | 在配置测试期间禁止非错误消息 |
-s signal | 指定nginx服务停止、退出、重启和重新加载 |
-p prefix | 指定nginx配置文件nginx.conf目录 |
-c filename | 指定nginx配置文件 |
-g directives | 设置配置文件中全局指令 |
(2)启动nginx
[[email protected] ~]# /usr/local/nginx/sbin/nginx [[email protected] ~]# netstat -nlutp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4098/nginx tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1428/sshd tcp 0 0 :::22 :::* LISTEN 1428/sshd [[email protected] ~]# curl -s http://localhost | grep nginx.com <a href="http://nginx.com/">nginx.com</a>.</p>
(3)关闭nginx
[[email protected] ~]# /usr/local/nginx/sbin/nginx -s stop [[email protected] ~]# netstat -nlutp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1428/sshd tcp 0 0 :::22 :::* LISTEN 1428/sshd [[email protected] ~]# curl -s http://localhost | grep nginx.com
(4)加载配置文件
[[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload /usr/local/nginx-1.5.1/sbin/nginx
(5)nginx启动脚本
[[email protected] ~]# cat /etc/init.d/nginx #!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse # proxy and IMAP/POP3 proxy server # processname: nginx # config: /usr/local/nginx/conf/nginx.conf # config: /usr/local/nginx/conf/nginx.conf # pidfile: /usr/local/nginx/logs/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/local/nginx//sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() { # make required directories user=`$nginx -V 2>&1 | grep "configure arguments:" | sed ‘s/[^*]*--user=\([^ ]*\).*/\1/g‘ -` if [ -z "`grep $user /etc/passwd`" ]; then useradd -M -s /bin/nologin $user fi options=`$nginx -V 2>&1 | grep ‘configure arguments:‘` for opt in $options; do if [ `echo $opt | grep ‘.*-temp-path‘` ]; then value=`echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then # echo "creating" $value mkdir -p $value && chown -R $user $value fi fi done } start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { #configtest || return $? stop sleep 1 start } reload() { #configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac [[email protected] ~]# chmod +x /etc/init.d/nginx [[email protected] ~]# /etc/init.d/nginx Usage: /etc/init.d/nginx {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}
到这来nginx就算是安装完成啦O(∩_∩)O哈哈~
本文出自 “炫维” 博客,请务必保留此出处http://xuanwei.blog.51cto.com/11489734/1905865
以上是关于NGINX由入门到精通:编译安装nginx的主要内容,如果未能解决你的问题,请参考以下文章
Nginx从入门到精通Nginx配置文件说明及Nginx主要应用