Linux 安装 nginx 详细教程


提示:以下是本篇文章正文内容,Linux 系列学习将会持续更新

Linux 安装 nginx 详细步骤

①安装依赖包

下载模块依赖性 Nginx 需要依赖下面3个包

  1. gzip 模块需要 zlib 库 ( 下载: http://www.zlib.***/ )
  2. rewrite 模块需要 pcre 库 ( 下载: http://www.pcre.org/ )
  3. ssl 功能需要 openssl 库 ( 下载: http://www.openssl.org/ )

一键安装四个依赖

yum -y install g*** zlib zlib-devel pcre-devel openssl openssl-devel

②下载并解压安装包

wget http://nginx.org/download/nginx-1.13.7.tar.gz
tar -xvf nginx-1.13.7.tar.gz

③安装 nginx

# 进入安装包目录
cd nginx-1.13.7
# 编译,执行配置: 考虑到后续安装ssl证书 添加两个模块
./configure --with-http_stub_status_module --with-http_ssl_module
# 安装
make && make install

报错:

如果出现这种错误,找到 nginx-1.13.7/src/os/unix/ngx_usr.c,找到 cd.current_salt[0] = ~salt[0] 给删除掉,然后保存退出,再次安装 nginx。


看到这种错误时也不要慌,进入 nginx-1.13.7/objs/Makefile,打开 Makefile 文件将编译选项中的 CFLAGS = -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -werror -g 中的 -werror 删除,再重新 make。


这就安装成功了。

回到目录…

④启动 nginx 服务

安装好的 nginx 服务在 /usr/local/nginx 下:

进入 /usr/local/nginx/sbin 目录下启动:

# 启动
./nginx

# 重启
./nginx -s reload

# 关闭
./nginx -s stop

# 或者,指定配置文件启动
./nginx -c /usr/local/nginx/conf/nginx.conf

查看进程:ps -ef | grep nginx

启动后,直接访问云服务器的外网IP就行,http://1.15.76.95

⑤nginx 反向代理

a. 找到 nginx.conf 配置文件

vim /usr/local/nginx/conf/nginx.conf

b. 查看官方默认的配置文件

#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"';

    #a***ess_log  logs/a***ess.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen   80;
        server_name  localhost;

        #charset koi8-r;

        #a***ess_log  logs/host.a***ess.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 a***ess to .hta***ess 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;
    #    }
    #}
}

c. 我们手动添加两个 Nacos 服务让 Nginx 做反向代理:

......

http {
    ......
    
    # 添加两个 nacos 服务器
    upstream nacos-server {
        server 10.0.4.12:8848;
        server 10.0.4.12:8850;
    }

    server {
        listen   80;
        server_name  localhost;

		# 添加需要转发的路径
        location /nacos {
            proxy_pass http://nacos-server;
        }
        ......
    }
    ......
}

d. 重启 Nginx 服务器,访问地址:http://1.15.76.95/nacos,成功连接到 Nacos 服务器上。

回到目录…


总结:
提示:这里对文章进行总结:
本文是对Linux的学习,学习了如何在 Linux 环境下安装 nginx,并且解决了安装过程中的报错。之后的学习内容将持续更新!!!

转载请说明出处内容投诉
CSS教程_站长资源网 » Linux 安装 nginx 详细教程

发表评论

欢迎 访客 发表评论

一个令你着迷的主题!

查看演示 官网购买