虚拟机Parallels Desktop安装nginx+php7并且允许外部访问

背景

之前一直在用docker跑php环境, 最近发现接口响应时间都在5s左右,开始以为是接口问题, 后来发现是docker 里面加载vendor非常慢。
我的目录是挂在的本地目录到容器。网上查资料是docker和mac文件系统不一致,导致读取时比较慢。感兴趣的可以看看下面这个文档

https://forums.docker.com/t/file-access-in-mounted-volumes-extremely-slow-cpu-bound/8076/152

网上有好几种解决办法, 比较常见的是虚拟机 或者 docker-sync.
这里主要说一下使用虚拟机配置

为什么要用虚拟机Parallels Desktop安装nginx+php7并且允许外部访问

  • mac上现在php7.1 mac 上 brew 已经搜索不到了。想安装貌似只能通过编译(第三方包安装的大多数也已经失效)。
  • mac上有其他软件,有些软件版本远比php7.1需要用到的版本高,安装了php7.1可能会影响到其他东东。
  • docker-sync 本来也是我的首选,奈何mac上安装也是频频报错,弃之。
  • 环境 虚拟机+centos8 + nginx(最新稳定版本) + php7.1 + 其他软件。
  • 安装这里不再细说了,网上的资料很多,而且不复杂, 都用yum安装即可

重点:这里记录一下安装完成后暴露出的问题整理记录

1、宿主机通过http访问nginx不通(虚拟机中用 curl http://localhost 正常)

端口映射问题, 设置路径 虚拟机-->偏好设置-->Shared-->端口转发规则
image
本机端口: 宿主机上的端口(本地端口)
目标地址: 直接选择虚拟机名称即可(也可以写虚拟机的ip地址)
目标端口:虚拟机里面的端口,也就是nginx项目配置访问的端口

#nginx 项目配置
server {
    listen       8090;
    server_name  localhost;
    root   /usr/share/nginx/html/testa;

    location / {
        index  index.php index.html index.htm;
        try_files $uri $uri/ /index.php?$args;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
        include        fastcgi_params;
    }
}

2、虚拟机端口不通,telnet不通

端口映射配置好后,虚拟机中用curl命令访问正常,所主机通过端口访问仍然不同,用telnet 虚拟机端口发现端口不同。
解决办法:关闭防火墙, 当然在防火墙中允许80端口也是可以的,不过我们是虚拟机为了以后这里直接关闭。

systemctl status firewall  // 查看防火墙状态
systemctl stop firewalld   // 关闭防火墙
systemctl disable firewalld  // 禁止防火墙

3、访问nginx 报错"An error occurred" php-fpm 没有启动成功, 报错信息如下。

An error occurred.
Sorry, the page you are looking for is currently unavailable.
Please try again later.

If you are the system administrator of this resource then you should check the error log for details.

Faithfully yours, nginx.

解决办法:启动php-fpm (我的php-fpm是用yum 安装的,所以直接使用systemctl 启用即可)

systemctl status php-fpm     // 查看php-fpm 状态
systemctl start php-fpm      // 启动php-fpm
systemctl stop php-fpm      // 停止php-fpm
systemctl restart php-fpm  // 重启php-fpm

4、共享目录挂在问题(重新设置pd desktop的共享目录)

设置路径: 虚拟机-->配置-->选项-->共享-->自定义文件夹
image

注意文件夹的权限:可读可写

5、报错 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream 这个错误的产生有两个问题

  • nginx配置错误,目录不存在
    检查nginx配置是否正确
  • selinux 没有关闭, 命令如下
[root@c8 ~]# getenforce    // 查看当前模式  Permissive 宽松模式(我们最终需要设置的模式)
Permissive
[root@c8 ~]# setenforce 1  // 设置模式 (1:严格模式, 0:宽松模式)
[root@c8 ~]# getenforce    // 查看当前模式  Enforcing 严格模式
Enforcing
[root@c8 ~]# setenforce 0  // 设置模式
[root@c8 ~]# getenforce    // 查看当前模式  
Permissive

6、最终的访问方式,通过端口映射访问项目 (localhost:8090)

7、其他

  • 记录一下php可能用到的扩展
yum install php-mysql php-gd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt php-xdebug
posted @ 2022-02-14 23:44  zzphper  阅读(524)  评论(0编辑  收藏  举报