Ubuntu安装nginx

简介:

是一个异步框架的Web服务器,也可以用作反向代理。现在常用在web服务器中,本文章介绍如何安装nginx.

  • 安装gcc和g++
apt-get install build-essential
apt-get install libtool

这里写图片描述

  • 安装pcre依赖库
apt-get install libpcre3 libpcre3-dev
  • 安装zlib依赖库
apt-get install zlib1g-dev
  • 安装openssl依赖库
apt-get install openssl
apt-get install libssl-dev      // 未安装该库会报 SSL modules require the OpenSSL library.
  • 安装nginx

nginx的依赖库顺利安装完成后就可以正式开始安装nginx了,先去官网下载最新版本的源码包。下载地址为:nginx下载地址

下载完成后将包上传到服务器某个目录下。然后解压安装。

1、配置nginx

tar -zxvf nginx-1.14.0.tar.gz

解压后可以进入目录并看目录文件:

cd nginx-1.14.0/

这里写图片描述

可以看到有一个特别颜色的configure文件。该文件的功能主要是配置nginx。
在该目录下执行配置文件的命令:

./configure --prefix=/usr/local/nginx  --with-http_ssl_module                // 安装在local/nginx路径下 --with-http_ssl_module主要是配置https时用到,不加上这个参数的话到时启动https会找不到ssl模块

–with-http_ssl_module主要是配置https时用到,不加上这个参数的话到时启动https会找不到ssl模块

2、编译nginx

make

这里写图片描述

3、安装nginx

make install

安装完成后,可以在我们之前配置的usr/local文件夹下看到有nginx的文件夹。
这里写图片描述

进入usr/local/nginx/conf目录下可以看到配置文件nginx.conf,往后对nginx的配置修改主要就是修改这个文件。
这里写图片描述

至此,nginx就算安装完成了。如果没进一步的需求到这里就可以结束了。

下面是对使用nginx的一些便利配置:

  • 配置软连接
    配置软连接后,以后启动nginx直接输入nginx即可。配置方法是直接输入命令行:
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx 

配置完成后,可以试试:
这里写图片描述
这里写图片描述
可以看到10450 10451是nginx的进程。

  • 配置开机启动服务
    进入/etc/init.d目录下创建nginx文件。
vim nginx

切换vim插入模式后粘贴如下脚本:

#!/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 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 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" ]; 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:
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
start-stop-daemon --start --quiet --pidfile $PID --exec $DAEMON --test > /dev/null \
|| return 1
start-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 occurred
start-stop-daemon --stop --quiet --retry=$STOP_SCHEDULE --pidfile $PID --name $NAME
RETVAL="$?"
sleep 1
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 0
}

rotate_logs() {
# Rotate log files
start-stop-daemon --stop --signal USR1 --quiet --pidfile $PID --name $NAME
return 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 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 + 1`
if [ $cnt -gt 10 ]; then
return 2
fi
sleep 1
done
# Everything is ready, gracefully stop the old master
if start-stop-daemon --stop --signal QUIT --quiet --pidfile "${PID}.oldbin" --name $NAME; then
return 0
else
return 3
fi
else
return 1
fi
}

case "$1" in
start)
log_daemon_msg "Starting $DESC" "$NAME"
start_nginx
case "$?" in
0|1) log_end_msg 0 ;;
2) log_end_msg 1 ;;
esac
;;
stop)
log_daemon_msg "Stopping $DESC" "$NAME"
stop_nginx
case "$?" in
0|1) log_end_msg 0 ;;
2) log_end_msg 1 ;;
esac
;;
restart)
log_daemon_msg "Restarting $DESC" "$NAME"

# Check configuration before stopping nginx
if ! test_config; then
log_end_msg 1 # Configuration error
exit $?
fi

stop_nginx
case "$?" in
0|1)
start_nginx
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_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; then
log_end_msg 1 # 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 0 || 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}" >&2
exit 3
;;
esac

粘贴完成后输入:wq保存。

单是这样配置文件还不足以开机启动,还得让文件具备运行权限。刚才创建未获得权限的文件会有颜色标注,如下图:
这里写图片描述

在创建文件的目录下输入权限命令:

chmod +x nginx

然后文件就获取了运行的权限。
这里写图片描述

获得权限后还需要注册nginx的服务。

update-rc.d nginx defaults

这里写图片描述

完成后就可以使用service nginx start/stop来控制nginx的启动及停止了。
附上常用命令:

service nginx start|stop|restart|reload|force-reload|status|configtest|rotate|upgrade
  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值