0%

Nginx安装与配置

实验楼nginx课程练习地址:Nginx实战

简介

Nnginx 是一款高性能的 HTTP 和反向代理服务器软件,第一个开源版本诞生于 2004 年,虽然诞生较晚但经过十多年的发展,已经成为非常流行的 web 服务器软件。

为什么选择 Nginx

Nginx 和 Apache 相同点

  • 同是 HTTP 服务器软件,都采用模块化结构设计
  • 支持通用语言接口,如 PHP、Python 等
  • 支持正向代理和反向代理
  • 支持虚拟主机及 ssl 加密传输
  • 支持缓存及压缩传输
  • 支持 URL 重写
  • 模块多,扩展性强
  • 多平台支持

优势与劣势

Nginx的优势

  • 轻量级 安装文件小 运行时 CPU 内存使用率低
  • 性能强 支持多核,处理静态文件效率高,内核采用的 poll 模型最大可以支持 50K 并发连接
  • 支持热部署 同时启动速度快,可以在不间断服务的情况下对软件和配置进行升级
  • 负载均衡 支持容错和健康检查
  • 代理功能强大 支持无缓存的反向代理,同时支持 IMAP/POP3/SMTP 的代理

Nginx的劣势

  • 相比 Apache 模块要少一些,常用模块都有了,而且支持 LUA 语言扩展功能
    对动态请求支持不如 apache
  • Windows 版本功能有限,受限于 windows 的特性,支持最好的还是 Unix 系统

Nginx 工作原理

Nginx 由内核和一系列模块组成,内核提供 web 服务的基本功能,如启用网络协议,创建运行环境,接收和分配客户端请求,处理模块之间的交互。Nginx 的各种功能和操作都由模块来实现。Nginx 的模块从结构上分为核心模块、基础模块和第三方模块。

核心模块: HTTP 模块、EVENT 模块和 MAIL 模块

基础模块: HTTP Access 模块、HTTP FastCGI 模块、HTTP Proxy 模块和 HTTP Rewrite 模块

第三方模块: HTTP Upstream Request Hash 模块、Notice 模块和 HTTP Access Key 模块及用户自己开发的模块

这样的设计使 Nginx 方便开发和扩展,也正因此才使得 Nginx 功能如此强大。Nginx 的模块默认编译进 nginx 中,如果需要增加或删除模块,需要重新编译 Nginx,这一点不如 Apache 的动态加载模块方便。如果有需要动态加载模块,可以使用由淘宝网发起的 web 服务器 Tengine,在 nginx 的基础上增加了很多高级特性,完全兼容 Nginx,已被国内很多网站采用。

Nginx 的常用架构

web 历史上最流行最经典的环境是 LAMP(Linux + Apache + Mysql + PHP),至今仍有大量网站采用此架构,Apache 默认配置在未优化的情况下比较占用 CPU 和内存。借助于 Nginx 的轻量和高性能,LNMP 架构只是将 LAMP 环境中的 Apache 换成 Nginx,于是另一经典 LNMP 架构就诞生了。LNMP 在服务器硬件配置相同时,相对于 LAMP 会使用更少的 CPU 和内存,是小型网站,低配服务器,和 VPS 的福音。

安装与启动

  • 环境:ubuntu 20.04

使用apt安装:

sudo apt-get install nginx
启动/关闭/状态查看/重新启动
sudo /etc/init.d/nginx [start|stop|status|restart]
也可以用systemctl工具
sudo systemctl [start|stop|status|restart] nginx
旧版的service
sudo service nginx [start|stop|status|reload]

手动编译

使用包管理器安装固然方便,单后续重新编译模块将会很麻烦,手动编译安装可以避免这一问题,还有助于我们更好理解nginx:
cd /usr/local,下载源码:sudo wget http://nginx.org/download/nginx-1.18.0.tar.gz
解压:sudo tar -zxvf nginx-1.18.0.tar.gz
删除压缩包:sudo rm nginx-1.18.0.tar.gz
sudo mv nginx-1.18.0 nginx && cd nginx

nginx编译需要依赖zlib,pcre,openssl,ubuntu下安装这些包:sudo apt install openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev

执行配置:sudo ./configure进行编译前的参数设置(可以用./configure --help查看具体配置参数),配置完后显示相关目标路径:

nginx path prefix: "/usr/local/nginx" #安装目录
nginx binary file: "/usr/local/nginx/sbin/nginx" #二进制文件位置
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx"
nginx configuration file: "/usr/local/nginx/nginx.conf" 配置文件
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/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"

配置完后,目录下会生成Makefile,编译安装:sudo make && sudo make install

添加/usr/local/nginx/sbin$PATH,输入nginx -v

nginx version: nginx/1.18.0

打印出版本信息,说明安装成功。

nginx -t检测nginx.conf配置文件是否合法,然后sudo nginx启动,可通过浏览器输入http://localhost查看启动情况。

基础配置

sudo vim /etc/nginx/sites-available/default
取消php模块的注释:

location ~ \.php$ {
include snippets/fastcgi-php.conf;

# With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# With php7.0-fpm:
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}

测试配置:
sudo nginx -t

显示如下则表示成功:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

使配置文件生效:
sudo systemctl restart nginx

在 LNMP 中的作用或角色:nginx 本身不能处理 PHP,它只是个 web 服务器,当接收到请求后,如果是 php 请求,则发给 php 解释器处理,并把结果返回给客户端.php-fpm 是一个守护进程(FastCGI 进程管理器)用于替换 PHP FastCGI 的大部分附加功能,对于高负载网站是非常有用的。安装php,并启动:
sudo apt-get install -y php7.0-fpm
sudo systemctl start php7.0-fpm

此时在webRoot(笔者为/var/www/html)下新建一个php测试文件test.php,输入:
<?php
echo('<h1>It works!</h1>');
?>

保存,输入域名地址http://your-server-domain/test.php,显示It works说明成功了。

如果是安装LNMP,还差一个mysql,首先安装mysql,然后还需要php和mysql的连接器,安装完成后还需要重启php-fpm

sudo apt-get install -y php7.0-mysql
sudo systemctl restart php7.0-fpm

有时候需要改变nginx监听的端口(虽然我觉得不太用得上。。。)sudo vim /etc/nginx/sites-available/default,原配置一般是这样:

server {
listen 80 default_server;
listen [::]:80 default_server;
.....

修改两个listen的端口,这里应该是对应的ipv4和ipv6监听的端口
server {
listen 9000 default_server;
listen [::]:9000 default_server ipv6only=on;

如果哦nginx启动不了,先看sudo systemctl status nginx 中的错误信息,然后查看日志 tail /var/log/nginx/error.log

Nginx进程和模块

Disqus评论区没有正常加载,请使用科学上网