您的位置:首页 > 运维架构 > Nginx

在 Mac 上搭建 Nginx PHP Mysql 开发环境

2015-06-06 12:07 791 查看
其实这个过程跟Linux下安装都差不多,只是部分命令有差别,大同小异。
网上看到很多教程都是用 brew 之类的包管理器安装,但是 Mac 自带了 php , 难道还要再装一个第三方的?强迫症果断不能忍,于是就想利用自带的 php-fpm 来搭建,没想到一搭建就是两个小时,在这里把过程整理一下备用,同时也方便需要的人。
因为是调试环境,所以没有注重安全方面,有些东西直接 chmod 777 了 要用于ws的话还是改改吧。
下载 nginx 源代码 并开始编译
tar zxvf nginx.tar.gz
cd nginx−1.7.4
./configure

执行报错:
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using −−without−http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using −−with−pcre=<path> option.

Rewrite 需要 PCRE 库的支持 , 下载pcre库后进行编译安装
tar xvzf pcre.tar.gz
cd pcre-8.35
./configure
make
sudo make install

重新编译nginx
cd ..
./configure
make
sudo make install

创建一个符号链接让开关容易一些
sudo ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx

有了 web 服务,接着尝试启动系统自带的php-fpm
php-fpm

执行报错
ERROR: failed to open configuration file '/private/etc/php-fpm.conf': No such file or directory (2)

找不到配置文件,为了省事就直接把.default复制,然后赋予权限
sudo cp /private/etc/php-fpm.conf.default /private/etc/php-fpm.conf
sudo chmod 777 /private/etc/php-fpm.conf
sudo php-fpm

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

找不到文件夹就新建
sudo mkdir /usr/var
sudo mkdir /usr/var/log
sudo php-fpm

此时PHP正常启动,不过还是需要完善一下 ,放置一下配置文件,修改一下权限,如果开公网访问的话“中奖”就杯具了,这种东西还是不要用 su 执行好一点。
cp /etc/php.ini.default  /etc/php.ini
sudo chmod 777 /etc/php.ini
sudo killall php-fpmphp-fpm

此时PHP正常启动 有两个notice 可以不管他
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

修改 nginx 配置文件 使其可以处理php
location ~ \.php$ {
      root   /path/to/wwwroot;
      fastcgi_pass   127.0.0.1:9000;
      fastcgi_index  index.php;
      include        fastcgi_params;
      fastcgi_param  SCRIPT_FILENAME  /path/to/wwwroot$fastcgi_script_name;
}

启动 nginx
sudo nginx

接下来就是 mysql 了
从官方下载 ( 下载DMG的话安装更简单的,可是我手贱下载了tar )http://dev.mysql.com/downloads/mysql/
tar zxvf mysql.tar.gz
cd mysql-*
mkdir /User/typcn/mysql
mv * /User/typcn/mysql/
cd /User/typcn/mysql
sudo chown -R _mysql .
#赋予权限
sudo chgrp -R _mysql . 
sudo scripts/mysql_install_db --user=_mysql --datadir=/User/typcn/mysql/data
#执行安装
sudo chown -R root . 
sudo chown -R _mysql data 
subl my.cnf

编辑Mysql 配置文件
basedir = /path/to/mysql
datadir = /path/to/mysql/data
port = 3306 
server_id = 1 
socket = /tmp/mysql.sock
user = _mysql

用脚本管理Mysql
sudo ln -s /User/typcn/mysql/support-files/mysql.server /usr/local/bin/mysql
subl support-files/mysql.server

修改配置文件
basedir=/path/to/mysql
datadir=/path/to/data
mysqld_pid_file_path=/path/to/mysql.pid

好吧。我承认pid什么的放tmp是个坏习惯。
sudo mysql start

现在访问 localhost 试试吧


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: