Mac安装配置PHP/Mysql/Nginx/SSL

by Web全栈工程师 on 2016 年 05 月 01 日

安装版本号:

MAC OS 10.11
Mysql:5.7.9
Nginx:1.8.0
PHP:5.6.16

Homebrew安装

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/    Homebrew/install/master/install)"

安装php-fpm

//因为已经安装homebrew 所以可以直接使用homebrew安装php-fpm
brew tap homebrew/dupes
brew tap homebrew/php
brew install --without-apache --with-fpm --with-mysql php56

安装完成 现在我们将php-fpm 添加入环境变量中 方便我们通过终端直接进行启动

echo 'export PATH="/usr/local/sbin:$PATH"' >> ~/.bash_profile
#If you use bsh
echo 'export PATH="/usr/local/sbin:$PATH"' >> ~/.zshrc
#If you use ZSH

创建文件夹 并启动服务

mkdir -p ~/Library/LaunchAgents
ln -sfv /usr/local/opt/php56/homebrew.mxcl.php56.plist ~/   Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/   homebrew.mxcl.php56.plist

如果没有报出什么bug的话 在终端中键入

lsof -Pni4 | grep LISTEN | grep php

应该会有下图的显示

php-fpm   44147  magentonotes    6u  IPv4 0x1fbca9574e04090b      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   44148  magentonotes    0u  IPv4 0x1fbca9574e04090b      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   44149  magentonotes    0u  IPv4 0x1fbca9574e04090b      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   44150  magentonotes    0u  IPv4 0x1fbca9574e04090b      0t0  TCP 127.0.0.1:9000 (LISTEN)

安装mysql

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

添加mysql到环境变量中

launchctl load ~/Library/LaunchAgents/  homebrew.mxcl.mysql.plist

进入mysql的初始化环节 去确认一些mysql的缺省选项

mysql_secure_installation
Enter current password for root (enter for none)
##回车 我们并不需要
 
> Change the root password? [Y/n]
如不愿意使用root密码缺省mysql的password 输入n
并键入自己想使用的password
 
> Remove anonymous users? [Y/n]
Yes. 匿名用户我们并不需要
 
> Disallow root login remotely? [Y/n]
Yes. 我们仅需要127.0.1 
 
> Remove test database and access to it? [Y/n]
Yes. 无需保留冗余的测试文件
 
> Reload privilege tables now? [Y/n]
Yes.重新加载数据库

测试mysql

mysql -uroot -p

输入root密码

Type \'help;\' or \'h\' for help. Type \'c\' to clear the     current input statement.
 
mysql>
#退出mysql
输入 \q

安装phpmyadmin

brew install autoconf

设置$PHP_AUTOCONF 环境变量

#bash环境
echo 'PHP_AUTOCONF="'$(which autoconf)'"' >> ~/.bash_profile && . ~/.bash_profile
#zsh环境
echo 'PHP_AUTOCONF="'$(which autoconf)'"' >> ~/.zshrc && . ~/.zshrc

安装phpmyadmin

brew install phpmyadmin

安装Nginx

brew install nginx

必须确保80端口是开启的,因为nginx是基于80端口的

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

默认的配置设置是将监听8080端口而非http默认的80端口

curl -IL http://127.0.0.1:8080

停止nginx

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

配置Nginx

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配置文件

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

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 clone demo

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

安装ssl

创建文件夹来存放ssl的证书和私钥

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

生成4096比特 的RSA秘钥

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

使用虚拟主机

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

打开下面的这些网站,来确认自己的nginx是否正确安装

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)

设置服务的别名

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

更新变量 让设置生效

source ~/.bash_profile
//or
source ~/.zshrc`
nginx.start
nginx.stop
nginx.restart
#错误或访问日志
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

参考资料:
https://blog.frd.mn/install-nginx-php-fpm-mysql-and-phpmyadmin-on-os-x-mavericks-using-homebrew/

Comments on this entry are closed.

Previous post:

Next post: