Ubuntu安装Nginx

在Ubuntu下安装Nginx有以下方法,但是如果想要安装最新版本的就必须下载源码包编译安装。

一、基于APT源安装

sudo apt-get install nginx

安装好的文件位置:

/usr/sbin/nginx:主程序

/etc/nginx:存放配置文件

/usr/share/nginx:存放静态文件

/var/log/nginx:存放日志

其实从上面的根目录文件夹可以知道,Linux系统的配置文件一般放在/etc,日志一般放在/var/log,运行的程序一般放在/usr/sbin或者/usr/bin。

当然,如果要更清楚Nginx的配置项放在什么地方,可以打开/etc/nginx/nginx.conf

我猜测,Nginx如果指定默认加载/etc/nginx/nginx.conf的配置文件。如果要查看加载的是哪个配置文件,可以用这个命令sudo nginx -t或者ps -ef | grep nginx

然后通过这种方式安装的,会自动创建服务,会自动在/etc/init.d/nginx新建服务脚本,然后就可以使用sudo service nginx {start|stop|restart|reload|force-reload|status|configtest|rotate|upgrade}的命令启动。

脚本如下:

#!/bin/sh### BEGIN INIT INFO
# Provides:      nginx
# Required-Start:    $local_fs $remote_fs $network $syslog $named
# Required-Stop:     $local_fs $remote_fs $network $syslog $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFOPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/nginx
NAME=nginx
DESC=nginx# Include nginx defaults if available
if [ -r /etc/default/nginx ]; then. /etc/default/nginx
fiSTOP_SCHEDULE="${STOP_SCHEDULE:-QUIT/5/TERM/5/KILL/5}"test -x $DAEMON || exit 0. /lib/init/vars.sh
. /lib/lsb/init-functions# Try to extract nginx pidfile
PID=$(cat /etc/nginx/nginx.conf | grep -Ev '^\s*#' | awk 'BEGIN { RS="[;{}]" } { if ($1 == "pid") print $2 }' | head -n1)
if [ -z "$PID" ]; thenPID=/run/nginx.pid
fiif [ -n "$ULIMIT" ]; then# Set ulimit if it is set in /etc/default/nginxulimit $ULIMIT
fistart_nginx() {# Start the daemon/service## Returns:#   0 if daemon has been started#   1 if daemon was already running#   2 if daemon could not be startedstart-stop-daemon --start --quiet --pidfile $PID --exec $DAEMON --test > /dev/null \|| return 1start-stop-daemon --start --quiet --pidfile $PID --exec $DAEMON -- \$DAEMON_OPTS 2>/dev/null \|| return 2
}test_config() {# Test the nginx configuration$DAEMON -t $DAEMON_OPTS >/dev/null 2>&1
}stop_nginx() {# Stops the daemon/service## Return#   0 if daemon has been stopped#   1 if daemon was already stopped#   2 if daemon could not be stopped#   other if a failure occurredstart-stop-daemon --stop --quiet --retry=$STOP_SCHEDULE --pidfile $PID --name $NAMERETVAL="$?"sleep 1return "$RETVAL"
}reload_nginx() {# Function that sends a SIGHUP to the daemon/servicestart-stop-daemon --stop --signal HUP --quiet --pidfile $PID --name $NAMEreturn 0
}rotate_logs() {# Rotate log filesstart-stop-daemon --stop --signal USR1 --quiet --pidfile $PID --name $NAMEreturn 0
}upgrade_nginx() {# Online upgrade nginx executable# http://nginx.org/en/docs/control.html
    ## Return#   0 if nginx has been successfully upgraded#   1 if nginx is not running#   2 if the pid files were not created on time#   3 if the old master could not be killedif start-stop-daemon --stop --signal USR2 --quiet --pidfile $PID --name $NAME; then# Wait for both old and new master to write their pid filewhile [ ! -s "${PID}.oldbin" ] || [ ! -s "${PID}" ]; docnt=`expr $cnt + 1`if [ $cnt -gt 10 ]; thenreturn 2fisleep 1done# Everything is ready, gracefully stop the old masterif start-stop-daemon --stop --signal QUIT --quiet --pidfile "${PID}.oldbin" --name $NAME; thenreturn 0elsereturn 3fielsereturn 1fi
}case "$1" instart)log_daemon_msg "Starting $DESC" "$NAME"start_nginxcase "$?" in0|1) log_end_msg 0 ;;2)   log_end_msg 1 ;;esac;;stop)log_daemon_msg "Stopping $DESC" "$NAME"stop_nginxcase "$?" in0|1) log_end_msg 0 ;;2)   log_end_msg 1 ;;esac;;restart)log_daemon_msg "Restarting $DESC" "$NAME"# Check configuration before stopping nginxif ! test_config; thenlog_end_msg 1 # Configuration errorexit $?fistop_nginxcase "$?" in0|1)start_nginxcase "$?" in0) log_end_msg 0 ;;1) log_end_msg 1 ;; # Old process is still running*) log_end_msg 1 ;; # Failed to startesac;;*)# Failed to stoplog_end_msg 1;;esac;;reload|force-reload)log_daemon_msg "Reloading $DESC configuration" "$NAME"# Check configuration before stopping nginx## This is not entirely correct since the on-disk nginx binary# may differ from the in-memory one, but that's not common.
        # We prefer to check the configuration and return an error# to the administrator.if ! test_config; thenlog_end_msg 1 # Configuration errorexit $?fireload_nginxlog_end_msg $?;;configtest|testconfig)log_daemon_msg "Testing $DESC configuration"test_configlog_end_msg $?;;status)status_of_proc -p $PID "$DAEMON" "$NAME" && exit 0 || exit $?;;upgrade)log_daemon_msg "Upgrading binary" "$NAME"upgrade_nginxlog_end_msg $?;;rotate)log_daemon_msg "Re-opening $DESC log files" "$NAME"rotate_logslog_end_msg $?;;*)echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest|rotate|upgrade}" >&2exit 3;;
esac
脚本

还有一个好处,创建好的文件由于放在/usr/sbin目录下,所以能直接在终端中使用nginx命令而无需指定路径。

二、通过源码包编译安装

这种方式可以自定安装指定的模块以及最新的版本。方式更灵活。

官方下载页面:http://nginx.org/en/download.html

configure配置文件详解:http://nginx.org/en/docs/configure.html

安装gcc g++的依赖库

Nginx是C语言开发,安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境

sudo apt-get install build-essential
sudo apt-get install libtool
centos的话:yum install gcc-c++ 

安装pcre依赖库(http://www.pcre.org/)

是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库

sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev
centos的话:yum install -y pcre pcre-devel

安装zlib依赖库(http://www.zlib.net)

zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库

sudo apt-get install zlib1g-dev
centos的话:yum install -y zlib zlib-devel

安装SSL依赖库(16.04默认已经安装了)

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。

sudo apt-get install openssl
centos的话:yum install -y openssl openssl-devel

安装Nginx

复制代码
#下载最新版本:
wget http://nginx.org/download/nginx-1.13.6.tar.gz
#解压:
tar -zxvf nginx-1.13.6.tar.gz
#进入解压目录:
cd nginx-1.13.6
#配置:
./configure --prefix=/usr/local/nginx 
#编译:
make
#安装:
sudo make install
#启动:
sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
注意:-c 指定配置文件的路径,不加的话,nginx会自动加载默认路径的配置文件,可以通过-h查看帮助命令。
#查看进程:
ps -ef | grep nginx
复制代码

关于configure、make、make install

源码的安装一般由有这三个步骤:配置(configure)、编译(make)、安装(make install)其中–prefix选项就是配置安装的路径,如果不配置该选项,安装后可执行文件默认放在/usr /local/bin,库文件默认放在/usr/local/lib,配置文件默认放在/usr/local/etc,其它的资源文件放在/usr /local/share,比较分散。为了便于集中管理某个软件的各种文件,可以配置–prefix,如:
./configure –prefix=/usr/local
可以把所有资源文件放在/usr/local的路径中,就不会分散了。

配置软链接

sudo ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx

现在就可以不用路径直接输入nginx启动。

配置开机启动服务

在/etc/init.d/下创建nginx文件,sudo vim /etc/init.d/nginx,内容如下:

#!/bin/sh### BEGIN INIT INFO
# Provides:      nginx
# Required-Start:    $local_fs $remote_fs $network $syslog $named
# Required-Stop:     $local_fs $remote_fs $network $syslog $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFOPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/nginx/sbin/nginx
NAME=nginx
DESC=nginx# Include nginx defaults if available
if [ -r /etc/default/nginx ]; then. /etc/default/nginx
fiSTOP_SCHEDULE="${STOP_SCHEDULE:-QUIT/5/TERM/5/KILL/5}"test -x $DAEMON || exit 0. /lib/init/vars.sh
. /lib/lsb/init-functions# Try to extract nginx pidfile
PID=$(cat /usr/local/nginx/conf/nginx.conf | grep -Ev '^\s*#' | awk 'BEGIN { RS="[;{}]" } { if ($1 == "pid") print $2 }' | head -n1)
if [ -z "$PID" ]; thenPID=/run/nginx.pid
fiif [ -n "$ULIMIT" ]; then# Set ulimit if it is set in /etc/default/nginxulimit $ULIMIT
fistart_nginx() {# Start the daemon/service## Returns:#   0 if daemon has been started#   1 if daemon was already running#   2 if daemon could not be startedstart-stop-daemon --start --quiet --pidfile $PID --exec $DAEMON --test > /dev/null \|| return 1start-stop-daemon --start --quiet --pidfile $PID --exec $DAEMON -- \$DAEMON_OPTS 2>/dev/null \|| return 2
}test_config() {# Test the nginx configuration$DAEMON -t $DAEMON_OPTS >/dev/null 2>&1
}stop_nginx() {# Stops the daemon/service## Return#   0 if daemon has been stopped#   1 if daemon was already stopped#   2 if daemon could not be stopped#   other if a failure occurredstart-stop-daemon --stop --quiet --retry=$STOP_SCHEDULE --pidfile $PID --name $NAMERETVAL="$?"sleep 1return "$RETVAL"
}reload_nginx() {# Function that sends a SIGHUP to the daemon/servicestart-stop-daemon --stop --signal HUP --quiet --pidfile $PID --name $NAMEreturn 0
}rotate_logs() {# Rotate log filesstart-stop-daemon --stop --signal USR1 --quiet --pidfile $PID --name $NAMEreturn 0
}upgrade_nginx() {# Online upgrade nginx executable# http://nginx.org/en/docs/control.html
    ## Return#   0 if nginx has been successfully upgraded#   1 if nginx is not running#   2 if the pid files were not created on time#   3 if the old master could not be killedif start-stop-daemon --stop --signal USR2 --quiet --pidfile $PID --name $NAME; then# Wait for both old and new master to write their pid filewhile [ ! -s "${PID}.oldbin" ] || [ ! -s "${PID}" ]; docnt=`expr $cnt + 1`if [ $cnt -gt 10 ]; thenreturn 2fisleep 1done# Everything is ready, gracefully stop the old masterif start-stop-daemon --stop --signal QUIT --quiet --pidfile "${PID}.oldbin" --name $NAME; thenreturn 0elsereturn 3fielsereturn 1fi
}case "$1" instart)log_daemon_msg "Starting $DESC" "$NAME"start_nginxcase "$?" in0|1) log_end_msg 0 ;;2)   log_end_msg 1 ;;esac;;stop)log_daemon_msg "Stopping $DESC" "$NAME"stop_nginxcase "$?" in0|1) log_end_msg 0 ;;2)   log_end_msg 1 ;;esac;;restart)log_daemon_msg "Restarting $DESC" "$NAME"# Check configuration before stopping nginxif ! test_config; thenlog_end_msg 1 # Configuration errorexit $?fistop_nginxcase "$?" in0|1)start_nginxcase "$?" in0) log_end_msg 0 ;;1) log_end_msg 1 ;; # Old process is still running*) log_end_msg 1 ;; # Failed to startesac;;*)# Failed to stoplog_end_msg 1;;esac;;reload|force-reload)log_daemon_msg "Reloading $DESC configuration" "$NAME"# Check configuration before stopping nginx## This is not entirely correct since the on-disk nginx binary# may differ from the in-memory one, but that's not common.
        # We prefer to check the configuration and return an error# to the administrator.if ! test_config; thenlog_end_msg 1 # Configuration errorexit $?fireload_nginxlog_end_msg $?;;configtest|testconfig)log_daemon_msg "Testing $DESC configuration"test_configlog_end_msg $?;;status)status_of_proc -p $PID "$DAEMON" "$NAME" && exit 0 || exit $?;;upgrade)log_daemon_msg "Upgrading binary" "$NAME"upgrade_nginxlog_end_msg $?;;rotate)log_daemon_msg "Re-opening $DESC log files" "$NAME"rotate_logslog_end_msg $?;;*)echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest|rotate|upgrade}" >&2exit 3;;
esac
脚本

-

#设置服务脚本有执行权限
sudo chmod +x /etc/init.d/nginx
#注册服务
cd /etc/init.d/ sudo update-rc.d nginx defaults

现在基本上就可以开机启动了,常用的命令如下:

sudo service nginx {start|stop|restart|reload|force-reload|status|configtest|rotate|upgrade}

 

转载于:https://www.cnblogs.com/dongye95/p/11196118.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/277198.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

9.28PMP每日一题

控制质量过程的输出是:A、变更请求B、批准的变更请求C、工作绩效数据D、事业环境因素 答案将于明天和新题一起发布!9.27试题答案:C转载于:https://blog.51cto.com/13554215/2287292

将数组绑定到dropdownlist上

<% Page Language"C#" %><% Import Namespace"System.Data" %><Script Language"C#" Runat"Server">public void Page_Load(Object src,EventArgs e){ //首先建立一个数组 ArrayList arrnew ArrayList(); …

HTML标题h,HTML H标题标签

可以将HTML标题或HTML h标签定义为要在网页上显示的标题或副标题。当你将文本放在标题标签……… h1>内时, 它在浏览器中以粗体显示, 并且文本的大小取决于标题的数量。从到标签定义了六个不同的HTML标题, 从最高级别h1(主标题)到最低级别h6(最重要的标题)。h1是最大的标题标…

汤姆大叔的6道javascript编程题题解

1、找出数字数组中最大的元素&#xff08;使用Math.max函数&#xff09; 123var a [1, 2, 3, 6, 5, 4];var ans Math.max.apply(null, a);console.log(ans); // 6这题很巧妙地用了apply&#xff0c;如果不是数组&#xff0c;是很多数字求最大值&#xff0c;我们知道可以这样…

Android 多线程之几个基本问题

Android中的进程和线程 Android中的一个应用程序一般就对应着一个进程&#xff0c;多进程的情况可以参考Android 多进程通信之几个基本问题 Android中更常见的是多线程的情况&#xff0c;一个应用程序中一般都有包括UI线程等多个线程。Android中规定网络访问必须在子线程中进行…

Web下的整体测试

随着Internet的日益普及&#xff0c;现在基于B/S结构的大型应用越来越多&#xff0c;可如何对这些应用进行测试成为日益迫切的问题。有许多测试人员来信问我B/S的测试如何做&#xff0c;由于工作较繁忙&#xff0c;对大家提出的问题也是头痛医头脚痛医脚&#xff0c;没有对WEB的…

用计算机算算术平方根顺序是ON然后是什么,第2课时用计算器求一个正数的算术平方根.ppt...

1.比较下列各组数的大小&#xff1a;(1) 与(2) 与 8依次按键显示&#xff1a;1.732 050 808例3 小丽想用一块面积为400 cm2的正方形纸片&#xff0c;沿着边的方向剪出一块面积为300 cm2的长方形纸片&#xff0c;使它的长宽之比为3:2&#xff0e;她不知能否裁得出来&#xff0c;…

MySQL 命令

版权声明&#xff1a;本文首发 http://asing1elife.com &#xff0c;转载请注明出处。 https://blog.csdn.net/asing1elife/article/details/82892834 MySQL 一些常见命令 更多精彩 更多技术博客&#xff0c;请移步 asing1elife’s blog 查看版本号 mysql -V重启/启动/停止 mys…

Bookshelf 2 简单DFS

链接&#xff1a;https://ac.nowcoder.com/acm/contest/993/C来源&#xff1a;牛客网 题目描述 Farmer John recently bought another bookshelf for the cow library, but the shelf is getting filled up quite quickly, and now the only available space is at the top.FJ…

一步一步SharePoint 2007之五:向网站中添加一个子网站

一步一步SharePoint 2007之五&#xff1a;向网站中添加一个子网站摘要感受完看到成果的激动&#xff0c;感受完邻家女孩的漂亮、可爱和端庄&#xff0c;不要停止&#xff0c;来&#xff0c;让我们一起来动手打造心目中的完美女神吧&#xff01;本篇文章将介绍如何向一个网站中添…

微型计算机系统分为哪几个层次,计算机系统分为哪4层?

满意答案al053192014.06.23采纳率&#xff1a;49% 等级&#xff1a;12已帮助&#xff1a;7516人第一层&#xff1a;物理层(PhysicalLayer)&#xff0c;规定通信设备的机械的、电气的、功能的和过程的特性&#xff0c;用以建立、维护和拆除物理链路连接。具体地讲&#xff0c…

ASP.NET Core 基础教程 - ASP.NET Core 基础教程 - 简单教程,简单编程

原文:ASP.NET Core 基础教程 - ASP.NET Core 基础教程 - 简单教程&#xff0c;简单编程 ASP.NET Core 是对 ASP.NET 有重大意义的一次重新设计。本章节我们将介绍 ASP.NET Core 中的一些新的概念和它们是如何帮助我们开发现代化的 Web 应用程序 尽管 ASP.NET Core 是跨平台的&a…

参数初始化

通过以下形式进行参数初始化 self.fc nn.Linear(n_head * d_v, d_model) nn.init.xavier_normal_(self.fc.weight) 转载于:https://www.cnblogs.com/yeran/p/11197047.html

使用input type=file 上传文件时需注意

在asp.net的中使用<input typefile />控件上传文件对文件的大小有限制,默认情况下大概在4m左右,如果上传再大的文件时就会出页面无法显示的错误.修改web.config文件中的参数可以设置该控件上传文件的大小,web.config中配置如下:在<system.web>节点下增加"<…

html模板 循环里if,django模板里循环变量table里想要两个一行如何控制

2016-8-3 周三做项目时遇到的问题&#xff1a;每个div由循环变量输出&#xff1a;{% for key,value in formextenddetail %}{{ key }}{{ value }}{% endfor %}但是我想两个div一行&#xff0c;使用...这种样子因为我负责的是前端&#xff0c;views这些不是很熟悉&#xff0c;想…

ASP.NET Core Windows 环境配置 - ASP.NET Core 基础教程 - 简单教程,简单编程

原文:ASP.NET Core Windows 环境配置 - ASP.NET Core 基础教程 - 简单教程&#xff0c;简单编程 ASP.NET Core Windows 环境配置 ASP.NET Core 是对 ASP.NET 有重大意义的一次重新设计。本章节我们将介绍 ASP.NET Core 中的一些新的概念和它们是如何帮助我们开发现代化的 Web 应…

Prim算法求最小生成树

给定一个n个点m条边的无向图&#xff0c;图中可能存在重边和自环&#xff0c;边权可能为负数。 求最小生成树的树边权重之和&#xff0c;如果最小生成树不存在则输出impossible。 给定一张边带权的无向图G(V, E)&#xff0c;其中V表示图中点的集合&#xff0c;E表示图中边的集合…

用installshield打包的asp.net程序

现在需要打包一个asp.net程序&#xff0c;具体要求如下&#xff1a;1、动态建立web虚拟目录或web站点&#xff0c;并保存至web.config文件相关字段中。2、动态建立ftp目录&#xff0c;并保存至web.config文件相关字段中。3、动态建立数据库联接类型sql或oracle&#xff0c;对sq…

合肥工业大学计算机学院王院长,王青山(合肥工业大学教授)_百度百科

王青山(合肥工业大学教授)语音编辑锁定讨论上传视频本词条缺少概述图&#xff0c;补充相关内容使词条更完整&#xff0c;还能快速升级&#xff0c;赶紧来编辑吧&#xff01;王青山&#xff0c;中国汉族人&#xff0c;合肥工业大学教授&#xff0c;现任中国计算机学会、ACM、IEE…

go 中gcc 编译问题(gcc.exe fatal error no input files compilation terminated)

2019独角兽企业重金招聘Python工程师标准>>> 问题背景 在windows 中编译 go 的pipe时由于pipe依赖sqlite&#xff0c;需要通过cgo进行编译。出现如下异常 gcc.exe fatal error no input files compilation terminated 问题分析 由于 windows中缺少c 的编译环境 解决…