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

1
2
[root@master ~]# groupadd nginx 
[root@master ~]# useradd -g nginx nginx

关闭系统防火墙,

1
2
[root@master ~]# service iptables stop 
[root@master ~]# chkconfig iptables off

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

1
2
3
4
5
6
[root@master ~]# wget http://nginx.org/download/nginx-1.8.0.tar.gz 
[root@master ~]# tar -xf nginx-1.8.0.tar.gz -C /usr/local/src 
[root@master ~]# cd /usr/local/src/nginx-1.8.0 
[root@master nginx-1.8.0]# ./configure --user=nginx --group=nginx 
--with-http_ssl_module \
--with-http_sub_module

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

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

1
2
3
4
./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的开发包,

1
2
3
4
./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.

1
2
3
4
[root@master ~]# yum install -y pcre-devel 
[root@master ~]# yum install -y gcc 
[root@master ~]# yum install -y zlib-devel 
[root@master ~]# yum install -y openssl-devel

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

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

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

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

1
[root@master ~]# /usr/local/nginx/sbin/nginx -c /usr/loca/nginx/conf/nginx.conf

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@master ~]# 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
[root@master ~]# 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自动启动,可以执行如下命令:

1
2
[root@master ~]# echo “/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
” >> /etc/rc.local

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
[root@master ~]# 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命令对其进行管理,

1
2
[root@master ~]# chmod 755 /etc/init.d/nginx
[root@master ~]# chkconfig nginx on

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

1
[root@master ~]# service nginx reload

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

1
[root@master ~]# /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进程。

版权声明:原创作品,如需转载,请注明出处。否则将追究法律责任
源代码Nginx编译安装
本文转自    bigstone2012   51CTO博客,原文链接:http://blog.51cto.com/lavenliu/1663792

Nginx源代码安装相关推荐

  1. 《精通Nginx》——1.2 从源代码安装Nginx

    本节书摘来自异步社区<精通Nginx>一书中的第1章,第1.2节,作者: [瑞士]Dimitri Aivaliotis 更多章节内容可以访问云栖社区"异步社区"公众号查 ...

  2. 在Centos7下源代码安装配置Nginx

    简介 本博文主要是针对开发或者测试人员能快速搭建Nginx负载均衡环境,使用不同的负载均衡策略去配置nginx,文章的内容主要参考Nginx的官方去配置和文档说明.本文主要注重搭建实践过程,并没有太多 ...

  3. CentOS 6.4用源代码安装LNMP环境

    2019独角兽企业重金招聘Python工程师标准>>> CentOS 6.4用源代码安装LNMP环境 对于在Linux系统中安装web服务其实都是差不多的,只是软件的选择不一样,不过 ...

  4. Nginx编译-安装-配置-优化实践总结

    http://www.zhuxiaodong.net/2016/configure-nginx-server-support-https-http2-on-centos7/ 一些更新说明: 2016- ...

  5. 千亿流量拦截控制处理技术-Nginx(安装 命令 路由匹配 负载均衡 常用配置)

    千亿流量拦截控制处理技术-Nginx Nginx使用与配置 1 什么是nginx 1.1 可大量并行处理 1.2 与 Apache 相比 1.2.1 Apache VS Nginx 1.2.2 常用w ...

  6. Docker安装Nginx,初学者也能让您轻松玩转Nginx的安装

    目录 从之前几期开始开始,我们一直在分享Linux的一些常见系统操作以及一些常用操作系统的安装. 如果您感兴趣的话,也可以看看我们之前的帖子 Docker安装BookStack,打造属于您公司自己的开 ...

  7. CentOS7.9下nginx的安装与配置(实现任意目录下均可直接执行 nginx 命令,以及开机自启动)

    目录 1.​​​​​​Nginx介绍 2.Nginx和apache的优缺点 2.1nginx相对于apache的优点: 2.2apache 相对于nginx 的优点: 3.Tengine介绍 3.1t ...

  8. 【Linux】在Ubuntu下部署nginx——nginx的安装与卸载

    介绍 这里是小编成长之路的历程,也是小编的学习之路.希望和各位大佬们一起成长! 以下为小编最喜欢的两句话: 要有最朴素的生活和最遥远的梦想,即使明天天寒地冻,山高水远,路远马亡. 一个人为什么要努力? ...

  9. 3-1 Nginx编译安装

    文章目录 Nginx服务 一.Nginx服务基础 1.Nginx简介 2.简述Nginx和Apache的差异 3.编译安装Nginx服务(Nginx-1.12.2) 1)环境准备:关闭防火墙,上传软件 ...

最新文章

  1. 华数软件测试岗位,重磅:字节跳动与华数共同研发的电视原创视频app已正式测试上线...
  2. 人脸识别技术法律缺口亟待补上
  3. 数字图像处理:第四章 点运算
  4. 从“鞭打快牛”故事来看团队的领导力
  5. 启动万维网发布服务(W3SVC)
  6. vue获取table一列数据_vue中比较重要的小知识点
  7. 在线最大公因数计算器
  8. php比较输入数字大小,jquery怎么比较两个数字大小
  9. Vue实例与组件实例
  10. 深蓝-视觉slam-第三节习题
  11. imac html5播放器,超给力的五款Mac最佳高清音乐播放器
  12. 计算机论文怎样投稿sci,计算机集成论文怎么投稿sci期刊
  13. Unity3d Ugui 20 Grid Layout Group Aspect Ratio Fitter
  14. Java 创建带有套接字的简单代理服务器示例
  15. IP对亚马逊测评自养号有多重要?
  16. 线性回归系数的几个性质
  17. Android使用Github Actions持续集成并自动上传apk到蒲公英App内测分发平台(含证书密码脱敏)
  18. 西安交大计算机在线作业答案,西安交大电路在线作业及答案.docx
  19. 获取顺序栈的栈顶元素
  20. abs、fabs、fabsf函数的用法区别

热门文章

  1. red hat linux 7.1 使用手册!,Red Hat Linux 7.1使用手册(中)
  2. python的for循环语句_干货丨Python的循环语句基础讲解!
  3. 计算机网络查看路由表命令,如何查看、添加、修改、删除路由器内部的路由表项?...
  4. maven netty 配置_SpringBoot整合Netty(附源码)
  5. 【linux】CMake Error in CMakeLists.txt: No CMAKE_CXX_COMPILER could be found.
  6. 极简写作语言-Markdown
  7. 【杂谈】来自一个懵懂青年的表白!
  8. 全球及中国液压磁路保护器行业发展规划与产销需求预测报告2022版
  9. 全球及中国木材加工行业运行状况与投资产值预测报告2022版
  10. 隐藏系统运行对话框立即启用