首頁  >  文章  >  運維  >  Ubuntu上安裝Nginx伺服器程式及簡單環境配置的方法

Ubuntu上安裝Nginx伺服器程式及簡單環境配置的方法

王林
王林轉載
2023-05-12 12:31:061289瀏覽

ubuntu 從官方來源安裝nginx

cd ~ 
wget http://nginx.org/keys/nginx_signing.key 
sudo apt-key add nginx_signing.key 
sudo nano /etc/apt/sources.list   # 添加以下两句 
deb http://nginx.org/packages/ubuntu/ precise nginx 
deb-src http://nginx.org/packages/ubuntu/ precise nginx 
sudo apt-get update 
sudo apt-get install nginx

ubuntu 從ppa來源安裝nginx :
##

sudo add-apt-repository ppa:nginx/stable 
sudo apt-get update 
sudo apt-get install nginx

ubuntu 從常規來源安裝nginx :

sudo apt-get install nginx

編譯安裝nginx

wget http://nginx.org/packages/mainline/ubuntu/pool/nginx/n/nginx/nginx_1.5.7-1~precise_i386.deb 
wget http://nginx.org/download/nginx-1.5.7.tar.gz
tar xzf nginx-1.5.7.tar.gz
cd nginx-1.5.7

(注意:nginx1.5.7是mainline版而非stable版)

為了方便開發和管理,我在根目錄下新建了一個png目錄,並且目錄所有者設置為當前用戶,nginx就編譯在/png/nginx/1.5.7下面:


sudo mkdir /png
sudo chown eechen:eechen /png

運行用戶我定義為png:png,所以我需要新建這樣一個用戶:


sudo addgroup png --system
sudo adduser png --system --disabled-login --ingroup png --no-create-home --home /nonexistent --gecos "png user" --shell /bin/false

(新建用戶的命令可以參考官方deb包裡的預安裝腳本debian/preinst)

編譯參數參考了nginx官方提供的deb套件(nginx -v可見).


./configure \
--prefix=/png/nginx/1.5.7 \
--sbin-path=/png/nginx/1.5.7/sbin/nginx \
--conf-path=/png/nginx/1.5.7/conf/nginx.conf \
--error-log-path=/png/nginx/1.5.7/var/log/error.log \
--http-log-path=/png/nginx/1.5.7/var/log/access.log \
--pid-path=/png/nginx/1.5.7/var/run/nginx.pid \
--lock-path=/png/nginx/1.5.7/var/run/nginx.lock \
--http-client-body-temp-path=/png/nginx/1.5.7/var/cache/client_temp \
--http-proxy-temp-path=/png/nginx/1.5.7/var/cache/proxy_temp \
--http-fastcgi-temp-path=/png/nginx/1.5.7/var/cache/fastcgi_temp \
--http-uwsgi-temp-path=/png/nginx/1.5.7/var/cache/uwsgi_temp \
--http-scgi-temp-path=/png/nginx/1.5.7/var/cache/scgi_temp \
--user=png \
--group=png \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-ipv6

注意:這一步按錯誤提示安裝依賴的包,這時就是apt要發威的時候了,比如我的系統安裝了這些包:


sudo apt-get -y install \
build-essential \
autoconf \
libtool \
libxml2 \
libxml2-dev \
openssl \
libcurl4-openssl-dev \
libbz2-1.0 \
libbz2-dev \
libjpeg-dev \
libpng12-dev \
libfreetype6 \
libfreetype6-dev \
libldap-2.4-2 \
libldap2-dev \
libmcrypt4 \
libmcrypt-dev \
libmysqlclient-dev \
libxslt1.1 \
libxslt1-dev \
libxt-dev \
libpcre3-dev

安裝好這些包後,下次編譯新版nginx就不用再裝了,而且基本也滿足編譯php時configure的需求.

好了,configure成功後就可以編譯安裝了:

time make && make install

time主要用來查看本次編譯耗時.

編譯好後可以看看這個傢伙的個​​頭:

du -sh /png/nginx/1.5.7/sbin/nginx
5.5m /png/nginx/1.5.7/sbin/nginx

環境簡單配置總結
減少nginx編譯後的檔案大小:
編輯原始檔nginx-1.5. 7/auto/cc/gcc 去除debug訊息(註解掉即可):

# debug 
# cflags="$cflags -g"

這樣編譯後的主程式大小則為700多k,和nginx官方提供的deb套件程式大小相近.

另外configure時去掉一些不需要的模組,編譯後的可執行檔會更小.
當然,我需要一個服務腳本用來管理nginx,這時同樣可以藉助官方deb包裡提供的服務腳本etc/init.d/nginx.
我把它放到/png/nginx/1.5.7/nginx,對開頭定義的幾個值(13到19行)進行稍加修改:

path=/sbin:/usr/sbin:/bin:/usr/bin
desc=nginx
name=nginx
conffile=/etc/nginx/nginx.conf
daemon=/usr/sbin/nginx
pidfile=/var/run/$name.pid
scriptname=/etc/init.d/$name
改为
path=/sbin:/usr/sbin:/bin:/usr/bin
desc=nginx
name=nginx
conffile=/png/nginx/1.5.7/conf/nginx.conf
daemon=/png/nginx/1.5.7/sbin/nginx
pidfile=/png/nginx/1.5.7/var/run/$name.pid
scriptname=/png/nginx/1.5.7/$name

啟動前先建立一個cache目錄,否則會提示出錯:


mkdir /png/nginx/1.5.7/var/cache

啟動nginx:


sudo /png/nginx/1.5.7/nginx start

測試頁面:


curl -i `hostname`

看一下連接埠:


sudo netstat -antp|grep nginx

查看一下它所佔用的記憶體:

htop按f4過濾nginx

Ubuntu上安裝Nginx伺服器程式及簡單環境配置的方法


Ubuntu上安裝Nginx伺服器程式及簡單環境配置的方法





##用top同樣能看到類似內容:


top -b -n1|head -n7 && top -b -n1|grep nginx

#主要看res這個值,常駐記憶體(resident),不包括swap空間的物理記憶體, 單位為kb,%mem就是以res為參照物件.###可以看到nginx兩個行程佔用的實體記憶體加起來不到2m,記憶體佔用非常小.###另外top裡的res這個值對應ps aux裡的rss這個值:######
ps aux|head -n1 && ps aux|grep nginx
###還有我們可以看到nginx的工人進程只有一個線程:######
cat /proc/25047/status|grep threads
threads: 1
###其中25047是nginx工人進程pid號.###把nginx做成系統服務,開機自啟動:######
sudo ln -s /png/nginx/1.5.7/nginx /etc/init.d/png-nginx
sudo update-rc.d png-nginx defaults #开机自启动 
sudo update-rc.d -f png-nginx remove # 以后不想开机自启动可以这样禁止
sudo service png-nginx reload #这样就可以用service来管理nginx服务了,比如重载配置
###最後,nginx的主設定檔位於/png/nginx/1.5.7/conf/nginx.conf,自己按需配置.###

以上是Ubuntu上安裝Nginx伺服器程式及簡單環境配置的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除