Nginx源代码安装

Linux下安装软件有三种方式,这里我以源代码编译安装为主。服务器最小化安装后,安装依赖包。
 
出于管理和安全的目的,我们希望使用一个指定的普通用户身份去运行我们的Web服务器。所以,我们首先增加一个普通用户用于运行我们的Nginx。

[[email protected] ~]# groupadd nginx 
[[email protected] ~]# useradd -g nginx nginx

关闭系统防火墙,

[[email protected] ~]# service iptables stop 
[[email protected] ~]# chkconfig iptables off

1. 下载最新稳定版本并安装Nginx
然后下载、解压并编译安装我们的Nginx, 这里使用的是最新稳定版本,

[[email protected] ~]# wget http://nginx.org/download/nginx-1.8.0.tar.gz 
[[email protected] ~]# tar -xf nginx-1.8.0.tar.gz -C /usr/local/src 
[[email protected] ~]# cd /usr/local/src/nginx-1.8.0 
[[email protected] nginx-1.8.0]# ./configure --user=nginx --group=nginx 
--with-http_ssl_module --with-http_sub_module

安装过程比较简单,./configure过程会报出一些依赖关系,这里一一解决之。首先,操作系统是最小化安装,并没有安装gcc,所以,第一步进行./configure的时候,就会报错。

当出现如下错误时,需要安装ssl的开发包,

./configure: error: SSL modules require the OpenSSL library. 
You can either do not enable the modules, or install the OpenSSL library 
into the system, or build the OpenSSL library statically from the source 
with nginx by using --with-openssl=<path> option.

当出现如下错误时,需要安装zlib的开发包,

./configure: error: SSL modules require the OpenSSL library. 
You can either do not enable the modules, or install the OpenSSL library 
into the system, or build the OpenSSL library statically from the source 
with nginx by using --with-openssl=<path> option.
[[email protected] ~]# yum install -y pcre-devel 
[[email protected] ~]# yum install -y gcc 
[[email protected] ~]# yum install -y zlib-devel 
[[email protected] ~]# yum install -y openssl-devel

下面来看看./configure后面几个常用的参数:

--prefix=<dir>         指定安装主目录,默认为/usr/local/nginx 
--user=<user>          指定用户身份,如果没有指定则默认使用nobody 
--group=<group>        指定组身份 
--with-http_ssl_module 启用https支持

2. Nginx的启动、重启与停止

安装完毕,我们就可以启动Nginx了,

[[email protected] ~]# /usr/local/nginx/sbin/nginx -c /usr/loca/nginx/conf/nginx.conf

-c是用来指定Nginx的主配置文件,如果没有指定则默认为/usr/loca/nginx/conf/nginx.conf文件。启动后,可以用ps与netstat命令查看是否启动成功,

[[email protected] ~]# ps -ef |grep nginx
root      1059     1  0 02:49 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nginx     1061  1059  0 02:49 ?        00:00:00 nginx: worker process                                          
root      1063  1013  0 02:49 pts/0    00:00:00 grep nginx

[[email protected] ~]# netstat -antup
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 192.168.1.151:8080          0.0.0.0:*                   LISTEN      1059/nginx          
tcp        0      0 192.168.1.150:8080          0.0.0.0:*                   LISTEN      1059/nginx          
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      1059/nginx          
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      801/sshd            
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      877/master          
tcp        0      0 192.168.1.129:22            192.168.1.106:56004         ESTABLISHED 1009/sshd           
tcp        0      0 :::22                       :::*                        LISTEN      801/sshd            
tcp        0      0 ::1:25                      :::*                        LISTEN      877/master          
udp        0      0 0.0.0.0:68                  0.0.0.0:*                               1007/dhclient

启动成功,我们可以访问首页去验证一下,

3. Nginx启动脚本

Nginx并没有提供类似System V服务的管理脚本,如果我们希望开机时要让Nginx自动启动,可以执行如下命令:

[[email protected] ~]# echo “/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
” >> /etc/rc.local

当然,如果我们对System V的服务管理脚本情有独钟的话,可以参考如下脚本

[[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: /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="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    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
    killall -9 nginx
}

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

最后,给脚本一个可执行的权限,然后使用chkconfig命令对其进行管理,

[[email protected] ~]# chmod 755 /etc/init.d/nginx
[[email protected] ~]# chkconfig nginx on

当我们对Nginx的配置文件做过一些更改后,希望在不中断当前服务的情况下,进行一个平滑的重启,可以使用如下命令,

[[email protected] ~]# service nginx reload

脚本中的reload函数会首先对配置文件做一个语法格式的检查,使用的是如下命令,

[[email protected] ~]# /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf

当语法格式检查通过后,会对Nginx发出一个标记为1或者说是HUP的信号,Nginx收到后会关闭旧进程,打开新进程,如果有进程正在为一个用户提供服务,则会等待这次服务结束。

当然,我们也可以使用service nginx restart的方式去重启服务。停止Nginx,直接service nginx stop即可,或者kill掉所有的Nginx进程。

时间: 2024-08-26 09:30:09

Nginx源代码安装的相关文章

CentOS6.4下源代码安装以及nginx配置

以前网上有个centos下的自动安装脚本,脚本本身应该是正确的,但是可能在多人转载后,中间有信息遗漏或者丢失,所以造成脚本失败,不能直接用 我把自己在CentOS下源代码安装OE以及与nginx整合的操作步骤拿出来分享一下,如有谬误,请坛友多指正 第一部分,安装OE说明:CentOS版本为6.4版本,命令行采用bash,PostgreSQL为9.2版本,Nginx为1.4版本 1.CentOS 的安装选项有最小化安装.基本服务器安装.虚拟主机安装等等的,我这里采用的是基本服务器安装(带的东西较少

Ubuntu中Nginx的安装与配置

Ubuntu中Nginx的安装与配置 1.Nginx介绍 Nginx是一个非常轻量级的HTTP服务器,Nginx,它的发音为“engine X”, 是一个高性能的HTTP和 反向代理服务器,同时也是一个IMAP/POP3/SMTP 代理服务器. 2.对PHP支持 目前各种web 服务器对PHP的支持一共有三种: (1)通过web 服务器内置的模块来实现,例如Apache的mod_php5,类似的Apache内置的mod_perl 可以对perl支持. (2)通过CGI来实现,这个就好比之前per

Nginx 在安装入门

1.首先需要安装必要的库,PCRE,zlib sudo apt-get install libpcre3 libpcre3-dev 假设找不到文件的话就下载源文件进行安装. 2.解压下载的nginx源代码.进入文件夹: sudo ./configure 得到的输出例如以下: Configuration summary + using system PCRE library + OpenSSL library is not used + using builtin md5 code + sha1

菜鸟nginx源代码剖析 配置与部署篇(一) 手把手实现nginx &amp;quot;I love you&amp;quot;

菜鸟nginx源代码剖析 配置与部署篇(一) 手把手配置nginx "I love you" Author:Echo Chen(陈斌) Email:[email protected] Blog:Blog.csdn.net/chen19870707 Date:Nov 7th, 2014 还记得在前几年的CSDN泄漏账号事件中.统计发现程序猿的账号中含有love的最多,这里我也俗套下.在这篇文章中将解说怎样 一步一步有用Nginx在一台机器上搭建一个最简单的显示"I love y

编译源代码安装软件大体步骤

使用包管理方式安装软件与编译源代码的区别,包管理方式提供的(以rpm为例)软件大多只保留了一部分常用功能,如果我们需要使用软件其它功能而rpm包没有,这咱情况下就需要编译源代码了.编译源代码的方式安装的软件,可以自由选择安装那些功能,而rpm是固定好的.源代码安装选择功能的灵活性好,rpm包安装则比较简便.而且大多数情况下,软件以源代码方式发行比rpm包要快. 源代码的获取方式, 编译源代码安装软件依赖开发环境,编译c程序需要gcc编译器,编译c++需要gcc-c++编译器,如果没有,需要安装.

Nginx编译安装,启动,停止,升级。

1.简单介绍下Nginx Nginx是一款轻量级的web服务器和反向代理服务器,它使用了epoll的I/O模型,也就是事件触发I/O模型,减少了进程的生成切换所消耗的系统资源(CPU的压力减少,内存的占用也会减少),可以达到很高的并发请求.它是一款开源软件,企业成本降低,它的使用配置也比较简单,同时支持Rewrite,作为反向代理的时候可以检查后端的Web服务器的健康状况,能够支持热部署. 2.Nginx安装,重启,升级,停止. 环境是Centos系统,通过www.nginx.org下载需要的源

linux下nginx的安装

以Red Hat Enterprise Linux 5为例进行讲解. 相关系列: linux下jdk的安装 linux下ant的安装 linux下redis的安装 linux下svn的安装 linux下nginx的安装 linux下graphviz的安装 linux下doxygen的安装 安装nginx版本为0.8.36 一.下载nginx 下载地址:http://www.nginx.org/ 选择nginx-0.8.36 将该下载包拷贝到/usr/local/下(随意了,找个地方就好) 二.安

深刻理解Nginx之Nginx完整安装

1.   Nginx安装 1.1预先准备 CentOS系统下,安装Nginx的库包依赖.安装命令如下: sudo yum groupinstall "DevelopmentTools" sudo yum install pcre pcre-devel sudo yum install zlib zlib-devel yum install perl-ExtUtils-Embed sudo yum install openssl openssl-devel 1.2 安装 最重要的特性和基

nginx的安装

nginx安装环境 nginx是C语言开发,建议在linux上运行,本教程使用Centos6.5作为安装环境. n  gcc 安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:yum install gcc-c++ n  PCRE PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库.nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装