Ubuntu16.04.1 安装Nginx

前端之家收集整理的这篇文章主要介绍了Ubuntu16.04.1 安装Nginx前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。

安装Nginx依赖库

安装gcc g++的依赖库

ubuntu平台可以使用如下命令。

  1. apt-get install build-essential
  2. apt-get install libtool

centeros平台可以使用如下命令。

  1. centos平台编译环境使用如下指令
  2. 安装make
  3. yum -y install gcc automake autoconf libtool make
  4. 安装g++:
  5. yum install gcc gcc-c++ 

安装 pcre依赖库(http://www.pcre.org/

  1. sudo apt-get update
  2. sudo apt-get install libpcre3 libpcre3-dev

安装 zlib依赖库(http://www.zlib.net

  1. apt-get install zlib1g-dev

安装 ssl依赖库

  1. apt-get install openssl


安装Nginxhttp://nginx.org

  1. #下载最新版本:
  2. wget http://Nginx.org/download/Nginx-1.11.3.tar.gz
  3. #解压:
  4. tar -zxvf Nginx-1.11.3.tar.gz
  5. #进入解压目录:
  6. cd Nginx-1.11.3
  7. #配置:
  8. ./configure --prefix=/usr/local/Nginx
  9. #编辑Nginx
  10. make
  11. 注意:这里可能会报错,提示pcre.h No such file or directory”,具体详见:http://stackoverflow.com/questions/22555561/error-building-fatal-error-pcre-h-no-such-file-or-directory
  12. 需要安装 libpcre3-dev,命令为:sudo apt-get install libpcre3-dev
  13. #安装Nginx
  14. sudo make install
  15. #启动Nginx
  16. sudo /usr/local/Nginx/sbin/Nginx -c /usr/local/Nginx/conf/Nginx.conf
  17. 注意:-c 指定配置文件的路径,不加的话,Nginx自动加载默认路径的配置文件,可以通过 -h查看帮助命令。
  18. #查看Nginx进程:
  19. ps -ef|grep Nginx


Nginx常用命令

启动 Nginx

  1. /usr/local/Nginx/sbin/Nginx
  2. ./sbin/Nginx



停止 Nginx

  1. ./sbin/Nginx -s stop
  2. ./sbin/Nginx -s quit

-s都是采用向 Nginx 发送信号的方式。

Nginx重新加载配置

  1. ./sbin/Nginx -s reload

指定配置文件

  1. ./sbin/Nginx -c /usr/local/Nginx/conf/Nginx.conf

-c表示configuration,指定配置文件

查看 Nginx 版本

有两种可以查看 Nginx 的版本信息的参数。第一种如下:

  1. ./sbin/Nginx -v
  2. Nginx: Nginx version: Nginx/1.0.0


另一种显示的是详细的版本信息:

  1. poechant@ubuntu:/usr/local/Nginx$ ./sbin/Nginx -V
  2. Nginx: Nginx version: Nginx/1.0.0
  3. Nginx: built by gcc 4.3.3 (Ubuntu 4.3.3-5ubuntu4)
  4. Nginx: TLS SNI support enabled
  5. Nginx: configure arguments: --with-http_ssl_module --with-openssl=/home/luming/openssl-1.0.0d/


检查配置文件是否正确

  1. poechant@ubuntu:/usr/local/Nginx$ ./sbin/Nginx -t
  2. Nginx: [alert] could not open error log file: open() "/usr/local/Nginx/logs/error.log" Failed (13: Permission denied)
  3. Nginx: the configuration file /usr/local/Nginx/conf/Nginx.conf Syntax is ok
  4. 2012/01/09 16:45:09 [emerg] 23898#0: open() "/usr/local/Nginx/logs/Nginx.pid" Failed (13: Permission denied)
  5. Nginx: configuration file /usr/local/Nginx/conf/Nginx.conf test Failed


如果出现如上的提示信息,表示没有访问错误日志文件和进程,可以sudo(super user do)一下:

  1. poerchant@ubuntu:/usr/local/Nginx$ sudo ./sbin/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


显示帮助信息如果显示如上,则表示配置文件正确。否则,会有相关提示

  1. poechant@ubuntu:/user/local/Nginx$ ./sbin/Nginx -h

或者:

  1. poechant@ubuntu:/user/local/Nginx$ ./sbin/Nginx -h

测试Nginx

进入目录/usr/local/Nginx/sbin,输入测试命令sudo ./Nginx -t,测试Nginx是否正确安装,这时出现下面的错误,这是文件权限问题。

Nginx: [alert] could not open error log file: open() "/usr/local/Nginx/logs/error.log" Failed (13: Permission denied)
2014/06/30 10:31:57 [emerg] 5358#0: mkdir() "/usr/local/Nginx/client_body_temp" Failed (13: Permission denied)

使用命令:

sudo chmod a+rwx -R 目录名

修改文件权限,改完后上面的测试命令就可以执行。

也可以输入sudo ./Nginx命令启动Nginx,使用浏览器访问http://127.0.0.1,会显示同样的Nginx欢迎界面,表示安装成功(源码安装和apt-get版本不一样,故显示结果也不一样)。

我是在vmware虚拟机中的ubuntu安装的,也可以通过win7主机系统的浏览器访问:

apt卸载Nginx方法

另附上删除Nginx方法

卸载方法1.

# 删除Nginx,保留配置文件
sudo apt-get remove Nginx
#删除配置文件
rm -rf /etc/Nginx

卸载方法2.
#删除Nginx连带配置文件
sudo apt-get purge Nginx # Removes everything.

#卸载不再需要的Nginx依赖程序 sudo apt-get autoremove

猜你在找的Ubuntu相关文章