linux安裝nginx


可部署集群   靜態頁面

下載Nginx地址

https://nginx.org/en/download.html

http://nginx.org/download/nginx-1.15.2.tar.gz  

pcre:https://sourceforge.net/projects/pcre/files/pcre/

 

 

安裝gcc

要用 apt-get install 來裝,說明是 ubuntu/debian之類的linux發行版

apt-get install build-essential

gcc –v  打印出gcc的版本信息

使用:gcc -o test test.c   就會編譯test.c,生成可執行文件 test

然后   ./test  就會運行test

把你的源文件先保存在linux機器上,比方說放到了 /home/yourname/aaa

那你要先 cd /home/yourname/aaa
然后再 gcc -o test test.c

ububtu平台編譯環境可以使用以下指令

apt-get install build-essential
apt-get install libtool

centos平台編譯環境使用如下指令

安裝make:

yum -y install gcc automake autoconf libtool make

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

 
測試1:
Installed:
  libtool.x86_64 0:2.4.2-22.el7_3
測試2:
Total download size: 564 k
Installed size: 1.9 M
Downloading Packages:
libtool-2.2.6-15.5.el6.x86_64.rpm   | 564 kB     00:00     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : libtool-2.2.6-15.5.el6.x86_64   1/1 
  Verifying  : libtool-2.2.6-15.5.el6.x86_64   1/1 
Installed:
  libtool.x86_64 0:2.2.6-15.5.el6
Complete!

安裝g++:

yum install gcc gcc-c++

[root@192-168-6-42 tools]# yum install gcc gcc-c++

Loaded plugins: fastestmirror

Loading mirror speeds from cached hostfile

 * base: mirrors.aliyun.com

 * extras: mirrors.aliyun.com

 * updates: mirrors.aliyun.com

Package gcc-4.8.5-28.el7_5.1.x86_64 already installed and latest version

Package gcc-c++-4.8.5-28.el7_5.1.x86_64 already installed and latest version

Nothing to do

(測試1,測試2)

一、選定安裝文件目錄

可以選擇任何目錄,本文選擇  cd /usr/local/src      cd /data/tools

cd /data/tools

二、安裝PCRE

ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 下載最新的 PCRE 源碼包,使用下面命令下載編譯和安裝 PCRE 包:(本文參照下載文件版本:pcre-8.39.tar.gz 經過驗證未發現這個版本,若想下載最新版本請打開上面網址。本文選擇pcre-8.42.tar.gz)

cd /data/tools
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.42.tar.gz 
tar -zxvf pcre-8.42.tar.gz
換目錄:mv /data/tools/pcre-8.42 /data/env
cd pcre-8.42
./configure
make
make install

make結果:
make[1]: Leaving directory `/data/env/pcre-8.42'
[root@iz2zedcpa52dllcphh40o7z pcre-8.42]# pcre-config --version
8.42

三、安裝zlib

  http://zlib.net/zlib-1.2.11.tar.gz 下載最新的 zlib 源碼包,使用下面命令下載編譯和安裝 zlib包:(本文參照下載文件版本:zlib-1.2.8.tar.gz 經過驗證未發現這個版本,若想下載最新版本請打開上面網址。本文選擇zlib-1.2.11.tar.gz )

cd /data/tools
wget http://zlib.net/zlib-1.2.11.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
換目錄:mv /data/tools/zlib-1.2.11 /data/env/
cd zlib-1.2.11
./configure
make
make install
rm -f /usr/local/lib/libz.a
cp libz.a /usr/local/lib
chmod 644 /usr/local/lib/libz.a
cp libz.so.1.2.11 /usr/local/lib
chmod 755 /usr/local/lib/libz.so.1.2.11
rm -f /usr/local/share/man/man3/zlib.3
cp zlib.3 /usr/local/share/man/man3
chmod 644 /usr/local/share/man/man3/zlib.3
rm -f /usr/local/lib/pkgconfig/zlib.pc
cp zlib.pc /usr/local/lib/pkgconfig
chmod 644 /usr/local/lib/pkgconfig/zlib.pc
rm -f /usr/local/include/zlib.h /usr/local/include/zconf.h
cp zlib.h zconf.h /usr/local/include
chmod 644 /usr/local/include/zlib.h /usr/local/include/zconf.h

四、安裝openssl(某些vps默認沒裝ssl)

cd /data/tools
wget https://www.openssl.org/source/openssl-1.0.1t.tar.gz
tar -zxvf openssl-1.0.1t.tar.gz
( openssl-1.1.1-pre9.tar.gz )

五、安裝nginx

Nginx 一般有兩個版本,分別是穩定版和開發版,您可以根據您的目的來選擇這兩個版本的其中一個,下面是把 Nginx 安裝到 /usr/local/nginx 目錄下的詳細步驟:

重點:nginx 必須安裝在/usr/local/nginx 下 ,就算不安裝在這,默認也會這。

cd /usr/local/nginx
wget http://nginx.org/download/nginx-1.15.2.tar.gz
tar -zxvf nginx-1.15.2.tar.gz
cd nginx-1.15.2
./configure
make  make[1]: Leaving directory `/data/env/nginx-1.15.2'
make install  make[1]: Leaving directory `/usr/local/nginx/nginx-1.15.2'
                 make[1]: Leaving directory `/data/env/nginx'
 

注:這里可能會出現報錯

ubuntu下

apt-get install openssl
apt-get install libssl-dev

centos下

yum -y install openssl openssl-devel
 

下面是把 Nginx 安裝到 /data/evn/nginx 目錄下的詳細步驟:

cd /data/tools/

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

tar -zxvf nginx-1.4.2.tar.gz

cd nginx-1.4.2

 

./configure --sbin-path=/data/env/nginx/nginx \

--conf-path=/data/env/nginx/nginx.conf \

--pid-path=/data/env/nginx/nginx.pid \

--with-http_ssl_module \

--with-pcre=/data/tools/pcre-8.21 \

--with-zlib=/data/tools/zlib-1.2.8 \

--with-openssl=/data/tools/openssl-1.0.1c

 

./configure --sbin-path=/home/nginx/nginx \

--conf-path=/home/nginx/nginx.conf \

--pid-path=/home/nginx/nginx.pid \

--with-http_ssl_module \

--with-pcre=/home/tools/pcre-8.42 \

--with-zlib=/home/tools/zlib-1.2.11 \

--with-openssl=/home/tools/openssl-1.0.1t

 

make

make install

--with-pcre=/data/tools/pcre-8.21 指的是pcre-8.21 的源碼路徑。
--with-zlib=/data/tools/zlib-1.2.7 指的是zlib-1.2.7 的源碼路徑。

 

安裝成功后 /data/env/nginx 目錄下如下

fastcgi.conf            koi-win             nginx.conf.default

fastcgi.conf.default    logs                scgi_params

fastcgi_params          mime.types          scgi_params.default

fastcgi_params.default  mime.types.default  uwsgi_params

html                    nginx               uwsgi_params.default

koi-utf                 nginx.conf          win-utf

 

 
 

六、啟動nginx

  因為可能apeache占用80端口,apeache端口盡量不要修改,我們選擇修改nginx端口。

  linux 修改路徑/usr/local/nginx/conf/nginx.conf,Windows 下 安裝目錄\conf\nginx.conf。

  修改端口為8090,localhost修改為你服務器ip地址。(成功就在眼前!!)172.28.20.156

啟動nginx

netstat -ano|grep 80
 

啟動nginx服務

[root@localhost ~]# nginx安裝目錄 -c nginx.conf配置文件目錄

安裝目錄:  /usr/local/nginx/sbin/nginx

配置文件目錄: /data/env/nginx/conf/nginx.conf

參數 “-c” 指定了配置文件的路徑,如果不加 “-c” 參數,Nginx 會默認加載其安裝目錄的 conf 子目錄中的 nginx.conf 文件

測試1:

/usr/local/nginx/sbin/nginx -c /data/env/nginx/conf/nginx.conf

測試2: 啟動  ./nginx 

[root@192-168-6-42 ~]# ps -ef | grep nginx
root      16400      1  0 02:32 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /data/env/nginx/conf/nginx.conf
nobody    16401  16400  0 02:32 ?        00:00:00 nginx: worker process
root      16403  13660  0 02:33 pts/1    00:00:00 grep --color=auto nginx
 
[root@192-168-6-42 nginx]# netstat -ano|grep 80
unix  3      [ ]         STREAM     CONNECTED     23680    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     28070    /run/systemd/journal/stdout
unix  3      [ ]         DGRAM                    26804    
unix  3      [ ]         DGRAM                    26803    
unix  3      [ ]         STREAM     CONNECTED     19801    /run/systemd/journal/stdout
[root@192-168-6-42 nginx]#

  如果查不到執行結果,則忽略上一步(ubuntu下必須用sudo啟動,不然只能在前台運行)

sudo /usr/local/nginx/nginx

打開瀏覽器訪問此機器的 IP,如果瀏覽器出現 Welcome to nginx! 則表示 Nginx 已經安裝並運行成功。

-----------------------------------------------------
到這里nginx就安裝完成了,如果只是處理靜態html就不用繼續安裝了!!!

 

七、nginx重啟、關閉、啟動

啟動

 啟動代碼格式:nginx安裝目錄地址 -c nginx配置文件地址

例如:

[root@LinuxServer sbin]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
/usr/local/nginx/sbin/nginx -c /data/env/nginx/conf/nginx.conf

停止

 nginx的停止有三種方式:

·         從容停止

  1、查看進程號

[root@LinuxServer ~]# ps -ef|grep nginx

 2、殺死進程

[root@LinuxServer ~]# kill -QUIT 2072

·         快速停止

1、查看進程號

[root@LinuxServer ~]# ps -ef|grep nginx

2、殺死進程

[root@LinuxServer ~]# kill -TERM 2132
或 [root@LinuxServer ~]# kill -INT 2132

·         強制停止

[root@LinuxServer ~]# pkill -9 nginx

重啟

1、驗證nginx配置文件是否正確

方法一:進入nginx安裝目錄sbin下,輸入命令./nginx -t

看到如下顯示nginx.conf syntax is ok

nginx.conf test is successful

說明配置文件正確!

方法二:在啟動命令-c前加-t

 2、重啟Nginx服務

 方法一:進入nginx可執行目錄sbin下,輸入命令 ./nginx -s reload  即可

方法二:查找當前nginx進程號,然后輸入命令:kill -HUP 進程號 實現重啟nginx服務

八、最終結果

九、項目文件存放路徑

  放到這個文件下,創建文件夾放入就好。

錯誤分析:

測試2 端口占用問題:

[root@localhost conf]# netstat -nat |grep 80

tcp        0      0 :::80                       :::*                        LISTEN     

[root@localhost conf]# netstat -nat |grep 5030

tcp        0      0 :::5030                     :::*                        LISTEN     

[root@localhost conf]# netstat -nat |grep 5040

[root@localhost conf]# netstat -nat |grep 5020

[root@localhost conf]# netstat -nat |grep 22

tcp        0      0 192.168.122.1:53            0.0.0.0:*                   LISTEN     

tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN     

tcp        0      0 10.49.1.28:22               10.60.253.253:38159         ESTABLISHED

tcp        0     52 10.49.1.28:22               10.60.253.253:40030         ESTABLISHED

tcp        0      0 :::22

 

yum install -y pcre pcre-devel

yum install -y zlib zlib-devel

yum install -y openssl openssl-devel

 

[root@192-168-6-42 nginx]# cd html/

[root@192-168-6-42 html]# ls

50x.html  index.html

[root@192-168-6-42 html]# cat index.html

<!DOCTYPE html>

<html>

<head>

<title>Welcome to nginx!</title>

<style>

    body {

        width: 35em;

        margin: 0 auto;

        font-family: Tahoma, Verdana, Arial, sans-serif;

    }

</style>

</head>

<body>

<h1>Welcome to nginx!</h1>

<p>If you see this page, the nginx web server is successfully installed and

working. Further configuration is required.</p>

 

<p>For online documentation and support please refer to

<a href="http://nginx.org/">nginx.org</a>.<br/>

Commercial support is available at

<a href="http://nginx.com/">nginx.com</a>.</p>

 

<p><em>Thank you for using nginx.</em></p>

</body>

</html>

[root@192-168-6-42 html]#

 

[root@192-168-6-42 html]# ls

50x.html  index.html

[root@192-168-6-42 html]# cat 50x.html

<!DOCTYPE html>

<html>

<head>

<title>Error</title>

<style>

    body {

        width: 35em;

        margin: 0 auto;

        font-family: Tahoma, Verdana, Arial, sans-serif;

    }

</style>

</head>

<body>

<h1>An error occurred.</h1>

<p>Sorry, the page you are looking for is currently unavailable.<br/>

Please try again later.</p>

<p>If you are the system administrator of this resource then you should check

the <a href="http://nginx.org/r/error_log">error log</a> for details.</p>

<p><em>Faithfully yours, nginx.</em></p>

</body>

</html>

[root@192-168-6-42 html]#

Nginx啟動錯誤:error while loading shared libraries: libpcre.so.1

1

# /usr/local/nginx/sbin/nginx

2

/usr/local/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory

     

從錯誤提示信息可以得知是因為缺少lib文件導致,進一步查看具體內容:

1

# ldd $(which /usr/local/nginx/sbin/nginx)

2

    libpthread.so.0 => /lib64/libpthread.so.0 (0x00000030e8400000)

     

 

3

    libcrypt.so.1 => /lib64/libcrypt.so.1 (0x00000030f9a00000)

4

    libpcre.so.1 => not found

     

 

5

    libcrypto.so.6 => /lib64/libcrypto.so.6 (0x00000030f2a00000)

6

    libz.so.1 => /lib64/libz.so.1 (0x00000030e8800000)

     

 

7

    libc.so.6 => /lib64/libc.so.6 (0x00000030e7800000)

8

    /lib64/ld-linux-x86-64.so.2 (0x00000030e7400000)

     

 

9

    libdl.so.2 => /lib64/libdl.so.2 (0x00000030e8000000)

 
        

查看結果顯示 : libpcre.so.1 => not found ,同時注意lib庫的路徑,有/lib/*/lib64/* 之分。

比如上面的是 /lib64/*,這個和下面解決問題時創建的軟連接有關系

解決辦法:

1、首先確認已經安裝好pcre 軟件(nginx 依賴該軟件)

2、創建軟連接

對於/lib/* 32位系統來說:

1

#查看lib庫

2

#  ls /lib/ |grep pcre

     

 

3

libpcre.so.0

4

libpcre.so.0.0.1

     

 

5

#添加軟連接

6

# ln -s /lib/libpcre.so.0.0.1 /lib/libpcre.so.1

ps: 也有可能 pcre lib文件在目錄:/usr/local/lib/

對於/lib64/* 64位系統來說:

1

#查看lib庫

2

#  ls /lib64/ |grep pcre

     

 

3

libpcre.so.0

4

libpcre.so.0.0.1

     

 

5

#添加軟連接

6

# ln -s /lib64/libpcre.so.0.0.1 /lib64/libpcre.so.1





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM