总目录 - Ubuntu 应用软件安装 && 开发环境搭建测试 目录
文章目录
使用包管理工具安装
安装
- 查询要安装的软件包名称
apt-cache search nginx
wuyujin@ubuntu18:~$ apt-cache search nginx | grep ^nginx
nginx - small, powerful, scalable web/proxy server
nginx-common - small, powerful, scalable web/proxy server - common files
nginx-core - nginx web/proxy server (standard version)
nginx-doc - small, powerful, scalable web/proxy server - documentation
nginx-extras - nginx web/proxy server (extended version)
nginx-full - nginx web/proxy server (standard version)
nginx-light - nginx web/proxy server (basic version)
wuyujin@ubuntu18:~$
由结果可知,要安装的软件包名为nginx
(其他也可以)。
- 安装
sudo apt-get install nginx
- 查询安装位置
dpkg -L nginx
若软件包未安装,会显示package 'nginx' is not installed
:
wuyujin@ubuntu18:~$ dpkg -L nginx
dpkg-query: package 'nginx' is not installed
Use dpkg --info (= dpkg-deb --info) to examine archive files,
and dpkg --contents (= dpkg-deb --contents) to list their contents.
wuyujin@ubuntu18:~$
若软件包已安装,会显示安装位置:
wuyujin@ubuntu18:~$ dpkg -L nginx
/.
/usr
/usr/share
/usr/share/doc
/usr/share/doc/nginx
/usr/share/doc/nginx/copyright
/usr/share/doc/nginx/changelog.Debian.gz
wuyujin@ubuntu18:~$
- 卸载并清除配置
sudo apt-get remove nginx --purge
nginx安装位置
类型 | 路径 | 作用 |
---|---|---|
directory | /etc/nginx/ | 配置文件目录 |
directory | /var/log/nginx/ | 默认日志目录 |
directory | /var/www/html/ | 默认虚拟主机 |
file | /etc/nginx/nginx,conf | 配置文件 |
file | /usr/sbin/nginx | nginx启动脚本 |
file | /etc/init.d/nginx | nginx服务管理脚本 |
其中,默认虚拟目录/var/www/html/
下有两个默认的HTML文件:
wuyujin@ubuntu18:/var/www/html$ ll
total 24
drwxr-xr-x 2 root root 4096 2月 14 22:24 ./
drwxr-xr-x 3 root root 4096 1月 31 21:20 ../
-rw-r--r-- 1 root root 10918 2月 14 22:24 index.html
-rw-r--r-- 1 root root 612 1月 31 21:20 index.nginx-debian.html
一会启动nginx之后,可以测试能否访问到这两个文件。
nginx服务的启动、停止及测试页面的访问
- 服务的管理
- 查看服务状态
/etc/init.d/nginx status
- 启动服务
/etc/init.d/nginx start
- 停止服务
/etc/init.d/nginx stop
- 重载配置
/etc/init.d/nginx reload
- 查看服务状态
- 运行
-
启动nginx服务器
/etc/init.d/nginx start
-
访问nginx
nginx默认监听HTTP协议的80
端口,所以直接访问localhost:80即可。若如下图:
其实这里访问的是/var/www/html
目录下的index.html
文件,等同于:localhost:80/index.html。
也可以访问:localhost:80/index.nginx-debian.html
以上,若访问成功,则表示nginx环境搭建成功。 -
停止nginx
/etc/init.d/nginx stop
-
再次访问localhost:80,若不能访问,合理。这表示服务配置成功。
-
编译源码包方式安装
安装命令
我打算把nginx安装到/opt/nginx/
目录中,以下操作用普通用户wuyujin
执行。
wget http://nginx.org/download/nginx-1.16.1.tar.gz # 下载源码包,其他版本见<http://nginx.org/en/download.html>。
sudo mkdir /opt/nginx # 新建nginx安装目录
sudo chown wuyujin -R /opt/nginx/ # 修改目录属主
tar -zxf nginx-1.16.1.tar.gz # 解压源码包
cd nginx-1.16.1/ # 进入源码文件夹
./configure --prefix=/opt/nginx # 配置
make # 编译
make install # 安装
ll /opt/nginx/ # 查看目录结构
tree /opt/nginx -L 2
操作过程如下:
wuyujin@ubuntu18:~/Downloads$ sudo mkdir /opt/nginx # 新建nginx安装目录
wuyujin@ubuntu18:~/Downloads$ sudo chown wuyujin -R /opt/nginx/ # 修改目录属主
wuyujin@ubuntu18:~/Downloads$ tar -zxf nginx-1.16.1.tar.gz # 解压源码包
wuyujin@ubuntu18:~/Downloads$ cd nginx-1.16.1/ # 进入源码文件夹
wuyujin@ubuntu18:~/Downloads/nginx-1.16.1$ ./configure --prefix=/opt/nginx # 配置
checking for OS
+ Linux 5.3.0-40-generic x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)
checking for gcc -pipe switch ... found
/ ... 省略多行信息
creating objs/Makefile
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ using system zlib library
nginx path prefix: "/opt/nginx"
nginx binary file: "/opt/nginx/sbin/nginx"
nginx modules path: "/opt/nginx/modules"
nginx configuration prefix: "/opt/nginx/conf"
nginx configuration file: "/opt/nginx/conf/nginx.conf"
nginx pid file: "/opt/nginx/logs/nginx.pid"
nginx error log file: "/opt/nginx/logs/error.log"
nginx http access log file: "/opt/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
wuyujin@ubuntu18:~/Downloads/nginx-1.16.1$ make # 编译
make -f objs/Makefile
make[1]: Entering directory '/home/wuyujin/Downloads/nginx-1.16.1'
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/nginx.o \
src/core/nginx.c
/ ... 省略多行信息
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/ngx_modules.o \
objs/ngx_modules.c
cc -o objs/nginx \
objs/src/core/nginx.o \
/ ...省略多行信息
objs/src/http/modules/ngx_http_upstream_zone_module.o \
objs/ngx_modules.o \
-ldl -lpthread -lcrypt -lpcre -lz \
-Wl,-E
sed -e "s|%%PREFIX%%|/opt/nginx|" \
-e "s|%%PID_PATH%%|/opt/nginx/logs/nginx.pid|" \
-e "s|%%CONF_PATH%%|/opt/nginx/conf/nginx.conf|" \
-e "s|%%ERROR_LOG_PATH%%|/opt/nginx/logs/error.log|" \
< man/nginx.8 > objs/nginx.8
make[1]: Leaving directory '/home/wuyujin/Downloads/nginx-1.16.1'
wuyujin@ubuntu18:~/Downloads/nginx-1.16.1$ make install # 安装
make -f objs/Makefile install
make[1]: Entering directory '/home/wuyujin/Downloads/nginx-1.16.1'
test -d '/opt/nginx' || mkdir -p '/opt/nginx'
test -d '/opt/nginx/sbin' \
|| mkdir -p '/opt/nginx/sbin'
test ! -f '/opt/nginx/sbin/nginx' \
|| mv '/opt/nginx/sbin/nginx' \
'/opt/nginx/sbin/nginx.old'
cp objs/nginx '/opt/nginx/sbin/nginx'
test -d '/opt/nginx/conf' \
|| mkdir -p '/opt/nginx/conf'
cp conf/koi-win '/opt/nginx/conf'
cp conf/koi-utf '/opt/nginx/conf'
cp conf/win-utf '/opt/nginx/conf'
test -f '/opt/nginx/conf/mime.types' \
|| cp conf/mime.types '/opt/nginx/conf'
cp conf/mime.types '/opt/nginx/conf/mime.types.default'
test -f '/opt/nginx/conf/fastcgi_params' \
|| cp conf/fastcgi_params '/opt/nginx/conf'
cp conf/fastcgi_params \
'/opt/nginx/conf/fastcgi_params.default'
test -f '/opt/nginx/conf/fastcgi.conf' \
|| cp conf/fastcgi.conf '/opt/nginx/conf'
cp conf/fastcgi.conf '/opt/nginx/conf/fastcgi.conf.default'
test -f '/opt/nginx/conf/uwsgi_params' \
|| cp conf/uwsgi_params '/opt/nginx/conf'
cp conf/uwsgi_params \
'/opt/nginx/conf/uwsgi_params.default'
test -f '/opt/nginx/conf/scgi_params' \
|| cp conf/scgi_params '/opt/nginx/conf'
cp conf/scgi_params \
'/opt/nginx/conf/scgi_params.default'
test -f '/opt/nginx/conf/nginx.conf' \
|| cp conf/nginx.conf '/opt/nginx/conf/nginx.conf'
cp conf/nginx.conf '/opt/nginx/conf/nginx.conf.default'
test -d '/opt/nginx/logs' \
|| mkdir -p '/opt/nginx/logs'
test -d '/opt/nginx/logs' \
|| mkdir -p '/opt/nginx/logs'
test -d '/opt/nginx/html' \
|| cp -R html '/opt/nginx'
test -d '/opt/nginx/logs' \
|| mkdir -p '/opt/nginx/logs'
make[1]: Leaving directory '/home/wuyujin/Downloads/nginx-1.16.1'
wuyujin@ubuntu18:~/Downloads/nginx-1.16.1$ ll /opt/nginx/ # 查看目录结构
total 24
drwxr-xr-x 6 wuyujin root 4096 2月 9 23:48 ./
drwxr-xr-x 17 root root 4096 2月 9 23:47 ../
drwxr-xr-x 2 wuyujin wuyujin 4096 2月 9 23:48 conf/
drwxr-xr-x 2 wuyujin wuyujin 4096 2月 9 23:48 html/
drwxr-xr-x 2 wuyujin wuyujin 4096 2月 9 23:48 logs/
drwxr-xr-x 2 wuyujin wuyujin 4096 2月 9 23:48 sbin/
wuyujin@ubuntu18:~/Downloads/nginx-1.16.1$
- 查看nginx安装目录下的内容:
wuyujin@ubuntu18:~/Downloads/nginx-1.16.1$ tree /opt/nginx -L 2
/opt/nginx
├── conf
│ ├── fastcgi.conf
│ ├── fastcgi.conf.default
│ ├── fastcgi_params
│ ├── fastcgi_params.default
│ ├── koi-utf
│ ├── koi-win
│ ├── mime.types
│ ├── mime.types.default
│ ├── nginx.conf
│ ├── nginx.conf.default
│ ├── scgi_params
│ ├── scgi_params.default
│ ├── uwsgi_params
│ ├── uwsgi_params.default
│ └── win-utf
├── html
│ ├── 50x.html
│ └── index.html
├── logs
└── sbin
└── nginx
4 directories, 18 files
wuyujin@ubuntu18:~/Downloads/nginx-1.16.1$
- 配置环境变量
编辑/etc/profile
,添加如下配置:
# nginx
export NGINX_HOME=/opt/nginx
export PATH=$NGINX_HOME/sbin:$PATH
重启使最新配置对所有用户生效。
- 测试
echo $NGINX_HOME
whereis nginx
在哪里有|查看PATH
环境变量下,以此有哪些路径下有nginx
。
which nginx
是哪个|查看要执行的nginx的位置。
确定是我们配置的/opt//nginx/sbin/nginx
,就可以测试了。
nginx -v
查看版本
nginx -h
查看帮助信息
nginx的home目录
编译源码包方式安装, 相关的目录基本都在一个目录下。我的是/opt/nginx/
- 查看nginx安装目录下的内容:
wuyujin@ubuntu18:~/Downloads/nginx-1.16.1$ tree /opt/nginx -L 2
/opt/nginx
├── conf
│ ├── fastcgi.conf
│ ├── fastcgi.conf.default
│ ├── fastcgi_params
│ ├── fastcgi_params.default
│ ├── koi-utf
│ ├── koi-win
│ ├── mime.types
│ ├── mime.types.default
│ ├── nginx.conf
│ ├── nginx.conf.default
│ ├── scgi_params
│ ├── scgi_params.default
│ ├── uwsgi_params
│ ├── uwsgi_params.default
│ └── win-utf
├── html
│ ├── 50x.html
│ └── index.html
├── logs
└── sbin
└── nginx
4 directories, 18 files
wuyujin@ubuntu18:~/Downloads/nginx-1.16.1$
可以看到,默认的html目录下有两个html页面:index.html和50x.html。
那么,启动nginx服务器之后,就可以访问到这两个页面了。
nginx服务的启动、停止以及测试页面的访问
- 启动nginx
wuyujin@ubuntu18:/opt/nginx/sbin$ ./nginx -h # 查看帮助信息
nginx version: nginx/1.16.1
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /opt/nginx/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
wuyujin@ubuntu18:/opt/nginx/sbin$ nginx start # 试图启动
nginx: invalid option: "start"
wuyujin@ubuntu18:/opt/nginx/sbin$ nginx # 不加参数启动
nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)
wuyujin@ubuntu18:/opt/nginx/sbin$ sudo nginx # 启动
wuyujin@ubuntu18:/opt/nginx/sbin$
可以看到,通过源码包安装后,直接通过nginx
脚本来启动nginx,不用加start
。
并且要用超级管理员的权限执行:sudo nginx
,就是启动。
从-c filename : set configuration file (default: conf/nginx.conf)
选项可知,默认会加载conf/nginx.conf
作为配置文件,
也可以自己指定,如:sudo /opt/nginx/sbin/nginx -c ~/Desktop/my_nginx.conf
。
- 访问localhost:80
访问index.html:
访问不存在的页面,会报404错误:
以上,表示我们通过源码包安装的nginx也可以正常使用了。
依赖问题
如果在安装出现问题(我是在源码包方式安装的过程中碰到的问题),如缺少依赖等,执行以下:
- 安装依赖
sudo apt-get install gcc g++ autoconf automake make pcre pcre-devel openssl openssl-devel
其中,PCRE依赖如果不能通过包管理工具直接安装,使用以下操作,下载源码包编译安装:
wget https://ftp.pcre.org/pub/pcre/pcre-8.00.tar.gz # 更多版本见<http://www.pcre.org/>
tar -zxvf pcre-8.40.tar.gz
cd pcre-8.00/
./configure
make
make install
whatis pcre # Perl-compatible regular expressions
nginx的配置
先放一些基本的配置,后期会陆续补充更多细节:
server {
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /var/www/html/statium;
location / {
root /var/www/html/statium;
try_files $uri $uri/ /index.html last;
index index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}