[玩转Linux] 安装Nginx

简介: [玩转Linux] 安装Nginx

简介

方法 / 步骤

一:使用Docker 安装 (不推荐)

1.1 Docker 搜索并拉取镜像

docker search nginx
# 指定版本
docker pull nginx
#最新版本
docker pull nginx:latest
# 查看本地镜像
docker images

在这里插入图片描述

1.2 运行容器

默认路径:
html文件路径: /etc/nginx/html
配置文件路径:/etc/nginx/nginx.conf
日志存放路径:/var/log/nginx
docker run \
-p 80:80 \
--privileged=true \
--name nginx \
-v /docker/nginx80/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /docker/nginx80/html:/usr/share/nginx/html \
-v /docker/nginx80/log:/var/log/nginx \
-e TZ=Asia/Shanghai \
-d nginx
  • 注意运行不成功的话记得把 /docker/nginx80/conf/nginx.conf 下的nginx.conf 文件夹删除,换成一个空文件
  • 运行成功

1.3 修改配置

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;

    #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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

1.4 设置自动启动

# 开启容器自启动
docker update --restart=always 【容器名】
例如:docker update --restart=always tracker
 
 
# 关闭容器自启动
docker update --restart=no【容器名】
例如:docker update --restart=no tracker
 
##### 相关配置解析
no:
    不要自动重启容器。(默认)
 
on-failure: 
    如果容器由于错误而退出,则重新启动容器,该错误表现为非零退出代码。
 
always:
    如果容器停止,请务必重启容器。如果手动停止,则仅在Docker守护程序重新启动或手动重新启动容器本身时才重新启动。(参见重启政策详情中列出的第二个项目)
 
unless-stopped:
    类似于always,除了当容器停止(手动或其他方式)时,即使在Docker守护程序重新启动后也不会重新启动容器。
  • 例如
docker update --restart=always dev-redis
docker update --restart=always mom-rabbitmq
docker update --restart=always mysql8.0
docker update --restart=always mysql5.7
docker update --restart=always jeecg3.2

二:离线安装 (推荐)

2.1 前置环境进行准备

主要安装的前置依赖包有

  1. 编译依赖的 gcc 环境
  2. 正则表达依赖的 Perl库 PCRE ((Perl Compatible Regular Expressions))
  3. 压缩和解压缩的zlib库
  4. OpenSSL https的SSL 协议

# 安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:

yum install gcc-c++
# PCRE pcre-devel 安装
# 查看是否已经安装过该软件(如果出现包名说明已经安装):
rpm -qa pcre

# PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。
#nginx 的 http 模块使用 pcre 来解析正则表达式,
#所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。
#nginx也需要此库。命令:
yum install -y pcre pcre-devel

# zlib 安装
# zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip 
#所以需要在 Centos 上安装 zlib 库。

yum install -y zlib zlib-devel

# OpenSSL 安装
#OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、
#常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
#nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。
yum install -y openssl openssl-devel

2.2 离线安装

点击 下载Nginx离线包

2.2.1 默认编译配置

# 解压并进入目录
tar -zxvf nginx-1.10.1.tar.gz
cd nginx-1.10.1
# 创建nginx安装文件夹
mkdir /usr/local/nginx
#在编译的时候 需要配置 ssl支持
./configure --prefix=/usr/local/nginx --with-http_ssl_module
#注:也可以直接进行./configure 但是反向代理到HTTPS服务的时候会提示需要SSL的支持;
./configure

2.2.2 编译安装

make & make install
  • 启动、停止 重载命令
cd /usr/local/nginx/sbin/
./nginx 
./nginx -s stop
./nginx -s quit
./nginx -s reload

2.2.3 开机自动启动

即在rc.local增加启动代码就可以了。

vi /etc/rc.local
# 增加一行 
/usr/local/nginx/sbin/nginx
# 设置执行权限:
chmod 755 /etc/rc.local

参考资料 & 致谢

[1] Docker 自动启动和容器自动启动

目录
相关文章
|
17天前
|
NoSQL 关系型数据库 Redis
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo
mall在linux环境下的部署(基于Docker容器),docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongodb、minio详细教程,拉取镜像、运行容器
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo
|
27天前
|
缓存 NoSQL Linux
【Azure Redis 缓存】Windows和Linux系统本地安装Redis, 加载dump.rdb中数据以及通过AOF日志文件追加数据
【Azure Redis 缓存】Windows和Linux系统本地安装Redis, 加载dump.rdb中数据以及通过AOF日志文件追加数据
【Azure Redis 缓存】Windows和Linux系统本地安装Redis, 加载dump.rdb中数据以及通过AOF日志文件追加数据
|
16天前
|
Ubuntu Linux Shell
Linux系统命令 安装和文件相关命令
本文档详细介绍了Linux系统中的常用命令,包括软件安装卸载命令如`dpkg`和`apt-get`,压缩与解压命令如`gzip`、`bzip2`和`xz`,以及`tar`命令用于打包和解包。此外还介绍了文件分割命令`split`,文件操作命令如`cat`、`head`、`tail`、`more`、`less`等,管道命令和`wc`、`grep`、`find`、`cut`、`sort`、`uniq`、`diff`等实用工具。最后,文档还讲解了文件属性相关的命令如`chmod`、`chown`、`chgrp`以及创建硬链接和软链接的`ln`命令。
|
22天前
|
Linux TensorFlow 算法框架/工具
在Linux上安装其他版本的cmake 或 升级cmake
在Linux上安装其他版本的cmake 或 升级cmake
33 2
|
22天前
|
人工智能 Linux 开发工具
Linux安装Taiyi stable-diffusion-webui
Linux安装Taiyi stable-diffusion-webui
|
23天前
|
Ubuntu Linux 虚拟化
安装Windows Linux 子系统的方法:适用于windows 11 版本
本文提供了在Windows 11系统上安装Linux子系统(WSL)的详细步骤,包括启用子系统和虚拟化功能、从Microsoft Store安装Linux发行版、设置WSL默认版本、安装WSL2补丁,以及完成Ubuntu的首次安装设置。
65 2
|
24天前
|
缓存 应用服务中间件 nginx
安装nginx-http-flv-module模块
本文介绍如何为Nginx安装`nginx-http-flv-module`模块。此模块基于`nginx-rtmp-module`二次开发,不仅具备原模块的所有功能,还支持HTTP-FLV播放、GOP缓存、虚拟主机等功能。安装步骤包括:确认Nginx版本、下载相应版本的Nginx与模块源码、重新编译Nginx并加入新模块、验证模块安装成功。特别注意,此模块已包含`nginx-rtmp-module`功能,无需重复编译安装。
66 1
|
27天前
|
关系型数据库 MySQL Linux
Linux环境安装MySQL8.0.36使用rpm包安装,安装顺序是什么?
【8月更文挑战第23天】Linux环境安装MySQL8.0.36使用rpm包安装,安装顺序是什么?
136 1
|
27天前
|
应用服务中间件 Linux PHP
【Azure 应用服务】App Service For Linux 环境中,如何修改 Nginx 配置中 server_name的默认值 example.com
【Azure 应用服务】App Service For Linux 环境中,如何修改 Nginx 配置中 server_name的默认值 example.com
|
27天前
|
关系型数据库 Linux PostgreSQL
【Azure 应用服务】Azure Function App Linux环境下的Python Function,安装 psycopg2 模块错误
【Azure 应用服务】Azure Function App Linux环境下的Python Function,安装 psycopg2 模块错误