Linux下源碼安裝並配置Nginx

来源:https://www.cnblogs.com/connect/archive/2018/07/28/nginx-install-src.html
-Advertisement-
Play Games

Linux下源碼安裝並配置Nginx,並將nginx命令添加到系統環境變數,將nginx添加到系統服務中 ...


實驗環境

  1. 一臺最小化安裝的CentOS 7.3 虛擬機

安裝nginx

安裝nginx依賴包

yum install -y pcre-devel zlib-devel openssl-devel wget gcc tree vim

Nginx依賴於pcre、zlib、openssl,在編譯前配置時如果有問題
可以使用yum方式安裝三個包(pcre-devel、zlib-devel、openssl-devel)

從Nginx官網下載Nginx源碼包

wget http://nginx.org/download/nginx-1.12.2.tar.gz

解壓Nginx源碼包到/root/nginx,並查看Nginx源文件結構

tar -xzvf nginx-1.12.2.tar.gz

/root/nginx目錄進行編譯前配置

cd /root/nginx*
./configure --prefix=/usr/local/nginx --with-http_ssl_module

/root/nginx目錄執行編譯安裝

make && make install

啟動nginx

關閉防火牆

setenforce 0
systemctl stop firewalld
systemctl disable firewalld

進入到安裝目錄/usr/local/nginx,查看目錄結構

cd /usr/local/nginx
pwd
ls

啟動Nginx

/usr/local/nginx/sbin/nginx

查看Nginx進程是否啟動

ps aux | grep nginx

查看Nginx占用的埠號

netstat -tlnp

使用本地主機訪問虛擬機上的Nginx伺服器

停止nginx

停止Nginx的三種方式

# 1. 立即停止Nginx服務
/usr/local/nginx/sbin/nginx -s stop

# 2.完成當前任務後停止
/usr/local/nginx/sbin/nginx -s quit

# 3.殺死Nginx進程
killall nginx

把nginx命令添加到環境變數

使用軟連接將nginx鏈接到/usr/local/sbin

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
ll /usr/local/sbin/ | grep "nginx"

顯示當前環境變數PATH

echo $PATH

編輯.bash_profile文件

vim ~/.bash_profile

.bash_profile文件末尾加入以下內容

export PATH=$PATH:/usr/local/nginx/sbin

引用.bash_profile文件

source ~/.bash_profile

使用nginx命令

# 啟動nginx
nginx
# 停止nginx
nginx -s quit

nginx命令添加到系統服務

創建並編輯文件/root/service-nginx.sh

#!/bin/sh
#
# filename: service-nginx.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/sbin/nginx"
prog=$(basename $nginx)

#NGINX_CONF_FILE="/etc/nginx/nginx.conf"
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -n "$user" ]; then
      if [ -z "`grep $user /etc/passwd`" ]; then
         useradd -M -s /bin/nologin $user
      fi
      options=`$nginx -V 2>&1 | grep 'configure arguments:'`
      for opt in $options; do
          if [ `echo $opt | grep '.*-temp-path'` ]; then
              value=`echo $opt | cut -d "=" -f 2`
              if [ ! -d "$value" ]; then
                  # echo "creating" $value
                  mkdir -p $value && chown -R $user $value
              fi
          fi
       done
    fi
}

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    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
}

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
# END

/root/service-nginx.sh替換/etc/init.d/nginx

mv /root/service-nginx.sh /etc/init.d/nginx

賦予可執行限權

chmod 755 /etc/init.d/nginx

執行

systemctl start nginx

源碼方式安裝nginx,自動化安裝腳本

#!/bin/bash

# installation configuration
NGINX_VERSION=1.12.2
NGINX_SRC_PATH=/root
NGINX_BIN_PATH=/usr/local/nginx

# disable firewall
systemctl stop firewalld
setenforce 0

# installation dependence
yum install -y pcre-devel zlib-devel openssl-devel wget gcc

# download nginx source package
cd ${NGINX_SRC_PATH}
wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz

# unzip source package
tar -xzvf nginx-${NGINX_VERSION}.tar.gz
cd ./nginx-${NGINX_VERSION}

# install nginx
./configure --prefix=${NGINX_BIN_PATH} --with-http_ssl_module
make & make install

# start nginx service
cd ${NGINX_BIN_PATH}/sbin
./nginx

# END

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 最近有童鞋有這種需求,說實話我不知道這個Panel怎麼起名字。 效果連接https://tuchong.com/tags/風光/ 下麵是我做成的效果,可以規定每個Row的Items個數 2個 3個 4個 代碼在:GitHub 下麵我來說一下我的思路 其實很早之前就寫過這種可變大小的控制項,但這次的跟這 ...
  • .NET 性能優化小技巧 Intro 之前做了簡訊發送速度的提升,在大師的指導下,發送簡訊的速度有了極大的提升,學到了一些提升 .NET 性能的一些小技巧 HttpClient 優化 關於使用 ,大概很多人都知道儘量使用單例以提升 的性能。 由於 在發送請求時需要進行功能變數名稱解析,使用的時候第一次一般來 ...
  • 對稱加密(向量) DES加密 倒序加1加密解密 Base64編解碼 ...
  • 0.簡介 緩存在一個業務系統中十分重要,常用的場景就是用來儲存調用頻率較高的數據。Abp 也提供了一套緩存機制供用戶使用,在使用 Abp 框架的時候可以通過註入 來新建/設置緩存。 同時 Abp 框架也提供了 Redis 版本的 實現,你也可以很方便的將現有的記憶體緩存替換為 Redis 緩存。 0. ...
  • var httpService = angular.module('httpService', []);httpService.factory("$httpService",function ($http, $q) { var $httpService = {}; $httpService.getD ...
  • StreamReader sr = new StreamReader(path); //path是要讀取的文件的完整路徑 String str_read = sr.ReadToEnd(); //從開始到末尾讀取文件的所有內容,str_read 存放的就是讀取到的文本 sr.Close(); //讀完 ...
  • 我這裡使用的時centos7-mini,centos系統本身預設安裝有python2.x,版本x根據不同版本系統有所不同,可通過 python --V 或 python --version 查看系統自帶的python版本 有一些系統命令時需要用到python2,不能卸載 1、安裝依賴包 1)首先安裝 ...
  • 今天郵箱里發現有一封某伺服器inode使用率發生告警的郵件 登錄到伺服器上df i查看,發現/路徑下91%,磁碟使用率卻不高,猜測可能是某個目錄下的小文件過多,進而造成inode占用率過高,但不清楚根路徑下各文件夾里的文件數 於是乎執行以下命令,查看根路徑下各國文件夾的文件數 for i in / ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...