操作系统:Ubuntu 16.04
一、安装
有两种方法进行安装,分别为Linux包安装和源码安装
1. Linux包安装(本文未使用)
- Install the prerequisites:
sudo apt install curl gnupg2 ca-certificates lsb-release
- 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
- 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
- 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 -
- Verify that you now have the proper key:
sudo apt-key fingerprint ABF5BD827BD9BF62
- 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>
- To install nginx, run the following commands:
sudo apt update sudo apt install nginx
2. 源码安装
-
获取源码
链接:nginx: download
下载任意版本的.tar.gz包,之后解压:tar -zxvf nginx-1.xxxxx.tar.gz
-
编译安装
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.
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的页面