ubuntu安装nginx
安装nginx
更新系统软件包列表
shellsudo apt update升级系统
shellsudo apt upgrade确保nginx所需的依赖项安装完毕
shellsudo apt install -y curl gnupg2 ca-certificates lsb-release安装nginx
shellsudo apt install -y nginx启动nginx命令
shellsudo systemctl start nginx确保nginx在系统启动时自动启动,可以执行下面命令
shellsudo systemctl enable nginx查看nginx启动状态
shellsudo systemctl status nginx
nginx存放静态资源
业务要求:想将静态资源放在 /var/nginx/resources 目录下
知识点:nginx配置文件中包含一个默认的虚拟主机配置(/etc/nginx/sites-available/default),监听80端口,开发者可以修改此文件添加配置
修改文件
shellsudo vi /etc/nginx/sites-available/default修改server_name和添加location块

注意
- 如果是root,那么当用户访问 域名:80/resources/test.png时,实际的访问路径是 /var/nginx/resources/resources/test.png
- 如果是alias,那么当用户访问 域名:80/resources/test.png时,实际的访问路径是 /var/nginx/resources/test.png
- 另外:alias的值 /var/nginx/resources/ 结尾必须带斜杠!!!
修改文件后,重新加载nginx以应用修改(必不可少)
shellsudo systemctl reload nginx
常用命令
启动nginx服务
shellsudo systemctl start nginx停止nginx服务
shellsudo systemctl stop nginx重新启动nginx服务
shellsudo systemctl restart nginx查看nginx服务当前状态
shellsudo systemctl status nginx查看错误日志(nginx日志文件位于 /var/log/nginx/ 目录下)
shellsudo tail -f /var/log/nginx/error.log
参考文档