reko_3 发表于 2017-12-23 13:32:19

php+mysql+nginx+phpmyadmin环境搭建

  英文原文:http://blog.frd.mn/install-nginx-php-fpm-mysql-and-phpmyadmin-on-os-x-mavericks-using-homebrew/
  参照博文作者: http://blog.liuweifeng.net/author/liuweifeng/
  首先,为了不出现各种神奇的错误,请安装 XCode。Yosemite 发布后,XCode 的最新版本是 6.1.0(6A1052d)。然后,不可忽略的一步,请在终端里执行
  

xcode-select --install  

  来安装最新的 XCode 命令行工具(Xcode Command Line Tools)。

安装 Homebrew
  如果没有安装,请移步官网 →_→ Homebrew 官方网站
  如果你已经安装了 Homebrew,请在终端执行brew doctor检查各种可能的冲突和问题。然后执行 brew update && brew upgrade 升级 Homebrew 自身和自带的 formulas

安装 PHP-FPM
  添加brew的php扩展库:
  

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

  安装开发包:
  

brew install wget watch tmux cmake openssl imagemagick graphicsmagick gearman geoip readline autoconf multitail source-highlight autojump zsh-completions sshfs  

  然后开始安装:
  

brew search php   //choose php version  

  

brew install php71 --with-fpm --with-gmp --with-imap --with-tidy —with-debug —with-mysql —with-libmysql  安装php一下扩展
  

brew install php71-apcu\  php71
-gearman\  php71
-geoip\  php71
-gmagick\  php71
-imagick\  php71
-mcrypt\  php71
-memcache\  php71
-memcached\  php71
-mongo\  php71
-apache\  php71
-pdo-pgsql\  php71
-redis\  php71
-sphinx\  php71
-swoole\  php71
-uuid\  php71
-xdebug;  

  更改为新安装的php,打开~/.bash_profile
  

export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"//可以采取下面的方式  

  因为我们打算安装 Nginx,Apache 就不需要了(貌似 OS X 自带了 Apache?)。
  如果你想在命令行里使用 PHP 的话,你需要在你 shell 的 Profile 里设置下 $PATH:
  

# If you use Bash  
echo 'export PATH="$(brew --prefix homebrew/php/php71)/bin:$PATH"' >> ~/.bash_profile && . ~/.bash_profile
  
# If you use ZSH
  
echo 'export PATH="$(brew --prefix homebrew/php/php71)/bin:$PATH"' >> ~/.zshrc && . ~/.zshrc
  

  安装composer
  

brew install composer  composer --version
  

  设置开机启动:
  

-p ~/Library/LaunchAgents  
cp
/usr/local/Cellar/php71/7.1.5_17/homebrew.mxcl.php71.plist ~/Library/LaunchAgents/  

  启动 PHP-FPM:
  

launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php71.plist   

  看下 PHP-FPM 是不是在监听 9000 端口:
  

lsof -Pni4 | grep LISTEN | grep php  

  输出应该是这个样子的:
  

php-fpm   78168 liuweifeng    7uIPv4 0x9c66922b37bcbcb7      0t0TCP 127.0.0.1:9000 (LISTEN)  
php
-fpm   78170 liuweifeng    0uIPv4 0x9c66922b37bcbcb7      0t0TCP 127.0.0.1:9000 (LISTEN)  
php
-fpm   92277 liuweifeng    0uIPv4 0x9c66922b37bcbcb7      0t0TCP 127.0.0.1:9000 (LISTEN)  
php
-fpm   94154 liuweifeng    0uIPv4 0x9c66922b37bcbcb7      0t0TCP 127.0.0.1:9000 (LISTEN)  


安装 MySQL
  

brew install  

  设置开机启动:
  

ln -sfv /usr/local/opt//*.plist ~/Library/LaunchAgents   


MySQL 安全设置
  

mysql_secure_installation  

  可以设置 root 的密码、移除匿名用户、禁止 root 远程登录,等等。

安装 phpMyAdmin
  首先需要安装 autoconf
  

brew install autoconf  

  

  设置 $PHP_AUTOCONF:
  

# If you use Bash  
echo 'PHP_AUTOCONF="'$(which autoconf)'"' >> ~/.bash_profile && . ~/.bash_profile
  
# If you use ZSH
  
echo 'PHP_AUTOCONF="'$(which autoconf)'"' >> ~/.zshrc && . ~/.zshrc
  

  

  开始安装 phpMyAdmin :
  

brew install phpmyadmin  

  


安装 Nginx
  

brew install nginx  

  


设置开机自动启动
  由于我们需要启动 80 端口,所以需要使用 sudo 来运行 nginx
  

sudo cp -v /usr/local/opt/nginx/*.plist /Library/LaunchDaemons/  
sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
  


启动 Nginx
  

sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist  

  


关闭 Nginx
  

sudo launchctl unload /Library/LaunchDaemons/homebrew.mxcl.nginx.plist  


更多配置

nginx.conf
  创建配置文件中需要用到的目录
  

mkdir -p /usr/local/etc/nginx/logs  
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
  

  

  移除默认的 nginx.conf(其实在 /usr/local/etc/nginx/nginx.conf.default 还有个备份,你可以看一眼,如果你想看的话。。。),然后使用 curl从 Github 上下载我的自定义版本:
  

rm /usr/local/etc/nginx/nginx.conf  
curl
-L https://gist.github.com/frdmn/7853158/raw/nginx.conf -o /usr/local/etc/nginx/nginx.conf  

  这是一个尽可能简单和轻量级的配置文件。

加载 PHP-FPM
  从 Github 下载我的自定义配置文件:
  

curl -L https://gist.github.com/frdmn/7853158/raw/php-fpm -o /usr/local/etc/nginx/conf.d/php-fpm   


创建默认的虚拟主机
  

curl -L https://gist.github.com/frdmn/7853158/raw/sites-available_default -o /usr/local/etc/nginx/sites-available/default  
curl -L https://gist.github.com/frdmn/7853158/raw/sites-available_default-ssl -o /usr/local/etc/nginx/sites-available/default-ssl
  
curl -L https://gist.github.com/frdmn/7853158/raw/sites-available_phpmyadmin -o /usr/local/etc/nginx/sites-available/phpmyadmin
  

  使用 Git 从 Github 上克隆我的实例虚拟主机配置(包括默认的 404、403页面,和一个 phpinfo() rewrite)。
  

git clone http://git.frd.mn/frdmn/nginx-virtual-host.git /var/www  
rm -rf /var/www/.git
  

  克隆完记得把 .git 目录删除掉,这样就不会被 Git 记录了。

  译者注:其实吧,我赶脚这个 404 和 403 挺丑的。。。


设置 SSL
  创建需要的目录
  

-p /usr/local/etc/nginx/ssl  

  创建 4096bit RSA keys 和自签名证书:
  

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. -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. -out /usr/local/etc/nginx/ssl/phpmyadmin.crt  


启用虚拟主机
  创建软链接:
  

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:
  

sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist   


最终测试
  基本上完事了,点击下面的链接来感受下吧:


[*]http://localhost → "Nginx works" page
[*]http://localhost/info → phpinfo()
[*]http://localhost/nope → " Not Found" page
[*]https://localhost:443 → "Nginx works" page (SSL)
[*]https://localhost:443/info → phpinfo() (SSL)
[*]https://localhost:443/nope → "Not Found" page (SSL)
[*]https://localhost:306 → phpMyAdmin (SSL)
服务配置
  以后你肯定会关闭或者重启上面设置的某个服务的,所以你可能需要设置一些别名。(需要修改bash_aliases中的php版本)
  

# If you use Bash  
curl -L https://gist.github.com/frdmn/7853158/raw/bash_aliases -o /tmp/.bash_aliases
  
cat /tmp/.bash_aliases >> ~/.bash_aliases
  
echo "source ~/.bash_aliases" >> ~/.bash_profile
  
# If you use ZSH
  
curl -L https://gist.github.com/frdmn/7853158/raw/bash_aliases -o /tmp/.zshrc
  
cat /tmp/.bash_aliases >> ~/.zshrc
  
echo "source ~/.bash_aliases" >> ~/.zshrc
  

  

  再打开一个终端窗口,来使上面的别名生效:
  

source ~/.bash_profile  

# or  
source ~/.zshrc
  

  现在你可以使用别名而不是 launchctl 和那一长串参数了~

Nginx
  你可以这样来启动、关闭、重启 Nginx:
  

nginx.start  
nginx
.stop  
nginx
.restart  

  快速 tail 访问日志和错误日志:
  

nginx.logs.access  
nginx
.logs.default.access  
nginx
.logs.phpmyadmin.access  
nginx
.logs.default-ssl.access  
nginx
.logs.error  
nginx
.logs.phpmyadmin.error  

  检查配置:
  

sudo nginx -t   


PHP-FPM
  启动、关闭、重启 PHP-FPM:
  

php-fpm.start  
php
-fpm.stop  
php
-fpm.restart   

  检查配置:
  

php-fpm -t  


MySQL
  启动、关闭、重启 MySQL:
  

.start  

.stop  

.restart  
页: [1]
查看完整版本: php+mysql+nginx+phpmyadmin环境搭建