Linux 6下安装编译安装Nginx

Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。在高连接并发的情况下,Nginx是Apache服务器不错的替代品:Nginx在美国是做虚拟主机生意的老板们经常选择的软件平台之一。能够支持高达50,000个并发连接数的响应,而且内存开销极小。这也是Nginx广受欢迎的重要原因。本文演示了基于Linux 6下编译安装Nginx,供大家参考。

一、安装环境

# cat /etc/issue
Red Hat Enterprise Linux Server release 6.3 (Santiago)
Kernel \r on an \m
# nginx -v
nginx version: nginx/1.8.0

二、配置安装环境

###为简化安装及配置,此处关闭了防火墙,生产环境建议开启
# service iptables stop
# chkconfig iptables off

# vi /etc/selinux/config 
SELINUX=disabled

###创建用户及组
#groupadd -r nginx
#useradd -s /sbin/nologin -g nginx -r nginx

###安装环境依赖包 http://nginx.org/en/linux_packages.html
# yum install pcre-devel zlib-devel openssl openssl-devel gcc gcc-c++

三、编译及安装Nginx

# cd /tmp/
# tar -xvf nginx-1.8.0.tar.gz
# cd /nginx-1.8.0
# ./configure                      \
--prefix=/etc/nginx                                          \
--sbin-path=/usr/sbin/nginx                                  \
--conf-path=/etc/nginx/nginx.conf                            \
--error-log-path=/var/log/nginx/error.log                    \
--http-log-path=/var/log/nginx/access.log                    \
--pid-path=/var/run/nginx.pid                                \
--lock-path=/var/run/nginx.lock                              \
--http-client-body-temp-path=/var/cache/nginx/client_temp    \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp           \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp       \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp           \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp             \
--user=nginx                                                 \
--group=nginx                                                \
--with-http_ssl_module                                       \
--with-http_realip_module                                    \
--with-http_addition_module                                  \
--with-http_sub_module                                       \
--with-http_dav_module                                       \
--with-http_flv_module                                       \
--with-http_mp4_module                                       \
--with-http_gunzip_module                                    \
--with-http_gzip_static_module                               \
--with-http_random_index_module                              \
--with-http_secure_link_module                               \
--with-http_stub_status_module                               \
--with-http_auth_request_module                              \
--with-mail                                                  \
--with-mail_ssl_module                                       \
--with-file-aio                                              \
--with-http_spdy_module                                      \
--with-ipv6                                                  

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: "/etc/nginx"
  nginx binary file: "/usr/sbin/nginx"
  nginx configuration prefix: "/etc/nginx"
  nginx configuration file: "/etc/nginx/nginx.conf"
  nginx pid file: "/var/run/nginx.pid"
  nginx error log file: "/var/log/nginx/error.log"
  nginx http access log file: "/var/log/nginx/access.log"
  nginx http client request body temporary files: "/var/cache/nginx/client_temp"
  nginx http proxy temporary files: "/var/cache/nginx/proxy_temp"
  nginx http fastcgi temporary files: "/var/cache/nginx/fastcgi_temp"
  nginx http uwsgi temporary files: "/var/cache/nginx/uwsgi_temp"
  nginx http scgi temporary files: "/var/cache/nginx/scgi_temp"

###如果apache httpd服务启动,建议先停止或更改端口号
# service httpd stop
# mkdir -p /var/cache/nginx/{client_temp,proxy_temp,fastcgi_temp,uwsgi_temp,scgi_temp}
# make && make install

###启动nginx
# /usr/sbin/nginx -c /etc/nginx/nginx.conf

# ps -ef|grep nginx|grep -v grep
root      33412      1  0 10:18 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     33413  33412  0 10:18 ?        00:00:00 nginx: worker process

[root@orasrv1 cache]# netstat -nltp|grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      33412/nginx         
[root@orasrv1 cache]# 

四、配置nginx为系统服务

vi /etc/init.d/nginx  

#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# Author : Leshami
# Blog   : http://blog.csdn.net/leshami           
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /etc/nginx/nginx.conf

#path for nginx binary
nginxd=/usr/sbin/nginx

#path for nginx configuration
nginx_config=/etc/nginx/nginx.conf

#path for nginx pid
nginx_pid=/var/run/nginx.pid
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

# chmod u+x /etc/init.d/nginx 

# service nginx start
Starting nginx:                                            [  OK  ]

# ps -ef|grep nginx |grep -v grep
root      33534      1  0 10:33 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     33535  33534  0 10:33 ?        00:00:00 nginx: worker process    

# service nginx stop
Stopping nginx:                                            [  OK  ]

# chkconfig --add nginx
# chkconfig nginx on

五、安装过程中的常见故障

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
### 以上2个错误,请安装相应的依赖包,见本文第二部分:配置安装环境

# /usr/sbin/nginx 
nginx: [emerg] getpwnam("nginx") failed
### 需要创建nginx用户组及用户

# /usr/sbin/nginx
nginx: [emerg] mkdir() "/var/cache/nginx/client_temp" failed (2: No such file or directory)
### 需要创建对应的目录

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏游戏杂谈

Flash Builder无法正常启动应用程序的原因

去掉编译器参数就可以了, 因为Flash Builer默认使用的是SDK 4.0,而目前开发中只需要使用3.5就可以了,加那个参数

1921
来自专栏菩提树下的杨过

IIS7.5中神秘的ApplicationPoolIdentity

IIS7.5中(仅win7,win2008 SP2,win2008 R2支持),应用程序池的运行帐号,除了指定为LocalService,LocalSystem...

22110
来自专栏WindCoder

JSON Web Token(JWT)教程:一个基于Laravel和AngularJS的例子

这是一篇介绍JSON Web Token(JWT)的文章,虽然可能用到的例子和Laravel和AngularJS有关,但知道了原理便能写出适用于自己的。同时,由...

3951
来自专栏阮一峰的网络日志

HTTPS 升级指南

上一篇文章我介绍了 HTTP/2 协议 ,它只有在 HTTPS 环境才会生效。 为了升级到 HTTP/2 协议,必须先启用 HTTPS。如果你不了解 HTTPS...

3425
来自专栏云计算教程系列

如何设置让我们在Ubuntu 14.04上加密多个Apache虚拟主机的证书

Web服务器中使用SSL证书来加密服务器和客户端之间的流量,为访问应用程序的用户提供额外的安全性。让我们的加密提供了一种免费获取和安装可信证书的简便方法。

1060
来自专栏电光石火

tengine+tomcat+php安装

在安装tengine之前,确认centos环境中有无gcc、pcre、openssl,如果没有按以下命令进行安装 #yum install gcc #yu...

22810
来自专栏游戏杂谈

ActionScript3检测当前下载资源的速度

speed = (byteTotal/1024)/(endTime-startTime),这个应该算是平均速度

1222
来自专栏程序猿

SSL 证书部署过程

本文演示环境 操作系统(64位):Ubuntu 16.04 CentOS 7.3 服务器软件:Nginx和Apache SSL证书:DV SSL 域名:19...

1.4K6
来自专栏Golang语言社区

Go代码打通HTTPs

TL;DR 手工创建CA证书链,手写代码打通HTTPs的两端 HTTPs最近是一个重要的话题,同时也是一个有点难懂的话题。所以网上有大量的HTTPs/TLS/S...

3854
来自专栏闻道于事

前后端分离之JWT用户认证(转)

在前后端分离开发时为什么需要用户认证呢?原因是由于HTTP协定是不储存状态的(stateless),这意味着当我们透过帐号密码验证一个使用者时,当下一个requ...

2091

扫码关注云+社区

领取腾讯云代金券