Ubuntu 16.04安装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:
# Default-Stop:
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO PATH=/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
fi STOP_SCHEDULE="${STOP_SCHEDULE:-QUIT/5/TERM/5/KILL/5}" test -x $DAEMON || exit . /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" ]; then
PID=/run/nginx.pid
fi if [ -n "$ULIMIT" ]; then
# Set ulimit if it is set in /etc/default/nginx
ulimit $ULIMIT
fi start_nginx() {
# Start the daemon/service
#
# Returns:
# if daemon has been started
# if daemon was already running
# if daemon could not be started
start-stop-daemon --start --quiet --pidfile $PID --exec $DAEMON --test > /dev/null \
|| return
start-stop-daemon --start --quiet --pidfile $PID --exec $DAEMON -- \
$DAEMON_OPTS >/dev/null \
|| return
} test_config() {
# Test the nginx configuration
$DAEMON -t $DAEMON_OPTS >/dev/null >&
} stop_nginx() {
# Stops the daemon/service
#
# Return
# if daemon has been stopped
# if daemon was already stopped
# if daemon could not be stopped
# other if a failure occurred
start-stop-daemon --stop --quiet --retry=$STOP_SCHEDULE --pidfile $PID --name $NAME
RETVAL="$?"
sleep
return "$RETVAL"
} reload_nginx() {
# Function that sends a SIGHUP to the daemon/service
start-stop-daemon --stop --signal HUP --quiet --pidfile $PID --name $NAME
return
} rotate_logs() {
# Rotate log files
start-stop-daemon --stop --signal USR1 --quiet --pidfile $PID --name $NAME
return
} upgrade_nginx() {
# Online upgrade nginx executable
# http://nginx.org/en/docs/control.html
#
# Return
# if nginx has been successfully upgraded
# if nginx is not running
# if the pid files were not created on time
# if the old master could not be killed
if start-stop-daemon --stop --signal USR2 --quiet --pidfile $PID --name $NAME; then
# Wait for both old and new master to write their pid file
while [ ! -s "${PID}.oldbin" ] || [ ! -s "${PID}" ]; do
cnt=`expr $cnt + `
if [ $cnt -gt ]; then
return
fi
sleep
done
# Everything is ready, gracefully stop the old master
if start-stop-daemon --stop --signal QUIT --quiet --pidfile "${PID}.oldbin" --name $NAME; then
return
else
return
fi
else
return
fi
} case "$1" in
start)
log_daemon_msg "Starting $DESC" "$NAME"
start_nginx
case "$?" in
|) log_end_msg ;;
) log_end_msg ;;
esac
;;
stop)
log_daemon_msg "Stopping $DESC" "$NAME"
stop_nginx
case "$?" in
|) log_end_msg ;;
) log_end_msg ;;
esac
;;
restart)
log_daemon_msg "Restarting $DESC" "$NAME" # Check configuration before stopping nginx
if ! test_config; then
log_end_msg # Configuration error
exit $?
fi stop_nginx
case "$?" in
|)
start_nginx
case "$?" in
) log_end_msg ;;
) log_end_msg ;; # Old process is still running
*) log_end_msg ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg
;;
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; then
log_end_msg # Configuration error
exit $?
fi reload_nginx
log_end_msg $?
;;
configtest|testconfig)
log_daemon_msg "Testing $DESC configuration"
test_config
log_end_msg $?
;;
status)
status_of_proc -p $PID "$DAEMON" "$NAME" && exit || exit $?
;;
upgrade)
log_daemon_msg "Upgrading binary" "$NAME"
upgrade_nginx
log_end_msg $?
;;
rotate)
log_daemon_msg "Re-opening $DESC log files" "$NAME"
rotate_logs
log_end_msg $?
;;
*)
echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest|rotate|upgrade}" >&
exit
;;
esac
还有一个好处,创建好的文件由于放在/usr/sbin目录下,所以能直接在终端中使用nginx命令而无需指定路径。
二、通过源码包编译安装
这种方式可以自定安装指定的模块以及最新的版本。方式更灵活。
官方下载页面:http://nginx.org/en/download.html
configure配置文件详解:http://nginx.org/en/docs/configure.html
安装gcc g++的依赖库
sudo apt-get install build-essential
sudo apt-get install libtool
安装pcre依赖库(http://www.pcre.org/)
sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev
安装zlib依赖库(http://www.zlib.net)
sudo apt-get install zlib1g-dev
安装SSL依赖库(16.04默认已经安装了)
sudo apt-get install openssl
安装Nginx
#下载最新版本:
wget http://nginx.org/download/nginx-1.13.6.tar.gz
#解压:
tar -zxvf nginx-1.13..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
配置软链接
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:
# Default-Stop:
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO PATH=/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
fi STOP_SCHEDULE="${STOP_SCHEDULE:-QUIT/5/TERM/5/KILL/5}" test -x $DAEMON || exit . /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" ]; then
PID=/run/nginx.pid
fi if [ -n "$ULIMIT" ]; then
# Set ulimit if it is set in /etc/default/nginx
ulimit $ULIMIT
fi start_nginx() {
# Start the daemon/service
#
# Returns:
# if daemon has been started
# if daemon was already running
# if daemon could not be started
start-stop-daemon --start --quiet --pidfile $PID --exec $DAEMON --test > /dev/null \
|| return
start-stop-daemon --start --quiet --pidfile $PID --exec $DAEMON -- \
$DAEMON_OPTS >/dev/null \
|| return
} test_config() {
# Test the nginx configuration
$DAEMON -t $DAEMON_OPTS >/dev/null >&
} stop_nginx() {
# Stops the daemon/service
#
# Return
# if daemon has been stopped
# if daemon was already stopped
# if daemon could not be stopped
# other if a failure occurred
start-stop-daemon --stop --quiet --retry=$STOP_SCHEDULE --pidfile $PID --name $NAME
RETVAL="$?"
sleep
return "$RETVAL"
} reload_nginx() {
# Function that sends a SIGHUP to the daemon/service
start-stop-daemon --stop --signal HUP --quiet --pidfile $PID --name $NAME
return
} rotate_logs() {
# Rotate log files
start-stop-daemon --stop --signal USR1 --quiet --pidfile $PID --name $NAME
return
} upgrade_nginx() {
# Online upgrade nginx executable
# http://nginx.org/en/docs/control.html
#
# Return
# if nginx has been successfully upgraded
# if nginx is not running
# if the pid files were not created on time
# if the old master could not be killed
if start-stop-daemon --stop --signal USR2 --quiet --pidfile $PID --name $NAME; then
# Wait for both old and new master to write their pid file
while [ ! -s "${PID}.oldbin" ] || [ ! -s "${PID}" ]; do
cnt=`expr $cnt + `
if [ $cnt -gt ]; then
return
fi
sleep
done
# Everything is ready, gracefully stop the old master
if start-stop-daemon --stop --signal QUIT --quiet --pidfile "${PID}.oldbin" --name $NAME; then
return
else
return
fi
else
return
fi
} case "$1" in
start)
log_daemon_msg "Starting $DESC" "$NAME"
start_nginx
case "$?" in
|) log_end_msg ;;
) log_end_msg ;;
esac
;;
stop)
log_daemon_msg "Stopping $DESC" "$NAME"
stop_nginx
case "$?" in
|) log_end_msg ;;
) log_end_msg ;;
esac
;;
restart)
log_daemon_msg "Restarting $DESC" "$NAME" # Check configuration before stopping nginx
if ! test_config; then
log_end_msg # Configuration error
exit $?
fi stop_nginx
case "$?" in
|)
start_nginx
case "$?" in
) log_end_msg ;;
) log_end_msg ;; # Old process is still running
*) log_end_msg ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg
;;
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; then
log_end_msg # Configuration error
exit $?
fi reload_nginx
log_end_msg $?
;;
configtest|testconfig)
log_daemon_msg "Testing $DESC configuration"
test_config
log_end_msg $?
;;
status)
status_of_proc -p $PID "$DAEMON" "$NAME" && exit || exit $?
;;
upgrade)
log_daemon_msg "Upgrading binary" "$NAME"
upgrade_nginx
log_end_msg $?
;;
rotate)
log_daemon_msg "Re-opening $DESC log files" "$NAME"
rotate_logs
log_end_msg $?
;;
*)
echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest|rotate|upgrade}" >&
exit
;;
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}
参考:
http://www.cnblogs.com/piscesLoveCc/p/5794926.html(以上部分内容转自此篇文章)
http://www.linuxidc.com/Linux/2016-08/134080.htm
https://segmentfault.com/a/1190000002797601
Ubuntu 16.04安装Nginx的更多相关文章
- Ubuntu 16.04 安装 Kodi v17 “Krypton” Alpha 2
Ubuntu 16.04 安装 Kodi v17 “Krypton” Alpha 2:sudo add-apt-repository ppa:team-xbmc/xbmc-nightlysudo ap ...
- Ubuntu 16.04安装QQ国际版图文详细教程
因工作需要,我安装了Ubuntu 16.04,但是工作上的很多事情需要QQ联系,然而在Ubuntu上的WebQQ很是不好用,于是在网上搜索了好多个Linux版本的QQ,然而不是功能不全 ...
- Ubuntu 16.04 安装Mysql 5.7 踩坑小记
title:Ubuntu 16.04 安装Mysql 5.7 踩坑小记 date: 2018.02.03 安装mysql sudo apt-get install mysql-server mysql ...
- Ubuntu 16.04安装Matlab 2016b教程
由于代码需要依赖Linux环境,只好尝试着装MATLAB,然而各种问题接踵而至,开始了由MATLAB引发的三天Linux探寻之旅-- 下载Matlab 2016b for Linux https:// ...
- ubuntu 16.04 安装 tensorflow-gpu 包括 CUDA ,CUDNN,CONDA
ubuntu 16.04 安装 tensorflow-gpu 包括 CUDA ,CUDNN,CONDA 显卡驱动装好了,如图: 英文原文链接: https://github.com/williamFa ...
- ROS_Kinetic_01 在ubuntu 16.04安装ROS Kinetic 2017.01更新
ROS_Kinetic系列学习(一),在ubuntu 16.04安装ROS Kinetic. Celebrating 9 Years of ROS! ubuntu16.04已经发布半年多了,ROS的K ...
- Ubuntu 16.04 安装 VMware Tools(解决windows和Ubuntu之间不能互相复制粘贴文件的问题)
Ubuntu 16.04安装虚拟工具VMware Tools,指的是在虚拟机VMWare安装Ubuntu 16.04后再安装VMware Tools的过程.很多人接触Linux都是从虚拟机开始,而安装 ...
- Ubuntu 16.04安装JDK(转载)
1.简单的安装方法 安装JDK的最简单方法应该就是使用apt-get来安装了,但是源一般是OpenJDK,如果需要安装Oracle的JDK这种方法就不合适了,直接跳过看下面的章节. 1.使用ctrl+ ...
- Ubuntu 16.04 安装Mysql数据库
系统环境 Ubuntu 16.04; 安装步骤 1.通过以下环境安装mysql服务端与客户端软件 sudo apt-get install mysql-server apt-get isntall m ...
随机推荐
- ios手机访问H5页面中$(document).on绑定无效问题
1.问题描述 用amazeUI做了个手机端网站,网站头部是一个点击按钮下拉菜单,点击页面其余区域下拉菜单隐藏.在chrome模拟安卓和iOS都可以正常触发,但是在真机实测的时候,iOS上面失效了.简单 ...
- Linux 下 MySQL-5.6.16 安装
转载请注明出处!!!! 卸载mysql 1 查找以前是否装有 mysql 命令:rpm -qa|grep -i mysql 2 将所有包删除 删除命令:rpm -e --nodeps 包名 3 删除 ...
- java中文和unicode编码相互转换(转)
工具类代码如下: package aa.com; import java.io.UnsupportedEncodingException; public class UnicodeUtil { pub ...
- pkuwc2018题解
题解: 思路挺好想的..然而今天写代码写成傻逼了 d1t1: 首先比较暴力的就是$f[i][j]$表示i个这个点是j的概率 然后前缀和一下dp就是$n^2$的 部分分树形态随机就说明树深度是$log$ ...
- *CTF 2019 quicksort、babyshell、upxofcpp
这次参加比赛总共出了三道,有两道队友都先交了,还是tcl,heap_master卡了差不多一天没解决....还是记录一下出的题目吧 quicksort 题目大体流程就是输入要输入的数字数量,然后输入数 ...
- java无需解压zip压缩包直接读取包内的文件名(含中文)
java自带了java.util.zip工具可以实现在不解压zip压缩包的情况下读取包内文件的文件名:(注:只能是ZIP格式的,rar我试了不行)代码如下: public static String ...
- vue vue-resource的特点
1.vue-resource 实现 get, post, jsonp请求. 2.JSONP的是实现原理: 由于浏览器的安全性限制,不允许AJAX访问 协议不同.域名不同.端口不同的数据接口,浏览器认为 ...
- PMP测试实践- 内附PMBOK中字与备考资料
最近笔者考了PMP(Project Management Professional )项目管理专业人士认证考试,主要为了系统学习下项目管理的整个过程与方法,结合PMP的理论与工作实践去更好的完成项目工 ...
- 京东B2B业务架构演变
京东 B2B 业务的定位是让各类型的企业都可以在京东的 B 平台上进行采购.建立采购关系. 京东 B2B 的用户群体主要分为 2 类,一类是大 B 用户.另一类是小 B 用户.比如联通.移动公司跟京东 ...
- LNMP环境并发优化
LNMP环境并发优化 服务器 8核32Gx3 如图是一条http请求的生命周期,共经过nginx,php-fpm,PHP三个模块 所以我们可以从nginx,php-fpm,PHP三个维度去优化 一.p ...