Mac环境下安装nginx并本地部署项目

本文详细介绍了如何在Mac上通过Homebrew安装Nginx,检查安装状态,搜索软件,修改配置文件以设置自定义端口,并部署项目。步骤包括安装Nginx,启动服务,修改配置文件中的端口号,以及配置代理规则来访问本地项目。
摘要由CSDN通过智能技术生成
1、前提

必须安装了homebrew,可在终端输入命令brew -v查看是否已经安装,如果输入指令出现版本号说明已经安装成功
在这里插入图片描述

如果未安装先安装 homebrew官网地址

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

安装完之后再次输入brew -v查看是否有版本号

2、执行brew search nginx 命令查询要安装的软件是否存在

在这里插入图片描述

3、执行brew install nginx安装nginx

在这里插入图片描述

4、执行brew info nginx信息,可以查看ngxin下载的位置以及nginx配置文件存放路径等信息(以下是安装成功所展示的信息,不成功会提示其他信息)路径不用死记硬背,忘记了执行命令查看就可以了

在这里插入图片描述

在这里插入图片描述

5、查看nginx存放目录是不是如第4步所说的一致

终端执行以下命令:
在这里插入图片描述
终端执行以下命令:
在这里插入图片描述

六、在终端cd /opt/homebrew/Cellar/nginx/1.23.2/bin进入bin目录,然后执行nginx命令(没有报错就是启动成功)下图显示的端口号等下会说

在这里插入图片描述

7、根据需求或者实际情况,修改nginx的配置文件:

上图看到的端口号需要在 open /opt/homebrew/Cellar/nginx/1.23.2/bin此目录下的nginx.conf里面修改,
修改之后重新运行nginx -s reload命令就能打开你自定义的端口号啦
在这里插入图片描述
编辑器打开:
在这里插入图片描述

八、部署项目

1.首先需要将打包好的项目放到你指定的的路径下
我是存放在open /opt/homebrew/Cellar/nginx/1.23.2/duxi 此目录下
在这里插入图片描述
在nginx.conf里面的server对象下配置

本地测试没有配置代理如下配置
在这里插入图片描述
本地测试配置了代理
在这里插入图片描述
nginx.conf配置文件如下:


#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       8086;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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



        location /MARKETING/ {
            root /opt/homebrew/Cellar/nginx/1.23.2/duxi;
            try_files $uri $uri/ /MARKETING/index.html;
        }
        location /MARKETING/API/  {
            proxy_pass http://localhost:8082/marketing/;
        }

       

        # 测试
        location /MY_PROJECT/ {
            root /opt/homebrew/Cellar/nginx/1.23.2/duxi;
            try_files $uri $uri/ /MY_PROJECT/index.html;
        }
        # location /MARKETING/API/  {
        #     proxy_pass http://localhost:8082/marketing/;
        # }

        #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;
    #    }
    #}
    include servers/*;
}

配置完毕之后,重启nginx(nginx -s reload),然后把本地项目运行起来(npm run serve),然后把端口号改成nginx里面配置的端口号即可看到本地部署的项目
在这里插入图片描述

要在Docker中安装Nginx部署项目,可以按照以下步骤进行操作: 1. 首先,确保你已经安装了Docker。可以使用适合你操作系统的安装程序来安装Docker。 2. 拉取Nginx镜像。在终端中运行以下命令来从Docker Hub上拉取Nginx镜像: ``` docker pull nginx ``` 3. 创建一个新的Docker容器并运行Nginx。使用以下命令来创建并运行一个新的Nginx容器: ``` docker run --name my-nginx -d -p 80:80 nginx ``` 这个命令会创建一个名为`my-nginx`的容器,并且将容器的80端口映射到主机的80端口。这样你就可以通过访问`http://localhost`来查看Nginx默认的欢迎页面。 4. 将你的项目文件复制到Nginx容器中。可以使用`docker cp`命令将你的项目文件复制到运行中的Nginx容器内的指定目录。例如,要将项目文件复制到Nginx容器的`/usr/share/nginx/html`目录下,可以运行以下命令: ``` docker cp /path/to/your/project my-nginx:/usr/share/nginx/html ``` 这个命令将你的项目文件复制到了名为`my-nginx`的Nginx容器的`/usr/share/nginx/html`目录下。 5. 配置Nginx。如果你有特定的Nginx配置文件或配置文件夹,可以使用`docker cp`命令将其复制到Nginx容器中的相应目录。例如,假设你有一个名为`nginx.conf`的Nginx配置文件和一个名为`conf.d`的配置文件夹,可以使用以下命令将它们复制到Nginx容器中: ``` docker cp /path/to/your/nginx.conf my-nginx:/etc/nginx/nginx.conf docker cp /path/to/your/conf.d my-nginx:/etc/nginx/conf.d ``` 这样,你的Nginx容器就会使用你提供的配置文件和配置文件夹进行配置。 现在,你的Nginx已经安装并且你的项目已经部署到了Nginx容器中。你可以通过访问`http://localhost`来查看你的项目。记得将`localhost`替换为你实际的主机名或IP地址,如果需要的话。 : 使用`docker cp`命令将Nginx容器中的`/usr/share/nginx/html`目录复制到主机的`/home/nginx`目录。 : 使用`docker cp`命令将Nginx容器中的`/etc/nginx/conf.d`目录复制到主机的`/home/nginx/conf/conf.d`目录。 : 使用`docker cp`命令将Nginx容器中的`/etc/nginx/nginx.conf`文件复制到主机的`/home/nginx/conf/nginx.conf`文件。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吃葡萄不吐葡萄皮嘻嘻

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值