共計(jì) 4385 個(gè)字符,預(yù)計(jì)需要花費(fèi) 11 分鐘才能閱讀完成。
這篇文章主要介紹“Ubuntu 上怎么安裝并配置 Nginx”,在日常操作中,相信很多人在 Ubuntu 上怎么安裝并配置 Nginx 問題上存在疑惑,丸趣 TV 小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Ubuntu 上怎么安裝并配置 Nginx”的疑惑有所幫助!接下來,請(qǐng)跟著丸趣 TV 小編一起來學(xué)習(xí)吧!
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 從常規(guī)源安裝 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 版)
為了方便開發(fā)和管理, 我在根目錄下新建了一個(gè) png 目錄, 并且目錄所有者設(shè)置為當(dāng)前用戶,nginx 就編譯在 /png/nginx/1.5.7 下面:
sudo mkdir /png
sudo chown eechen:eechen /png
運(yùn)行用戶我定義為 png:png, 所以我需要新建這樣一個(gè)用戶:
sudo addgroup png --system
sudo adduser png --system --disabled-login --ingroup png --no-create-home --home /nonexistent --gecos png user --shell /bin/false
(新建用戶的命令可以參考官方 deb 包里的預(yù)安裝腳本 debian/preinst)
編譯參數(shù)參考了 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
注意: 這一步按錯(cuò)誤提示安裝依賴的包, 這時(shí)就是 apt 要發(fā)威的時(shí)候了, 比如我的系統(tǒng)安裝了這些包:
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 時(shí) configure 的需求.
好了,configure 成功后就可以編譯安裝了:
time make make install
time 主要用來查看本次編譯耗時(shí).
編譯好后可以看看這個(gè)家伙的個(gè)頭:
du -sh /png/nginx/1.5.7/sbin/nginx
5.5m /png/nginx/1.5.7/sbin/nginx
環(huán)境簡(jiǎn)單配置總結(jié)
減小 nginx 編譯后的文件大小:
編輯源文件 nginx-1.5.7/auto/cc/gcc 去除 debug 信息(注釋掉即可):
# debug
# cflags= $cflags -g
這樣編譯后的主程序大小則為 700 多 k, 和 nginx 官方提供的 deb 包程序大小相近.
另外 configure 時(shí)去掉一些不需要的模塊, 編譯后的可執(zhí)行文件會(huì)更小.
當(dāng)然, 我需要一個(gè)服務(wù)腳本用來管理 nginx, 這時(shí)同樣可以借助官方 deb 包里提供的服務(wù)腳本 etc/init.d/nginx.
我把它放到 /png/nginx/1.5.7/nginx, 對(duì)開頭定義的幾個(gè)值 (13 到 19 行) 進(jìn)行稍加修改:
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
啟動(dòng)前先創(chuàng)建一個(gè) cache 目錄, 否則會(huì)提示出錯(cuò):
mkdir /png/nginx/1.5.7/var/cache
啟動(dòng) nginx:
sudo /png/nginx/1.5.7/nginx start
測(cè)試頁面:
curl -i `hostname`
看一下端口:
sudo netstat -antp|grep nginx
查看一下它占用的內(nèi)存:
htop 按 f4 過濾 nginx
用 top 同樣能看到類似內(nèi)容:
top -b -n1|head -n7 top -b -n1|grep nginx
主要看 res 這個(gè)值, 常駐內(nèi)存 (resident), 不包括 swap 空間的物理內(nèi)存, 單位為 kb,%mem 就是以 res 為參照對(duì)象.
可以看到 nginx 兩個(gè)進(jìn)程占用的物理內(nèi)存加起來不到 2m, 內(nèi)存占用非常小.
另外 top 里的 res 這個(gè)值對(duì)應(yīng) ps aux 里的 rss 這個(gè)值:
ps aux|head -n1 ps aux|grep nginx
還有我們可以看到 nginx 的工人進(jìn)程只有一個(gè)線程:
cat /proc/25047/status|grep threads
threads: 1
其中 25047 是 nginx 工人進(jìn)程 pid 號(hào).
把 nginx 做成系統(tǒng)服務(wù), 開機(jī)自啟動(dòng):
sudo ln -s /png/nginx/1.5.7/nginx /etc/init.d/png-nginx
sudo update-rc.d png-nginx defaults # 開機(jī)自啟動(dòng)
sudo update-rc.d -f png-nginx remove # 以后不想開機(jī)自啟動(dòng)可以這樣禁止
sudo service png-nginx reload # 這樣就可以用 service 來管理 nginx 服務(wù)了, 比如重載配置
最后,nginx 的主配置文件位于 /png/nginx/1.5.7/conf/nginx.conf, 自己按需配置.
到此,關(guān)于“Ubuntu 上怎么安裝并配置 Nginx”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注丸趣 TV 網(wǎng)站,丸趣 TV 小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!