Ubuntu下Nginx的安装及使用


操作系统:Ubuntu 16.04

一、安装

有两种方法进行安装,分别为Linux包安装源码安装

1. Linux包安装(本文未使用)

参考:nginx: Linux packages

  1. Install the prerequisites:
    sudo apt install curl gnupg2 ca-certificates lsb-release
  2. To set up the apt repository for stable nginx packages, run the following command:
    echo "deb http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \ | sudo tee /etc/apt/sources.list.d/nginx.list
  3. If you would like to use mainline nginx packages, run the following command instead:
    echo "deb http://nginx.org/packages/mainline/ubuntu `lsb_release -cs` nginx" \ | sudo tee /etc/apt/sources.list.d/nginx.list
  4. Next, import an official nginx signing key so apt could verify the packages authenticity:
    curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo apt-key add -
  5. Verify that you now have the proper key:
    sudo apt-key fingerprint ABF5BD827BD9BF62
  6. The output should contain the full fingerprint 573B FD6B 3D8F BC64 1079 A6AB ABF5 BD82 7BD9 BF62 as follows:
    pub rsa2048 2011-08-19 [SC] [expires: 2024-06-14] 573B FD6B 3D8F BC64 1079 A6AB ABF5 BD82 7BD9 BF62 uid [ unknown] nginx signing key <signing-key@nginx.com>
  7. To install nginx, run the following commands:
    sudo apt update sudo apt install nginx

2. 源码安装

  1. 获取源码
    链接:nginx: download
    下载任意版本的.tar.gz包,之后解压:

    tar -zxvf nginx-1.xxxxx.tar.gz
    
  2. 编译安装
    cd进目录,之后:

    ./configure
    make
    make install
    

*可能会遇到的问题1:

./configure: error: the HTTP rewrite module requires the PCRE library.

解决:ubuntu安装nginx时提示error: the HTTP rewrite module requires the PCRE library

sudo apt update
sudo apt install libpcre3 libpcre3-dev

*换了个机子,在这一步遇到了新的问题:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
libpcre3 is already the newest version (2:8.38-3.1).
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
 libpcre3-dev : Depends: libpcre16-3 (= 2:8.38-3.1) but it is not going to be installed
                Depends: libpcre32-3 (= 2:8.38-3.1) but it is not going to be installed
                Depends: libpcrecpp0v5 (= 2:8.38-3.1) but it is not going to be installed
 openssh-server : Depends: openssh-client (= 1:7.2p2-4ubuntu2.6)
                  Depends: openssh-sftp-server but it is not going to be installed
                  Recommends: ssh-import-id but it is not going to be installed
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).

按照提示,执行apt-get -f install,继续报错:

Setting up openssh-server (1:7.2p2-4ubuntu2.8) ...
invoke-rc.d: dangling symlink: /etc/rc5.d/S02ssh
dpkg: error processing package openssh-server (--configure):
 subprocess installed post-installation script returned error exit status 102
E: Sub-process /usr/bin/dpkg returned an error code (1)

解决方法参考:dpkg: error processing package xxx (–configure)的解决方法

mv /var/lib/dpkg/info/ /var/lib/dpkg/info_old/
mkdir /var/lib/dpkg/info/
apt-get update
apt-get -f install
mv /var/lib/dpkg/info/* /var/lib/dpkg/info_old/
rm -rf /var/lib/dpkg/info
mv /var/lib/dpkg/info_old/ /var/lib/dpkg/info

之后就可以了


*可能会遇到的问题2:

./configure: error: the HTTP gzip module requires the zlib library.

解决:Ubuntu安装zlib和zlib-devel

sudo apt update
sudo apt install zlib1g.dev

二、使用(基于源码安装)

默认的安装目录:

/usr/local/nginx

启动:

/usr/local/nginx/sbin/nginx

*建议将路径添加进环境变量(/etc/profile中的$PATH),输命令直接用nginx即可:

NGINX_PATH=/usr/local/nginx/sbin
export PATH=$PATH:$NGINX_PATH

常用选项:

-t:测试配置文件
-c [配置文件目录]:指定配置文件
-s reload:重载配置文件
-s stop:快速停止,相当于kill
-s quit:退出(完成当前工作后停止)

此外,配置文件目录:

/usr/local/nginx/conf/nginx.conf

网页目录:

/usr/local/nginx/html/index.html

验证:
打开浏览器,跳转至:

http://localhost/

即可看到Nignx的页面

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Green Lv

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值