天天看点

nginx的两种安装方式

我们知道nginx是非常火热的一块搭建web服务的软件,它具有两种不同的安装方式。一种是软件包管理工具直接安装,一种是下载源码包进行编译安装。各有各的优缺点,下面就详细介绍两种不同安装方式

安装nginx

安装nginx主要有两种方式,一种是yum安装,一种是源码包安装

  • yum安装简单,使用方便
  • 可以直接使用nginx命令等
  • 但文件不在同一位置,卸载时不是特别方便
  • 源码包安装在同一位置,版本可以自己选择
  • 容易卸载,并且可以自己指定使用第三方模块,可以根据自己的需求选定模块
  • 在自己的系统上进行编译出来的软件运行效率更高

yum安装

# 1.下载阿里源的EPEL源仓库
[[email protected] ~]# wget ‐O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel‐7.repo
# 2.安装nginx
[[email protected] ~]# yum install nginx ‐y
[[email protected] ~]# nginx ‐v
nginx version: nginx/1.12.2

           
  • 配置文件
/usr/sbin/nginx # 软件目录
/usr/share/nginx/html/index.html #网站默认目录
/etc/nginx/nginx.conf # 主配置文件
/var/log/nginx # 日志目录
           
  • nginx.conf主配置文件
[[email protected] html]# cat /etc/nginx/nginx.conf

user nginx; # 运行用户
worker_processes auto; # 启动进程数,通常和cpu核数一样
error_log /var/log/nginx/error.log; # 错误日志
pid /run/nginx.pid; PID文件

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024; # 单个进程的最大连接数
}
# http全局配置
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on; # 允许sendfile方式传输文件
    tcp_nopush          on; # 在sendfile方式启动下,使用TCP_CORK套接字
    tcp_nodelay         on; # 连接保持活动状态
    keepalive_timeout   65; # 连接超时时间,默认为75s
    types_hash_max_size 2048; # 设置类型哈希表的最大大小

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _; # 域名
        root         /usr/share/nginx/html; # 网站根目录

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html; # 错误页
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

[[email protected] html]# 

           

编译安装nginx

  • 首先是下载安装包并解压,源码包可以去nginx.org官网下载
[[email protected] nginx]# tar -xzf nginx-1.16.1.tar.gz 
[[email protected] nginx]# ll
总用量 1012
drwxr-xr-x. 8 1001 1001     158 8月  13 20:51 nginx-1.16.1
-rw-r--r--. 1 root root 1032630 8月  14 01:01 nginx-1.16.1.tar.gz
[[email protected] nginx]# cd nginx-1.16.1/
[[email protected] nginx-1.16.1]# ll
总用量 752
drwxr-xr-x. 6 1001 1001   4096 9月   4 15:04 auto
-rw-r--r--. 1 1001 1001 296463 8月  13 20:51 CHANGES
-rw-r--r--. 1 1001 1001 452171 8月  13 20:51 CHANGES.ru
drwxr-xr-x. 2 1001 1001    168 9月   4 15:04 conf
-rwxr-xr-x. 1 1001 1001   2502 8月  13 20:51 configure
drwxr-xr-x. 4 1001 1001     72 9月   4 15:04 contrib
drwxr-xr-x. 2 1001 1001     40 9月   4 15:04 html
-rw-r--r--. 1 1001 1001   1397 8月  13 20:51 LICENSE
drwxr-xr-x. 2 1001 1001     21 9月   4 15:04 man
-rw-r--r--. 1 1001 1001     49 8月  13 20:51 README
drwxr-xr-x. 9 1001 1001     91 9月   4 15:04 src
[[email protected] nginx-1.16.1]# 
           
  • nginx源码包目录解析
    • CHANGES 就是每个版本中提供了哪些功能以及bug的修复
    • CHANGES 因为作者是俄罗斯人,所以有一个俄罗斯语言的文件版本
    • auto目录中存放了大量脚本文件,和configure脚本程序有关。
    • conf目录中存放的是Nginx的配置文件,包含基本配置文件和对部分特性的配置文件。
    • configure文件是Nginx的自动脚本程序。
    • contrib 提供了两个破脚本和vim工具
    • html 目录存放了两个后缀名为.html的静态网页文件。
    • man 目录中Nginx中存放的是所有帮助文档,在安装完成后用man命令来查看帮助文档,如man nginx
    • src 目录中存放的是nginx软件的所有源代码
  • nginx.conf代码高亮显示
mkdir ~/.vim #创建.vim目录(如果没有的话)
cp -r contrib/vim/*  ~/.vim #在nginx目录下执行
           
  • 编译之前安装依赖软件gcc,然后编译
[[email protected] nginx-1.16.1]# yum -y install gcc gcc-c++ autoconf automake make
# 编译
[[email protected] nginx-1.16.1]# ./configure --prefix=/home/nginx/nginx/ --without-http_rewrite_module --without-http_gzip_module
!
!
Configuration summary
  + PCRE library is not used
  + OpenSSL library is not used
  + zlib library is not used

  nginx path prefix: "/home/nginx/nginx/"
  nginx binary file: "/home/nginx/nginx//sbin/nginx"
  nginx modules path: "/home/nginx/nginx//modules"
  nginx configuration prefix: "/home/nginx/nginx//conf"
  nginx configuration file: "/home/nginx/nginx//conf/nginx.conf"
  nginx pid file: "/home/nginx/nginx//logs/nginx.pid"
  nginx error log file: "/home/nginx/nginx//logs/error.log"
  nginx http access log file: "/home/nginx/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"

[[email protected] nginx-1.16.1]# 

           

接下来便可以使用make 命令来根据 Makefile 文件内容提供的合适规则编译生成出真正可供用户安装服务程序的二进制可执行文件了。

[email protected] frp 0.21.0]# make

运行二进制的服务程序安装包

[[email protected] frp 0.21.0]# make install

清理源码包临时 文件

[[email protected] frp 0.21.0]# make clean

最后防火墙记得放行80端口,运行./sbin/nginx 就可以开启服务了。如果你在自己编译安装时出现错误,记得看报错的提示。

继续阅读