[玩转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 自动启动和容器自动启动

目录
相关文章
|
4天前
|
应用服务中间件 PHP nginx
Mac安装Nginx
Mac安装Nginx
11 2
Mac安装Nginx
|
3天前
|
Linux C语言
成功解决 在Linux CentOS 7 中安装gcc
这篇文章介绍了如何在Linux CentOS 7系统中安装gcc (g++) 8工具集。由于CentOS 7默认的gcc版本是4.8,而这个版本与Qt 5.14、Qt 5.15或更高版本不兼容,可能会导致编译时出现系统头文件错误。文章中提到,即使在项目配置中添加了`CONFIG+=c++11`,如果仍然报错,那么很可能是gcc版本的问题。为了解决这个问题,文章提供了使用CentOS的Software Collections (scl)来安装更新版本的gcc的步骤。
成功解决 在Linux CentOS 7 中安装gcc
|
3天前
|
关系型数据库 Java MySQL
Linux安装JDK1.8 & tomcat & MariaDB(MySQL删减版)
本教程提供了在Linux环境下安装JDK1.8、Tomcat和MariaDB的详细步骤。这三个组件的组合为Java Web开发和部署提供了一个强大的基础。通过遵循这些简单的指导步骤,您可以轻松建立起一个稳定、高效的开发和部署环境。希望这个指导对您的开发工作有所帮助。
24 8
|
5天前
|
Linux Python
Linux 升级安装 Python 3
Linux 升级安装 Python 3
12 2
|
5天前
|
Linux 网络安全 Python
Linux离线安装Python时ssh和hashlib死活安装不上的解决方案
本文提供了Linux环境下离线安装Python时遇到的"ImportError: No module named _ssl"和"ERROR:root:code for hash md5|sha1|sha224|sha256|sha384|sha512 was not found"两个问题的解决方案,通过设置OpenSSL环境变量和编辑Python源码配置文件来解决。
9 1
|
5天前
|
应用服务中间件 Linux nginx
在CentOS上使用源码包安装Nginx、以及手动启动Nginx的步骤过程
这篇文章介绍了在CentOS系统上使用Nginx源码包进行安装和配置的详细步骤,包括源码包的获取、解压、配置、编译、安装、启动验证以及注意事项。
20 0
在CentOS上使用源码包安装Nginx、以及手动启动Nginx的步骤过程
|
5天前
|
存储 NoSQL Java
使用redis进行手机验证码的验证、每天只能发送三次验证码 (redis安装在虚拟机linux系统中)
该博客文章展示了如何在Linux虚拟机上使用Redis和Jedis客户端实现手机验证码的验证功能,包括验证码的生成、存储、验证以及限制每天发送次数的逻辑,并提供了测试结果截图。
使用redis进行手机验证码的验证、每天只能发送三次验证码 (redis安装在虚拟机linux系统中)
|
5天前
|
Linux
虚拟机安装Linux系统的网络配置
该博客文章提供了解决虚拟机中Linux系统网络问题的多种方法,包括重置网络服务、修改网络配置文件、使用不同网络模式等,以确保虚拟机能够成功连接到网络。
虚拟机安装Linux系统的网络配置
|
3天前
|
机器学习/深度学习 Linux Shell
超详细的Linux-Conda环境安装教程
超详细的Linux-Conda环境安装教程
|
4天前
|
关系型数据库 MySQL 应用服务中间件
在Ubuntu 16.04上使用Nginx安装和保护phpMyAdmin的方法
在Ubuntu 16.04上使用Nginx安装和保护phpMyAdmin的方法
10 0