阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Nginx的编译安装

114次阅读
没有评论

共计 6003 个字符,预计需要花费 16 分钟才能阅读完成。

Nginx 是一个高性能的 Web 和反向代理服务器, 它具有有很多非常优越的特性:

作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率,这点使 Nginx 尤其受到虚拟主机提供商的欢迎。能够支持高达 50,000 个并发连接数的响应,感谢 Nginx 为我们选择了 epoll and kqueue 作为开发模型.

作为负载均衡服务器:Nginx 既可以在内部直接支持 Rails 和 PHP,也可以支持作为 HTTP 代理服务器 对外进行服务。Nginx 用 C 编写, 不论是系统资源开销还是 CPU 使用效率都比 Perlbal 要好的多。

作为邮件代理服务器: Nginx 同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last.fm 描述了成功并且美妙的使用经验。

Nginx 安装非常的简单,配置文件 非常简洁(还能够支持 perl 语法),Bugs 非常少的服务器: Nginx 启动特别容易,并且几乎可以做到 7 *24 不间断运行,即使运行数个月也不需要重新启动。你还能够在 不间断服务的情况下进行软件版本的升级。
 
编译环境:
[root@www ~]# yum groupinstall Development Tools
[root@www ~]# yum install pcre-devel openssl-devel

获取 nginx 程序源码包:

[root@www ~]# ll
total 888
drwxr-xr-x  9 1001 1001  4096 Dec 27 02:19 nginx-1.6.2
-rw-r–r–  1 root root 804164 Sep 16 22:45 nginx-1.6.2.tar.gz
-rw-r–r–  1 root root  29542 Apr 23  2014 nginx.vim

解压源码包:

[root@www ~]# tar xf nginx-1.6.2.tar.gz
[root@www ~]# cd nginx-1.6.2

创建 nginx 管理用户:

[root@www nginx-1.6.2]# useradd -M -s /sbin/nologin nginx
[root@www nginx-1.6.2]# id nginx
uid=500(nginx) gid=500(nginx) groups=500(nginx)

源码编译安装 nginx:

我们编译安装时需要定义各种配置需要的目录进行,所以需要新建一些目录:

日志存储目录:

[root@www ~]# mkdir /var/log/nginx

各种缓存目录,客户端,代理,fastcgi 目录:

[root@www ~]# mkdir -pv /var/tmp/nginx/{client,proxy,fastcgi}
mkdir: created directory `/var/tmp/nginx’
mkdir: created directory `/var/tmp/nginx/client’
mkdir: created directory `/var/tmp/nginx/proxy’
mkdir: created directory `/var/tmp/nginx/fastcgi’
[root@www ~]#

可以使用如下命令查看帮助文件:

[root@www nginx-1.6.2]# ./configure –help |less

开始编译 nginx:

[root@www nginx-1.6.2]# ./configure –prefix=/usr/local/nginx –conf-path=/etc/nginx/nginx.conf –user=nginx –group=nginx –error-log-path=/var/log/nginx/error.log –http-log-path=/var/log/nginx/access.log –pid-path=/var/run/nginx/nginx.pid –lock-path=/var/lock/nginx.lock –with-http_ssl_module –with-http_stub_status_module –with-http_gzip_static_module –with-http_flv_module –with-http_mp4_module –http-client-body-temp-path=/var/tmp/nginx/client –http-proxy-temp-path=/var/tmp/nginx/proxy –http-fastcgi-temp-path=/var/tmp/nginx/fastcgi

安装 nginx:

[root@www nginx-1.6.2]# make && make install

 检查安装:

查看安装目录

[root@www ~]# ls /usr/local/nginx/
html  sbin
[root@www ~]# ls /usr/local/nginx/sbin/
nginx

查看配置文件目录:

[root@www ~]# ls /etc/nginx/
fastcgi.conf            fastcgi_params.default  mime.types              nginx.conf.default      uwsgi_params           
fastcgi.conf.default    koi-utf                mime.types.default      scgi_params            uwsgi_params.default   
fastcgi_params          koi-win                nginx.conf              scgi_params.default    win-utf

环境变量设置:

[root@www ~]# echo “export PATH=/usr/local/nginx/sbin:$PATH” > /etc/profile.d/nginx.sh
[root@www ~]# source /etc/profile.d/nginx.sh

查看 nginx 的版本:

[root@www ~]# nginx -v
nginx version: nginx/1.6.2

 测试 nginx 的配置文件:

[root@www ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

提供 nginx 服务脚本:

[root@www ~]# vim /etc/rc.d/init.d/nginx
#!/bin/bash
#
# 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:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:    /var/run/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=”/etc/nginx/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’ -`
  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

将服务脚本加入系统启动并设置开机自动启动:

[root@www ~]# chkconfig –list nginx
service nginx supports chkconfig, but is not referenced in any runlevel (run ‘chkconfig –add nginx’)
[root@www ~]# chkconfig –add nginx
[root@www ~]# chkconfig nginx on
 

设置服务脚本开启执行权限:

[root@www ~]# chmod +x /etc/rc.d/init.d/nginx

启动 nginx 服务器:
[root@www ~]# vim /etc/rc.d/init.d/nginx
[root@www ~]# service nginx start           
Starting nginx:                                            [OK]
[root@www ~]# ss -tunl |grep 80
tcp    LISTEN    0      128                  :::38804                :::*   
tcp    LISTEN    0      128                    *:80                    *:*

简单的 nginx 基础介绍完毕!

————————————– 分割线 ————————————–

CentOS 6.2 实战部署 Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

使用 Nginx 搭建 WEB 服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm

搭建基于 Linux6.3+Nginx1.2+PHP5+MySQL5.5 的 Web 服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3 下 Nginx 性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3 下配置 Nginx 加载 ngx_pagespeed 模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

CentOS 6.4 安装配置 Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx 安装配置使用详细笔记 http://www.linuxidc.com/Linux/2014-07/104499.htm

Nginx 日志过滤 使用 ngx_log_if 不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

————————————– 分割线 ————————————–

Nginx 的详细介绍 :请点这里
Nginx 的下载地址 :请点这里

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-20发表,共计6003字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中