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.pswp.cn/news/277198.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/277198.shtml
英文地址,請注明出處:http://en.pswp.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 的編譯環境 解決…