Mac OSX 下安装nginx mysql php 本地开发环境

2021-07-09 11:44:18 浏览数 (1)

温馨提示:本文的一些资源可能会因为gfw的原因无法正常安装,建议挂上科学上网工具进行部署。

再windows下有WAMP等安装包搭建本地环境,但是mac下只有MAMP PRO和XAMPP这两个安装包,前者还是付费的,这两个玩意有共同的缺点就是给你的app目录加上无用的文件夹和管理面板。。作为一个强迫症完全无法忍受,所以直接在服务器上远程调试了。

昨天mufeng给我推荐了一篇文章,但是按照教程搭建却失败了,打算折腾一下,确实失败了。但是排查了一下成功了。

现在完美运行,而且还是支持SSL,开心。

首先要安装Xpre,可以直接在app store下载安装。

Brew

Brew 是 Mac 下面的包管理工具,通过 Github 托管适合 Mac 的编译配置以及 Patch,可以方便的安装开发工具。 Mac 自带ruby 所以安装起来很方便,同时它也会自动把git也给你装上。官方网站: http://brew.sh 。

安装完成之后,建议执行一下自检,brew doctor如果看到Your system is ready to brew. 那么你的brew已经可以开始使用了。

代码语言:javascript复制
ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go/install)"
brew doctor

homebrew-cask

安装cask:

代码语言:javascript复制
brew tap phinze/homebrew-cask && brew install brew-cask 

安装mysql

brew install mysql MySQL开机启动:

代码语言:javascript复制
ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

安装完成之后开启MySQL安全机制:

代码语言:javascript复制
/usr/local/opt/mysql/bin/mysql_secure_installation

安装php

代码语言:javascript复制
brew update                        #更新brew可安装包,建议每次执行一下
brew search php55                  #搜索php5.5
brew tap josegonzalez/php          #安装扩展   
brew tap                           #查看安装的扩展列表
brew install php55                 #安装php5.5

brew update
brew tap homebrew/dupes
brew tap josegonzalez/homebrew-php

brew install php55 --with-fpm --with-gmp --with-imap --with-tidy --with-debug --with-mysql --with-libmysql

等待PHP编译完成,开始安装PHP常用扩展,扩展安装过程中brew会自动安装依赖包,例如php55-pdo-pgsql 会自动装上postgresql,这里我安装以下PHP扩展:

代码语言:javascript复制
 brew install php55-apcu
 php55-gearman
 php55-geoip
 php55-gmagick
 php55-imagick
 php55-intl
 php55-mcrypt
 php55-memcache
 php55-memcached
 php55-mongo
 php55-opcache
 php55-pdo-pgsql
 php55-phalcon
 php55-redis
 php55-sphinx
 php55-swoole
 php55-uuid
 php55-xdebug;

由于Mac自带了php和php-fpm,因此需要添加系统环境变量PATH来替代自带PHP版本。

代码语言:javascript复制
echo 'export PATH="$(brew --prefix php55)/bin:$PATH"' >> ~/.bash_profile  #for php
echo 'export PATH="$(brew --prefix php55)/sbin:$PATH"' >> ~/.bash_profile  #for php-fpm
echo 'export PATH="/usr/local/bin:/usr/local/sbib:$PATH"' >> ~/.bash_profile #for other brew install soft
source ~/.bash_profile

修改php-fpm配置文件,vim /usr/local/etc/php/5.5/php-fpm.conf,找到pid相关大概在25行,去掉注释 pid = run/php-fpm.pid, 那么php-fpm的pid文件就会自动产生在/usr/local/var/run/php-fpm.pid,下面要安装的Nginx pid文件也放在这里。

代码语言:javascript复制
#测试php-fpm配置
php-fpm -t
php-fpm -c /usr/local/etc/php/5.5/php.ini -y /usr/local/etc/php/5.5/php-fpm.conf -t

#启动php-fpm
php-fpm -D
php-fpm -c /usr/local/etc/php/5.5/php.ini -y /usr/local/etc/php/5.5/php-fpm.conf -D

#关闭php-fpm
kill -INT `cat /usr/local/var/run/php-fpm.pid`

#重启php-fpm
kill -USR2 `cat /usr/local/var/run/php-fpm.pid`

#也可以用上文提到的brew命令来重启php-fpm,不过他官方不推荐用这个命令了
brew services restart php55

#还可以用这个命令来启动php-fpm
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php55.plist</pre>
启动php-fpm之后,确保它正常运行监听9000端口:

<pre>lsof -Pni4 | grep LISTEN | grep php
php-fpm   30907 calvin    9u  IPv4 0xf11f9e8e8033a2a7      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   30917 calvin    0u  IPv4 0xf11f9e8e8033a2a7      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   30918 calvin    0u  IPv4 0xf11f9e8e8033a2a7      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   30919 calvin    0u  IPv4 0xf11f9e8e8033a2a7      0t0  TCP 127.0.0.1:9000 (LISTEN)
#正常情况,会看到上面这些进程

PHP-FPM开机启动:

代码语言:javascript复制
ln -sfv /usr/local/opt/php55/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php55.plist

安装phpmyadmin

代码语言:javascript复制
brew install phpmyadmin

nginx

代码语言:javascript复制
brew install nginx --with-http_geoip_module

Nginx启动关闭命令:

代码语言:javascript复制
#测试配置是否有语法错误
nginx -t

#打开 nginx
sudo nginx

#重新加载配置|重启|停止|退出 nginx
nginx -s reload|reopen|stop|quit

#也可以使用Mac的launchctl来启动|停止
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

Nginx开机启动

代码语言:javascript复制
ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

Nginx监听80端口需要root权限执行,因此:

代码语言:javascript复制
sudo chown root:wheel /usr/local/Cellar/nginx/1.8.0/bin/nginx
sudo chmod u s /usr/local/Cellar/nginx/1.8.0/bin/nginx

这里要注意你安装的nginx版本号

配置nginx.conf 创建需要用到的目录:

代码语言:javascript复制
mkdir -p /usr/local/var/logs/nginx
mkdir -p /usr/local/etc/nginx/sites-available
mkdir -p /usr/local/etc/nginx/sites-enabled
mkdir -p /usr/local/etc/nginx/conf.d
mkdir -p /usr/local/etc/nginx/ssl
sudo mkdir -p /var/www
sudo chown :staff /var/www
sudo chmod 775 /var/www

vim /usr/local/etc/nginx/nginx.conf 输入以下内容:

代码语言:javascript复制
worker_processes  1;

error_log   /usr/local/var/logs/nginx/error.log debug;


pid        /usr/local/var/run/nginx.pid;


events {
    worker_connections  256;
}


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  /usr/local/var/logs/access.log  main;

    sendfile        on;
    keepalive_timeout  65;
    port_in_redirect off;

    include /usr/local/etc/nginx/sites-enabled/*;
}

设置nginx php-fpm配置文件

代码语言:javascript复制
vim /usr/local/etc/nginx/conf.d/php-fpm
#proxy the php scripts to php-fpm
location ~ .php$ {
    try_files                   $uri = 404;
    fastcgi_pass                127.0.0.1:9000;
    fastcgi_index               index.php;
    fastcgi_intercept_errors    on;
    include /usr/local/etc/nginx/fastcgi.conf;
}

nginx虚拟主机准备工作

代码语言:javascript复制
#创建 info.php index.html 404.html 403.html文件到 /var/www 下面
vi /var/www/info.php
vi /var/www/index.html
vi /var/www/403.html
vi /var/www/404.html

创建默认虚拟主机default vim /usr/local/etc/nginx/sites-available/default输入:

代码语言:javascript复制
server {
    listen       80;
    server_name  localhost;
    root         /var/www/;

    access_log  /usr/local/var/logs/nginx/default.access.log  main;

    location / {
        index  index.html index.htm index.php;
        autoindex   on;
        include     /usr/local/etc/nginx/conf.d/php-fpm;
    }

    location = /info {
        allow   127.0.0.1;
        deny    all;
        rewrite (.*) /.info.php;
    }

    error_page  404     /404.html;
    error_page  403     /403.html;
}

创建ssl默认虚拟主机default-ssl vim /usr/local/etc/nginx/sites-available/default-ssl输入:

代码语言:javascript复制
server {
    listen       443;
    server_name  localhost;
    root       /var/www/;

    access_log  /usr/local/var/logs/nginx/default-ssl.access.log  main;

    ssl                  on;
    ssl_certificate      ssl/localhost.crt;
    ssl_certificate_key  ssl/localhost.key;

    ssl_session_timeout  5m;

    ssl_protocols  SSLv2 SSLv3 TLSv1;
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers   on;

    location / {
        include   /usr/local/etc/nginx/conf.d/php-fpm;
    }

    location = /info {
        allow   127.0.0.1;
        deny    all;
        rewrite (.*) /.info.php;
    }

    error_page  404     /404.html;
    error_page  403     /403.html;
}

创建phpmyadmin虚拟主机

vim /usr/local/etc/nginx/sites-available/phpmyadmin #输入以下配置

代码语言:javascript复制
server {
    listen       306;
    server_name  localhost;
    root    /usr/local/share/phpmyadmin;

    error_log   /usr/local/var/logs/nginx/phpmyadmin.error.log;
    access_log  /usr/local/var/logs/nginx/phpmyadmin.access.log main;

    ssl                  on;
    ssl_certificate      ssl/phpmyadmin.crt;
    ssl_certificate_key  ssl/phpmyadmin.key;

    ssl_session_timeout  5m;

    ssl_protocols  SSLv2 SSLv3 TLSv1;
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers   on;

    location / {
        index  index.html index.htm index.php;
        include   /usr/local/etc/nginx/conf.d/php-fpm;
    }
}

设置SSL

代码语言:javascript复制
mkdir -p /usr/local/etc/nginx/ssl
openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=US/ST=State/L=Town/O=Office/CN=localhost" -keyout /usr/local/etc/nginx/ssl/localhost.key -out /usr/local/etc/nginx/ssl/localhost.crt
openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=US/ST=State/L=Town/O=Office/CN=phpmyadmin" -keyout /usr/local/etc/nginx/ssl/phpmyadmin.key -out /usr/local/etc/nginx/ssl/phpmyadmin.crt

创建虚拟主机软连接,开启虚拟主机

代码语言:javascript复制
ln -sfv /usr/local/etc/nginx/sites-available/default /usr/local/etc/nginx/sites-enabled/default
ln -sfv /usr/local/etc/nginx/sites-available/default-ssl /usr/local/etc/nginx/sites-enabled/default-ssl
ln -sfv /usr/local/etc/nginx/sites-available/phpmyadmin /usr/local/etc/nginx/sites-enabled/phpmyadmin

启动|停止Nginx

代码语言:javascript复制
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

接下来你可以通过下面这些连接访问:

http://localhost/ -> index.html http://localhost/info -> info.php via phpinfo(); http://localhost/404 -> 404.html https://localhost/ -> index.html(SSL) https://localhost/info -> info.php via phpinfo();(SSL) https://localhost/404 -> 404.html(SSL) https://localhost:306 -> phpmyadmin(SSL)

设置快捷服务控制命令

为了后面管理方便,将命令 alias 下,vim ~/.bash_aliases 输入一下内容:

代码语言:javascript复制
alias nginx.start='launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist'
alias nginx.stop='launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist'
alias nginx.restart='nginx.stop && nginx.start'
alias php-fpm.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php55.plist"
alias php-fpm.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.php55.plist"
alias php-fpm.restart='php-fpm.stop && php-fpm.start'
alias mysql.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist"
alias mysql.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist"
alias mysql.restart='mysql.stop && mysql.start'
alias redis.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.redis.plist"
alias redis.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.redis.plist"
alias redis.restart='redis.stop && redis.start'
alias memcached.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist"
alias memcached.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist"
alias memcached.restart='memcached.stop && memcached.start'
代码语言:javascript复制
#让快捷命令生效
echo "[[ -f ~/.bash_aliases ]] && . ~/.bash_aliases" >> ~/.bash_profile     
source ~/.bash_profile
#创建站点目录到主目录,方便快捷访问
ln -sfv /var/www ~/htdocs

实际安装过程中,由于我的mysql是之前用官方包安装的,所以不需要用brew安装了。

0 人点赞