Ubuntu源码安装Nginx

Nginx是常用Web服务器,市场占有率直追老牌Web服务器软件Apache。Nginx除了作为Web服务器以外,还可以作为邮件服务器,反向代理服务器。下面我们介绍源代码安装和简单配置Nginx

#下载源代码压缩包,以Nginx-1.11.11为例
dell@dell:~$ wget http://Nginx.org/download/Nginx-1.11.11.tar.gz
#解压文件
dell@dell:~$ tar -zxvf Nginx-1.11.11.tar.gz

下载PCRE,前面博文中已有下载和安装过程。

#PCRE官方网站是 https://sourceforge.net/projects/pcre/files/pcre/

安装pcre,zlib,前者一般是为了重写rewrite,后者一般是为了gzip压缩。

PCRE目前(2017-3-24)有两个版本,分别是PCRE与PCRE2,这里我们选择下载PCRE。

PCRE最新版本下载地址是https://sourceforge.net/projects/pcre/files/pcre/8.40/。

选择文件下载、解压与安装。

#下载源代码
dell@dell:~$ wget https://nchc.dl.sourceforge.net/project/pcre/pcre/8.40/pcre-8.40.tar.gz
#解压文件
dell@dell:~$ tar -zxvf pcre-8.40.tar.gz
dell@dell:~$ cd pcre-8.40/
#配置安装文件
dell@dell:~/pcre-8.40$ ./configure --prefix=/usr/local/pcre
dell@dell:~/pcre-8.40$ make
dell@dell:~/pcre-8.40$ sudo make install

安装zlib。

zlib官方网站是 http://zlib.net/

#可在官方网站下载,或去https://sourceforge.net/projects/libpng/files/zlib/ 下载
dell@dell:~$ wget http://zlib.net/zlib-1.2.11.tar.gz
dell@dell:~$ tar -zxvf zlib-1.2.11.tar.gz
dell@dell:~$ cd zlib-1.2.11/
dell@dell:~/zlib-1.2.11$ ./configure --prefix=/usr/local/zlib1.2
Checking for gcc...
Checking for shared library support...
Building shared library libz.so.1.2.11 with gcc.
Checking for size_t... Yes.
Checking for off64_t... Yes.
Checking for fseeko... Yes.
Checking for strerror... Yes.
Checking for unistd.h... Yes.
Checking for stdarg.h... Yes.
Checking whether to use vs[n]printf() or s[n]printf()... using vs[n]printf().
Checking for vsnprintf() in stdio.h... Yes.
Checking for return value of vsnprintf()... Yes.
Checking for attribute(visibility) support... Yes.
dell@dell:~/zlib-1.2.11$ make
dell@dell:~/zlib-1.2.11$ sudo make install

安装OpenSSL。 OpenSSL官方网站https://www.openssl.org/ 。下载页面 https://www.openssl.org/source/

#
dell@dell:~$ wget https://www.openssl.org/source/openssl-1.1.0e.tar.gz
dell@dell:~$ tar -zxvf openssl-1.1.0e.tar.gz
dell@dell:~$ cd openssl-1.1.0e/
dell@dell:~/openssl-1.1.0e$ ./config
############################################
示例配置代码
############################################
dell@dell:~/openssl-1.1.0e$ ./config --prefix=/usr/local/openssl1.1
Operating system: x86_64-whatever-linux2
Configuring for linux-x86_64
Configuring OpenSSL version 1.1.0e (0x1010005fL)
    no-asan         [default]  OPENSSL_NO_ASAN
    no-crypto-mdebug [default]  OPENSSL_NO_CRYPTO_MDEBUG
    no-crypto-mdebug-backtrace [default]  OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
    no-ec_nistp_64_gcc_128 [default]  OPENSSL_NO_EC_NISTP_64_GCC_128
    no-egd          [default]  OPENSSL_NO_EGD
    no-fuzz-afl     [default]  OPENSSL_NO_FUZZ_AFL
    no-fuzz-libfuzzer [default]  OPENSSL_NO_FUZZ_LIBFUZZER
    no-heartbeats   [default]  OPENSSL_NO_HEARTBEATS
    no-md2          [default]  OPENSSL_NO_MD2 (skip dir)
    no-msan         [default]  OPENSSL_NO_MSAN
    no-rc5          [default]  OPENSSL_NO_RC5 (skip dir)
    no-sctp         [default]  OPENSSL_NO_SCTP
    no-ssl-trace    [default]  OPENSSL_NO_SSL_TRACE
    no-ssl3         [default]  OPENSSL_NO_SSL3
    no-ssl3-method  [default]  OPENSSL_NO_SSL3_METHOD
    no-ubsan        [default]  OPENSSL_NO_UBSAN
    no-unit-test    [default]  OPENSSL_NO_UNIT_TEST
    no-weak-ssl-ciphers [default]  OPENSSL_NO_WEAK_SSL_CIPHERS
    no-zlib         [default]
    no-zlib-dynamic [default]
Configuring for linux-x86_64
CC            =gcc
CFLAG         =-Wall -O3 -pthread -m64 -DL_ENDIAN  -Wa,--noexecstack
SHARED_CFLAG  =-fPIC -DOPENSSL_USE_NODELETE
DEFINES       =DSO_DLFCN HAVE_DLFCN_H NDEBUG OPENSSL_THREADS OPENSSL_NO_STATIC_ENGINE OPENSSL_PIC OPENSSL_IA32_SSE2 OPENSSL_BN_ASM_MONT OPENSSL_BN_ASM_MONT5 OPENSSL_BN_ASM_GF2m SHA1_ASM SHA256_ASM SHA512_ASM RC4_ASM MD5_ASM AES_ASM VPAES_ASM BSAES_ASM GHASH_ASM ECP_NISTZ256_ASM PADLOCK_ASM POLY1305_ASM
LFLAG         =
PLIB_LFLAG    =
EX_LIBS       =-ldl
APPS_OBJ      =
cpuID_OBJ     =x86_64cpuid.o
UPLINK_OBJ    =
BN_ASM        =asm/x86_64-gcc.o x86_64-mont.o x86_64-mont5.o x86_64-gf2m.o rsaz_exp.o rsaz-x86_64.o rsaz-avx2.o

EC_ASM        =ecp_nistz256.o ecp_nistz256-x86_64.o
DES_ENC       =des_enc.o fcrypt_b.o
AES_ENC       =aes-x86_64.o vpaes-x86_64.o bSAEs-x86_64.o aesni-x86_64.o aesni-sha1-x86_64.o aesni-sha256-x86_64.o aesni-mb-x86_64.o
BF_ENC        =bf_enc.o
CAST_ENC      =c_enc.o
RC4_ENC       =rc4-x86_64.o rc4-md5-x86_64.o
RC5_ENC       =rc5_enc.o
MD5_OBJ_ASM   =md5-x86_64.o
SHA1_OBJ_ASM  =sha1-x86_64.o sha256-x86_64.o sha512-x86_64.o sha1-mb-x86_64.o sha256-mb-x86_64.o
RMD160_OBJ_ASM=
CMLL_ENC      =cmll-x86_64.o cmll_misc.o
MODES_OBJ     =ghash-x86_64.o aesni-gcm-x86_64.o
PADLOCK_OBJ   =e_padlock-x86_64.o
CHACHA_ENC    =chacha-x86_64.o
POLY1305_OBJ  =poly1305-x86_64.o
BLAKE2_OBJ    =
PROCESSOR     =
RANLIB        =ranlib
ARFLAGS       =
PERL          =/usr/bin/perl

SIXTY_FOUR_BIT_LONG mode

Configured for linux-x86_64.
dell@dell:~/openssl-1.1.0e$
############################################
示例配置代码结束
############################################
dell@dell:~/openssl-1.1.0e$ make
dell@dell:~/openssl-1.1.0e$ sudo make install
dell@dell:~/openssl-1.1.0e$ cd
#检查OpenSSL安装位置
dell@dell:~$ which openssl
/usr/local/bin/openssl
dell@dell:~$ whereis openssl
openssl: /usr/bin/openssl /usr/local/bin/openssl /usr/share/man/man1/openssl.1ssl.gz
dell@dell:~$

开始安装Nginx

dell@dell:~$ tar -zxvf Nginx-1.11.11.tar.gz
dell@dell:~$ cd Nginx-1.11.11/
dell@dell:~/Nginx-1.11.11$ ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
dell@dell:~/Nginx-1.11.11$ ./configure --prefix=/usr/local/Nginx1.11 --with-pcre=/usr/local/pcre
--with-zlib=/usr/local/zlib1.2
dell@dell:~/Nginx-1.11.11$ make
make -f objs/Makefile
make[1]: Entering directory '/home/dell/Nginx-1.11.11'
cd /usr/local/pcre \
&& if [ -f Makefile ]; then make distclean; fi \
&& CC="cc" CFLAGS="-O2 -fomit-frame-pointer -pipe " \
./configure --disable-shared
/bin/sh: 3: ./configure: not found
objs/Makefile:1170: recipe for target '/usr/local/pcre/Makefile' Failed
make[1]: *** [/usr/local/pcre/Makefile] Error 127
make[1]: Leaving directory '/home/dell/Nginx-1.11.11'
Makefile:8: recipe for target 'build' Failed
make: *** [build] Error 2
dell@dell:~/Nginx-1.11.11$
#解决该问题一个方法
#参考链接 http://kexiaoke.blog.51cto.com/5530023/1375299
#指定PCRE和zlib源代码位置,复制文件时请执行make distclean
dell@dell:~$ sudo mkdir /usr/local/pcre/src
dell@dell:~$ sudo mkdir /usr/local/zlib1.2/src
dell@dell:~$ sudo cp -r zlib-1.2.11/* /usr/local/zlib1.2/src/
dell@dell:~$ sudo cp -r pcre-8.40/* /usr/local/pcre/src/
dell@dell:~/Nginx-1.11.11$ make clean

请检查PCRE是否正在运行。有相关进程先结束掉。推测可能是PHP或者Apache导致PCRE某些文件不能写入导致Nginx编译不成功。重启电脑可能会解决该问题。

dell@dell:~/Nginx-1.11.11$ whereis pcre
pcre: /usr/local/pcre
dell@dell:~/Nginx-1.11.11$ which pcre
dell@dell:~/Nginx-1.11.11$ ./configure --prefix=/usr/local/Nginx1.11 --with-pcre=/home/dell/pcre-8.40 --wit h-zlib=/home/dell/zlib-1.2.11
#开始安装
dell@dell:~/Nginx-1.11.11$ sudo make install
[sudo] dell 的密码:
make -f objs/Makefile install
make[1]: Entering directory '/home/dell/Nginx-1.11.11'
test -d '/usr/local/Nginx1.11' || mkdir -p '/usr/local/Nginx1.11'
test -d '/usr/local/Nginx1.11/sbin' \
        || mkdir -p '/usr/local/Nginx1.11/sbin'
test ! -f '/usr/local/Nginx1.11/sbin/Nginx' \
        || mv '/usr/local/Nginx1.11/sbin/Nginx' \
                '/usr/local/Nginx1.11/sbin/Nginx.old'
cp objs/Nginx '/usr/local/Nginx1.11/sbin/Nginx'
test -d '/usr/local/Nginx1.11/conf' \
        || mkdir -p '/usr/local/Nginx1.11/conf'
cp conf/koi-win '/usr/local/Nginx1.11/conf'
cp conf/koi-utf '/usr/local/Nginx1.11/conf'
cp conf/win-utf '/usr/local/Nginx1.11/conf'
test -f '/usr/local/Nginx1.11/conf/mime.types' \
        || cp conf/mime.types '/usr/local/Nginx1.11/conf'
cp conf/mime.types '/usr/local/Nginx1.11/conf/mime.types.default'
test -f '/usr/local/Nginx1.11/conf/fastcgi_params' \
        || cp conf/fastcgi_params '/usr/local/Nginx1.11/conf'
cp conf/fastcgi_params \
        '/usr/local/Nginx1.11/conf/fastcgi_params.default'
test -f '/usr/local/Nginx1.11/conf/fastcgi.conf' \
        || cp conf/fastcgi.conf '/usr/local/Nginx1.11/conf'
cp conf/fastcgi.conf '/usr/local/Nginx1.11/conf/fastcgi.conf.default'
test -f '/usr/local/Nginx1.11/conf/uwsgi_params' \
        || cp conf/uwsgi_params '/usr/local/Nginx1.11/conf'
cp conf/uwsgi_params \
        '/usr/local/Nginx1.11/conf/uwsgi_params.default'
test -f '/usr/local/Nginx1.11/conf/scgi_params' \
        || cp conf/scgi_params '/usr/local/Nginx1.11/conf'
cp conf/scgi_params \
        '/usr/local/Nginx1.11/conf/scgi_params.default'
test -f '/usr/local/Nginx1.11/conf/Nginx.conf' \
        || cp conf/Nginx.conf '/usr/local/Nginx1.11/conf/Nginx.conf'
cp conf/Nginx.conf '/usr/local/Nginx1.11/conf/Nginx.conf.default'
test -d '/usr/local/Nginx1.11/logs' \
        || mkdir -p '/usr/local/Nginx1.11/logs'
test -d '/usr/local/Nginx1.11/logs' \
        || mkdir -p '/usr/local/Nginx1.11/logs'
test -d '/usr/local/Nginx1.11/html' \
        || cp -R html '/usr/local/Nginx1.11'
test -d '/usr/local/Nginx1.11/logs' \
        || mkdir -p '/usr/local/Nginx1.11/logs'
make[1]: Leaving directory '/home/dell/Nginx-1.11.11'
dell@dell:~/Nginx-1.11.11$
dell@dell:~$ whereis Nginx
Nginx: /usr/local/Nginx1.11
#运行Nginx
dell@dell:/usr/local/Nginx1.11/sbin$ sudo /usr/local/Nginx1.11/sbin/Nginx
#检查Nginx是否占用80端口
dell@dell:/usr/local/Nginx1.11/sbin$ sudo netstat -lpn|grep :80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      8166/Nginx
dell@dell:/usr/local/Nginx1.11/sbin$ sudo netstat -lpn|grep :8080
dell@dell:/usr/local/Nginx1.11/sbin$ sudo /usr/local/apache2.4/bin/apachectl start
dell@dell:/usr/local/Nginx1.11/sbin$ sudo netstat -lpn|grep :8080
tcp6       0      0 :::8080                 :::*                    LISTEN      8203/httpd
dell@dell:/usr/local/Nginx1.11/sbin$

安装成功截图如下。

编辑Nginx配置文件

dell@dell:~$ sudo vim /usr/local/Nginx1.11/conf/Nginx.conf

文件可以修改服务器http监听端口、服务器名称等多项设置。

#重启服务
dell@dell:~$ sudo /usr/local/Nginx1.11/sbin/Nginx -s reload
#停止服务
dell@dell:~$ sudo /usr/local/Nginx1.11/sbin/Nginx -s stop
#启动服务
dell@dell:~$ sudo /usr/local/Nginx1.11/sbin/Nginx
#测试服务,请检查-t和-T区别
dell@dell:~$ sudo /usr/local/Nginx1.11/sbin/Nginx -t
Nginx: the configuration file /usr/local/Nginx1.11/conf/Nginx.conf Syntax is ok
Nginx: configuration file /usr/local/Nginx1.11/conf/Nginx.conf test is successful
dell@dell:~$ sudo /usr/local/Nginx1.11/sbin/Nginx -T
Nginx: the configuration file /usr/local/Nginx1.11/conf/Nginx.conf Syntax is ok
Nginx: configuration file /usr/local/Nginx1.11/conf/Nginx.conf test is successful
# configuration file /usr/local/Nginx1.11/conf/Nginx.conf:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/Nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.PHP$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.PHP$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.PHP;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files,if Apache's document root
        # concurs with Nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-,name-,and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

# configuration file /usr/local/Nginx1.11/conf/mime.types:

types {
    text/html                             html htm shtml;
    text/css                              css;
    text/xml                              xml;
    image/gif                             gif;
    image/jpeg                            jpeg jpg;
    application/javascript                js;
    application/atom+xml                  atom;
    application/RSS+xml                   RSS;

    text/mathml                           mml;
    text/plain                            txt;
    text/vnd.sun.j2me.app-descriptor      jad;
    text/vnd.wap.wml                      wml;
    text/x-component                      htc;

    image/png                             png;
    image/tiff                            tif tiff;
    image/vnd.wap.wbmp                    wbmp;
    image/x-icon                          ico;
    image/x-jng                           jng;
    image/x-ms-bmp                        bmp;
    image/svg+xml                         svg svgz;
    image/webp                            webp;

    application/font-woff                 woff;
    application/java-archive              jar war ear;
    application/json                      json;
    application/mac-binhex40              hqx;
    application/msword                    doc;
    application/pdf                       pdf;
    application/postscript                ps eps ai;
    application/rtf                       rtf;
    application/vnd.apple.mpegurl         m3u8;
    application/vnd.ms-excel              xls;
    application/vnd.ms-fontobject         eot;
    application/vnd.ms-powerpoint         ppt;
    application/vnd.wap.wmlc              wmlc;
    application/vnd.google-earth.kml+xml  kml;
    application/vnd.google-earth.kmz      kmz;
    application/x-7z-compressed           7z;
    application/x-cocoa                   cco;
    application/x-java-archive-diff       jardiff;
    application/x-java-jnlp-file          jnlp;
    application/x-makeself                run;
    application/x-perl                    pl pm;
    application/x-pilot                   prc pdb;
    application/x-rar-compressed          rar;
    application/x-redhat-package-manager  rpm;
    application/x-sea                     sea;
    application/x-shockwave-flash         swf;
    application/x-stuffit                 sit;
    application/x-tcl                     tcl tk;
    application/x-x509-ca-cert            der pem crt;
    application/x-xpinstall               xpi;
    application/xhtml+xml                 xhtml;
    application/xspf+xml                  xspf;
    application/zip                       zip;

    application/octet-stream              bin exe dll;
    application/octet-stream              deb;
    application/octet-stream              dmg;
    application/octet-stream              iso img;
    application/octet-stream              msi msp msm;

    application/vnd.openxmlformats-officedocument.wordprocessingml.document    docx;
    application/vnd.openxmlformats-officedocument.spreadsheetml.sheet          xlsx;
    application/vnd.openxmlformats-officedocument.presentationml.presentation  pptx;

    audio/midi                            mid midi kar;
    audio/mpeg                            mp3;
    audio/ogg                             ogg;
    audio/x-m4a                           m4a;
    audio/x-realaudio                     ra;

    video/3gpp                            3gpp 3gp;
    video/mp2t                            ts;
    video/mp4                             mp4;
    video/mpeg                            mpeg mpg;
    video/quicktime                       mov;
    video/webm                            webm;
    video/x-flv                           flv;
    video/x-m4v                           m4v;
    video/x-mng                           mng;
    video/x-ms-asf                        asx asf;
    video/x-ms-wmv                        wmv;
    video/x-msvideo                       avi;
}

dell@dell:~$

相关文章

1.安装过程出现0x00000000指令引用的0x00000000内存该内存不能为written 如果你安装的是inux系统 需要在...
写在全面:如果根据以下教程涉及到只读文件需要更改文件权限才能需修改文件内容,参考我的另一篇博客:...
写在前面:以下步骤中需要在终端输入命令,电脑端查看博客的朋友可以直接复制粘贴到终端,手机端查看的...
ubuntu16.04和18.04更换国内源 写在前面:安装好ubuntu双系统后,默认的软件更新源是国外的,在国内使用...
ubuntu双系统启动时卡死解决办法(在ubuntu16.04和18.04测试无误) 问题描述: 在安装完ubuntu双系统后...
又来造轮子了。。。。。。。。。。。。。。。。 今天使用w3af向文件中写入的时候,发现没有write权限,...