PHP7+nginx+mysql开发环境

nginx

  • 使用brew install nginx 安装nginx,注意如果卡在brew update上面的话可以使用ctrl+c跳过
    当然这种方法治标不治本,具体参考 [Linux实践] macOS平台Homebrew更新brew update卡死,完美解决

  • mac查看端口对应的进程
    在Linux下,查看占用端口的进程可以使用netstat -antpl | grep port,但是在Mac下,这个netstat的功能缩水了(很多命令在Mac下都缩水了),所以怎么查看占用端口的进程呢?
    搜索了一遍,感觉可以使用lsof -i tcp:{port} 这个命令来实现,{port} 表示端口号。

  • 网址:localhost:8080

  • Nginx默认index.html文件的位置
    在nginx.conf文件中,我们看到根路径是:

root   html;

问题是:“html”的相对路径在哪里?此相对路径在编译时设置。您可以通过命令检查路径

$>nginx -V

您将看到“--prefix = / usr / local / Cellar / nginx / 1.12.0_1”,这是nginx文件的文件夹。现在你应该“cd”到这个目录,看看你的“html”文件夹。

$> cd /usr/local/Cellar/nginx/1.12.0_1
$> ls -l html

然后你会看到“html”文件夹是“/ usr / local / var / www”的软链接

总之,在我的例子中,“html”文件夹是“/ usr / local / var / www”。

php环境的搭建

mac自带php,但是环境配置需要进行一些添加

1. nginx php-fpm file not found错误

server {
    listen   [::]:80;
    server_name  example.com www.example.com;
    access_log  /var/www/logs/example.com.access.log;  

    location / {
        root   /var/www/example.com;
        index  index.html index.htm index.pl;
    }

    location /images {
        autoindex on;
    }

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/example.com$fastcgi_script_name;
        include fastcgi_params;
    }
}

这个配置中有很多不合理的地方,其中一个明显的问题就是root指令被放到了location /块。如果root指令被定义在location块中那么该root指令只能对其所在的location生效。其它location中没有root指令,像 location /images块不会匹配任何请求,需要在每个请求中重复配置root指令来解决这个问题。因此我们需要把root指令放在server块,这样各个 location就会继承父server块定义的$document_root,如果某个location需要定义一个不同 的$document_root,则可以在location单独定义一个root指令。

另一个问题就是fastCGI参数SCRIPT_FILENAME 是写死的。如果修改了root指令的值或者移动文件到别的目录,php-fpm会返回“No input file specified”错误,因为SCRIPT_FILENAME在配置中是写死的并没有随着$doucument_root变化而变化,我们可以修改 SCRIPT_FILENAME配置如下:

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

所以我们不能忘记在server块中配置root指令,不然$document_root的值为空,只会传$fastcgi_script_namephp-fpm,这样就会导致“No input file specified”错误。

2. 服务器nginx配置问题the page you are looking for is currently unavailable

这个可能出现的问题原因之一是服务器里面有个叫 php-fpm 被停止了。
首先用
ps -ef|grep 9000
监听端口,如果发现 php-fpm被停止了,给启动一下就可以了。
去到对应的php/sbin目录下面执行:./php-fpm

其他常见的对应端口:
ngnx启动对应80端口
php-fpm 对应9000
mysql 对应 3306

3. php-fpm启动报错 failed to open error_log (/usr/var/log/php-fpm.log): No such file or directory

在php-fpm.conf文件下面的[global]
把注释去掉,并且改为

[global]
error_log = /usr/local/var/log/php-fpm.log

或者不修改配置文件中配置项的路径,在php-fpm的运行参数中(-p)指定放置运行时文件的相对路径前缀

$ php-fpm --fpm-config /private/etc/php-fpm.conf  --prefix /usr/local/var

到此,php-fpm守护进程已经基本可以正确的启动了。
当然如果没有该文件,可以执行
cp /private/etc/php-fpm.conf.default /private/etc/php-fpm.conf

参考https://github.com/musicode/test/issues/5

Mac 自带 php-fpm,在终端执行 php-fpm,会报如下错误:

ERROR: failed to open configuration file '/private/etc/php-fpm.conf': No such file or directory (2)
ERROR: failed to load configuration file '/private/etc/php-fpm.conf'
ERROR: FPM initialization failed

错误信息显示,不能打开配置文件,cd /private/etc,发现没有 php-fpm.conf 文件,但是有 php-fpm.conf.default 文件。这个文件是默认配置,我们可以复制一份,改名为 php-fpm.conf,然后再根据需要改动配置。
cp /private/etc/php-fpm.conf.default /private/etc/php-fpm.conf

执行 php-fpm,再次报错:

ERROR: failed to open error_log (/usr/var/log/php-fpm.log): No such file or directory (2)
ERROR: failed to post process the configuration
ERROR: FPM initialization failed

错误信息显示,不能打开错误日志文件。cd /usr/var/log 发现根本没有这个目录,甚至连 var 目录都没有,加上为了避免权限问题,干脆配置到 /usr/local/var/log 目录。

修改 php-fpm.conf error_log 配置为 /usr/local/var/log/php-fpm.log,并把 user 和 group 改为和当前用户一样。

执行 php-fpm,再次报错:

NOTICE: [pool www] 'user' directive is ignored when FPM is not running as root
NOTICE: [pool www] 'group' directive is ignored when FPM is not running as root

于是 sudo php-fpm,再次报错:

ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (48)
ERROR: FPM initialization failed

编辑 php-fpm.conf,修改 listen 为 127.0.0.1:9999。

执行 php-fpm -t,这个世界终于清净了!

mysql

brew install [email protected]

[email protected] is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have [email protected] first in your PATH run:
  echo 'export PATH="/usr/local/opt/[email protected]/bin:$PATH"' >> /Users/misaki/.bash_profile

For compilers to find [email protected] you may need to set:
  export LDFLAGS="-L/usr/local/opt/[email protected]/lib"
  export CPPFLAGS="-I/usr/local/opt/[email protected]/include"

For pkg-config to find [email protected] you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/[email protected]/lib/pkgconfig"


To have launchd start [email protected] now and restart at login:
  brew services start [email protected]
Or, if you don't want/need a background service you can just run:
  /usr/local/opt/[email protected]/bin/mysql.server start
==> Summary
  /usr/local/Cellar/[email protected]/5.7.31: 319 files, 235.8MB

mysql安装的时候安装成默认版本后来remove重安装报错:ERROR! The server quit without updating PID file
找了很多百度,都无效,结果还是这篇文章救命了 MySQL 5.7 installed via brew now getting ERROR! The server quit without updating PID file (/usr/local/var/mysql/dev_box.local.pid)

翻译一下

I had mysql version 8 earlier, due to some dependency i had to downgrade to 5.7 so installed [email protected] via brew.
# 我本来安装了mysql8.0版本,但是由于某些原因我需要对mysql降级,降级步骤如下
brew uninstall mysql
brew install [email protected]
mysql.server start
brew link [email protected] --force
Now once i have stopped the server and now i want to restart it but i am getting
# 但现在我遇到了bug
ERROR! The server quit without updating PID file (/usr/local/var/mysql/dev_box.local.pid).

I had the same situation. What helped me is:
# 我遇到类似状况,下面是我的处理方式
First uninstall mysql using brew
# 首先使用brew卸载mysql
brew uninstall mysql
brew uninstall [email protected]
Then manually go through all folders in /usr/local/ and delete everything related to mysql.
# 需要手动删除所有/var/local下面个步mysql相关的文件
For example, I had /usr/local/etc/my.cnf and my.cnf.default, so had to run rm -f /usr/local/etc/my*; also /usr/local/mysql-5.7.21-macos10.13-x86_64 and mysql-8.0.16-macos10.14-x86_64, so rm -rf /usr/local/mysql* and so on.
#执行 run rm -f /usr/local/etc/my*

#执行 rm -rf /usr/local/mysql* 

After this is done install using brew
# 然后用brew重新安装
brew install [email protected]
brew services start [email protected]

你可能感兴趣的:(PHP7+nginx+mysql开发环境)