nginx是由俄羅斯工程師Igor Sysoev 用C語言開發的一個免費開源的Web服務器軟件,於2004年發佈,彙集輕量級、高併發、高性能、低消耗等一系列優勢。目前Nginx是互聯網上僅次於Apache的第二流行的Web服務器軟件。html
接下來咱們開始安裝nginx,咱們下面是以centos7爲基礎環境進行搭建,咱們的安裝方法:1、yum安裝 2、源碼安裝。nginx
1 yum install nginx
若是你沒有yum 源的話你須要配置下yum源:c++
1 vim /etc/yum.repos.d/nginx.repo
nginx.repo裏填入如下內容:vim
1 [nginx] 2 name=nginx repo 3 baseurl=http://nginx.org/packages/centos/7/x86_64/ 4 gpgcheck=0 5 enabled=1
而後咱們清除yum的緩存,進行安裝centos
yum clean all yum makecahe yum install nginx
這樣咱們就成功安裝好了nginx接下來說講源碼安裝,源碼安裝是能夠個性化定製的。緩存
首先咱們須要去下載源碼包:bash
源碼包下載地址:http://nginx.org/en/download.html,咱們選擇穩定版的包進行下載服務器
而後咱們進行解壓和安裝:併發
wget http://nginx.org/download/nginx-1.14.0.tar.gz tar -xzvf nginx-1.14.0.tar.gz cd nginx-1.14.0
建立nginx用戶和用戶組tcp
useradd -M -s /sbin/nologin nginx
在進行安裝以前咱們須要的一些通用配置選項:(官網源碼編譯配置文檔路徑:http://nginx.org/en/docs/configure.html )
--prefix=<path> nginx 的安裝的根路徑,全部其餘的安裝路徑都要依賴於該選項 --sbin-path=<path> 指定nginx二進制文件的路徑。若是沒有指定,那麼這個路徑會依賴於--prefix選項 --conf-path=<path> 若是在命令行沒有指定配置文件,那麼將會經過這裏指定的路徑,nginx將會去那裏查找它的配置文件,這裏的路徑要精確到文件. --error-log-path=<path> 指定錯誤日誌文件的路徑,要精確到文件。 --pid-path=<path>指定pid文件的路徑,一般在/var/run/下 --lock-path=<path>互斥鎖文件的路徑 --user=<user> worker進程運行的用戶 --group=<group> worker進程運行的組
--with-http_ssl_module 啓用ssl模塊
以上是自定義安裝的一些配置選項:
源碼的安裝通常由3個步驟組成:配置(configure)、編譯(make)、安裝(make install)。接下來咱們開始進行編譯安裝,我會把安裝過程當中的遇到的問題貼出來。
mkdir /opt/nginx &&chown nginx:nginx /opt/nginx/
./configure --prefix=/opt/nginx/ --sbin-path=/usr/bin/ --conf-path=/etc/nginx/nginx.conf --user=nginx --group=nginx
#--prefix=/opt/nginx/ 指定nginx的安裝目錄是/opt/nginx
#--sbin-path=/usr/bin/ 指定nginx二進制文件的路徑是/usr/bin
#--conf-path=/etc/nginx/nginx.conf 指定配置文件的路徑
#--user=nginx 指定進程運行的用戶
#--group=nginx 指定進程運行的用戶
在編譯的過程當中我遇到了如下的錯誤,這裏作個記錄:
第一個錯誤 :./configure: error: C compiler cc is not found缺乏gcc編譯器。
解決方法:安裝gcc編譯器
yum -y install gcc-c++ autoconf automake
第二個錯誤:/configure: error: the HTTP rewrite module requires the PCRE library.確少PCRE庫.
解決方法:安裝PCRE
yum -y install pcre pcre-devel
第三個錯誤:./configure: error: the HTTP cache module requires md5 functions from OpenSSL library. You can either disable the module by using --without-http-cache option, or install the OpenSSL library into the system, or build the OpenSSL library statically from the source with nginx by using --with-http_ssl_module --with-openssl=<path> options. 缺乏ssl錯誤。
解決方法:安裝openssl
yum -y install openssl openssl-devel
第四個錯誤:./configure: error: the HTTP gzip module requires the zlib library. 缺乏zlib庫
解決辦法:安裝zlib庫
yum install -y zlib-devel
還有些錯誤是我沒有遇到的,可是我在這邊作下記錄,之後可能會有幫助。
錯誤信息:./configure: error: the HTTP XSLT module requires the libxml2/libxslt 缺乏libxml2
解決辦法:yum -y install libxml2 libxml2-dev && yum -y install libxslt-devel
錯誤信息:./configure: error: the HTTP image filter module requires the GD library. You can either do not enable the module or install the libraries.
解決方法:http_image_filter_module是nginx提供的集成圖片處理模塊,須要gd-devel的支持 yum -y install gd-devel
錯誤信息:./configure: error: perl module ExtUtils::Embed is required 缺乏ExtUtils
解決方法:yum -y install perl-devel perl-ExtUtils-Embed
錯誤信息:./configure: error: the GeoIP module requires the GeoIP library. You can either do not enable the module or install the library. 缺乏GeoIP
解決方法:yum -y install GeoIP GeoIP-devel GeoIP-data
以上是配置過程當中可能遇到的錯誤,接下來咱們進行編譯。
make -j 4
-j 參數的意義是並行編譯,在多核的狀況下能夠提高編譯速度
安裝:
make install
而後咱們須要更改一下目錄的全部者和所屬組,避免nginx運行時的權限問題
chown -R nginx:nginx /opt/nginx/ chown -R nginx:nginx /etc/nginx/
查看nginx的版本:
[root@test1 etc]# /usr/bin/nginx -v nginx version: nginx/1.14.0
運行nginx
[root@test1 etc]# /usr/bin/nginx [root@test1 etc]# ps -ef |grep nginx root 8984 1 0 10:56 ? 00:00:00 nginx: master process /usr/bin/nginx nginx 8985 8984 0 10:56 ? 00:00:00 nginx: worker process root 8987 3060 0 10:56 pts/2 00:00:00 grep --color=auto nginx
而後咱們就能夠直接訪問咱們的ip的80端口(端口須要開放的)
若是開了防火牆能夠關閉也能夠開放80端口。
關閉防火牆:
systemctl stop firewalld
開放80端口:
firewall-cmd --add-port=80/tcp --permanent firewall-cmd --reload
當咱們看到這個界面就意味安裝成功了:
建立 service 文件,若是你的安裝文件路徑和下面的配置文件的路徑不一致的時候,須要進行對應的替換。
cat <<EOF >> /usr/lib/systemd/system/nginx1.service [Unit] Description=The nginx HTTP and reverse proxy server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/run/nginx.pid ExecStartPre=/usr/bin/rm -f /run/nginx.pid ExecStartPre=/usr/sbin/nginx -t ExecStart=/usr/sbin/nginx ExecReload=/bin/kill -s HUP $MAINPID KillSignal=SIGQUIT TimeoutStopSec=5 KillMode=process PrivateTmp=true [Install] WantedBy=multi-user.target EOF
經過 systemd 來管理 nginx
# 查看狀態
systemctl status nginx
# 啓動nginx
systemctl start nginx
# 暫停nginx
systemctl stop nginx
# 重啓nginx
systemctl restart nginx
# 設置開機自啓
systemctl enable nginx
# 關閉開機自啓
systemctl disable nginx
最後咱們再講講幾個nginx的命令:
指定配置文件啓動:/usr/bin/nginx -c conf/nginx.conf 查看配置文件是否正確:/usr/bin/nginx -t 從新加載配置|重啓|中止|退出 nginx : nginx -s reload|reopen|stop|quit 打開幫助信息: -?,-h 查看版本信息: -v 查看版本信息和配置信息: -V
以上就是安裝nginx的一個筆記。