Ubuntu Nginx安装启动

原文地址:http://blog.csdn.net/a19881029/article/details/51824790

Linux Distribution:Ubuntu 14


一,Nginx的安装

首先从Nginx的官网下载最新的稳定版本1.10.1,下载地址如下

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


[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. sean@sean:~$ tar -xzf nginx-1.10.1.tar.gz  
  2. sean@sean:~$ cd nginx-1.10.1/  
  3. sean@sean:~/nginx-1.10.1$ sudo ./configure  
  4.     ......  
  5. ./configure: error: the HTTP rewrite module requires the PCRE library.  
  6. You can either disable the module by using --without-http_rewrite_module  
  7. option, or install the PCRE library into the system, or build the PCRE library  
  8. statically from the source with nginx by using --with-pcre=<path> option.  

配置时异常,提示HTTP的rewrite模块需要PCRE库,如果需要使用HTTP的rewrite功能,需要首先安装PCRE库,如果不需要该功能,也可使用--without-http_rewrite_module关闭该功能

PCRE库的下载地址如下

https://sourceforge.net/projects/pcre/files/pcre/8.39/pcre-8.39.tar.gz/download
[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. sean@sean:~$ tar -xzf pcre-8.39.tar.gz   
  2. sean@sean:~$ cd pcre-8.39/  
  3. sean@sean:~/pcre-8.39$ sudo ./configure  
  4. sean@sean:~/pcre-8.39$ sudo make  
  5.     ......  
  6. configure: error: You need a C++ compiler for C++ support.  

提示没有C++编译器

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. sean@sean:~/pcre-8.39$ sudo apt-get install gcc-c++  
  2. Reading package lists... Done  
  3. Building dependency tree         
  4. Reading state information... Done  
  5. E: Unable to locate package gcc-c+  
  6. E: Couldn't find any package by regex 'gcc-c+'  
  7. sean@sean:~/pcre-8.39$ sudo apt-get install g++  

Ubuntu中C++的编译器并不叫gcc-c++,而是叫g++
继续之前的操作,完成PCRE的安装

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. sean@sean:~/pcre-8.39$ sudo make  
  2. sean@sean:~/pcre-8.39$ sudo make install  

PCRE安装完成之后可以继续Nginx的安装

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. sean@sean:~/pcre-8.39$ cd ../nginx-1.10.1/  
  2. sean@sean:~/nginx-1.10.1$ sudo ./configure  
  3.     ......  
  4. ./configure: error: the HTTP gzip module requires the zlib library.  
  5. You can either disable the module by using --without-http_gzip_module  
  6. option, or install the zlib library into the system, or build the zlib library  
  7. statically from the source with nginx by using --with-zlib=<path> option.  

HTTP的gzip模块需要zlib库的支持,zlib库的下载地址为:

http://www.zlib.net/

zlib的最新版本为1.2.8

[plain]  view plain  copy
  1. sean@sean:~$ tar -xzvf zlib-1.2.8.tar.gz   
  2. sean@sean:~$ cd zlib-1.2.8/  
  3. sean@sean:~/zlib-1.2.8$ sudo ./configure  
  4. sean@sean:~/zlib-1.2.8$ sudo make  
  5. sean@sean:~/zlib-1.2.8$ sudo make install  

安装完zlib库之后再次安装Nginx

[plain]  view plain  copy
  1. sean@sean:~/zlib-1.2.8$ cd ../nginx-1.10.1/  
  2. sean@sean:~/nginx-1.10.1$ sudo ./configure --without-http_gzip_module  
  3. sean@sean:~/nginx-1.10.1$ sudo make  
  4. sean@sean:~/nginx-1.10.1$ sudo make install  

Nginx最终将被安装在/usr/local/nginx目录下

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. sean@sean:/usr/local/nginx$ ll  
  2. total 24  
  3. drwxr-xr-x  6 root root 4096  7月  4 22:14 ./  
  4. drwxr-xr-x 11 root root 4096  7月  4 22:14 ../  
  5. drwxr-xr-x  2 root root 4096  7月  4 22:14 conf/  
  6. drwxr-xr-x  2 root root 4096  7月  4 22:14 html/  
  7. drwxr-xr-x  2 root root 4096  7月  4 22:14 logs/  
  8. drwxr-xr-x  2 root root 4096  7月  4 22:14 sbin/  

Nginx的可执行文件放在sbin目录下

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. sean@sean:/usr/local/nginx/sbin$ ./nginx   
  2. ./nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory  
  3. sean@sean:/usr/local/nginx/sbin$ ldd nginx   
  4.     linux-vdso.so.1 =>  (0x00007ffdb31f6000)  
  5.     libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fa01d5c4000)  
  6.     libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fa01d3a6000)  
  7.     libcrypt.so.1 => /lib/x86_64-linux-gnu/libcrypt.so.1 (0x00007fa01d16c000)  
  8.     libpcre.so.1 => not found  
  9.     libz.so.1 => /usr/local/lib/libz.so.1 (0x00007f8278c55000)  
  10.     libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fa01cda7000)  
  11.     /lib64/ld-linux-x86-64.so.2 (0x00005583ba8e7000)  

虽然Pcre已经安装了,但是Nginx启动时找不到libpcre.so.1,Pcre默认安装在/usr/local/lib目录下,在/lib目录中手动创建一个libpcre.so.1的链接即可

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. sean@sean:/usr/local/nginx/sbin$ sudo ln -s /usr/local/lib/libpcre.so.1 /lib  
  2. sean@sean:/usr/local/nginx/sbin$ ldd nginx   
  3.     linux-vdso.so.1 =>  (0x00007fff2abef000)  
  4.     libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f15a891f000)  
  5.     libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f15a8701000)  
  6.     libcrypt.so.1 => /lib/x86_64-linux-gnu/libcrypt.so.1 (0x00007f15a84c7000)  
  7.     libpcre.so.1 => /lib/libpcre.so.1 (0x00007f15a82a9000)  
  8.     libz.so.1 => /usr/local/lib/libz.so.1 (0x00007f8278c55000)  
  9.     libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f15a7ee4000)  
  10.     /lib64/ld-linux-x86-64.so.2 (0x000055fce0ecb0  

再次尝试启动Nginx

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. sean@sean:/usr/local/nginx/sbin$ ./nginx   
  2. nginx: [alert] could not open error log file: open() "/usr/local/nginx/logs/error.log" failed (13: Permission denied)  
  3. 2016/07/05 00:01:01 [emerg] 59770#0: open() "/usr/local/nginx/logs/access.log" failed (13: Permission denied)  
  4. sean@sean:/usr/local/nginx/sbin$ sudo ./nginx   

查看一下进程状态可以发现Nginx已经成功启动了

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. sean@sean:/usr/local/nginx/sbin$ ps -ef|grep nginx  
  2. root       9700   9062  0 22:31 ?        00:00:00 nginx: master process ./nginx  
  3. nobody     9701   9700  0 22:31 ?        00:00:00 nginx: worker process  
  4. sean       9729   9638  0 22:32 pts/6    00:00:00 grep --color=auto nginx  

Nginx的默认监听端口是80,此时我们访问http://127.0.0.1:80,就会看到Nginx的欢迎页面


二,Nginx的启停

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. sean@sean:/usr/local/nginx/sbin$ sudo ./nginx -h  
  2. nginx version: nginx/1.10.1  
  3. Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]  
  4.   
  5. Options:  
  6.   -?,-h         : this help  
  7.   -v            : show version and exit  
  8.   -V            : show version and configure options then exit  
  9.   -t            : test configuration and exit  
  10.   -T            : test configuration, dump it and exit  
  11.   -q            : suppress non-error messages during configuration testing  
  12.   -s signal     : send signal to a master process: stop, quit, reopen, reload  
  13.   -p prefix     : set prefix path (default: /usr/local/nginx/)  
  14.   -c filename   : set configuration file (default: conf/nginx.conf)  
  15.   -g directives : set global directives out of configuration file  

比较常用的几个命令如下:

1,使用-t来校验配置文件格式

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. sean@sean:/usr/local/nginx/sbin$ sudo ./nginx -t  
  2. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok  
  3. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful  

2,使用-c指定单独的配置文件

3,平滑关闭

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. sean@sean:/usr/local/nginx/sbin$ sudo ./nginx -s quit  

当然也可以使用kill -QUIT <pid>

4,快速关闭

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. sean@sean:/usr/local/nginx/sbin$ sudo ./nginx -s stop  

当然也可以使用kill -TERM <pid>

此外还有平滑的重载配置文件等一系列命令

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值