mac下配置php、nginx
PHP升级
解除原php
brew unlink php70
删除原配置
cd /usr/bin/ && sudo rm -rf php php-config phpdoc phpize
cd /usr/include && sudo rm -rf php
cd /usr/lib && sudo rm -rf php
cd /usr/sbin && sudo rm -rf php-fpm
cd /usr/share && sudo rm -rf php
cd /usr/share/man/man1 && sudo rm -rf php-config.1 php.1 phpize.1
cd /usr/share/man/man8 && sudo rm -rf php-fpm.8
cd /usr/bin/ && sudo rm -rf php php-config phpdoc phpize
安装php7.1
brew install homebrew/php/php71
brew install homebrew/php/php71-mcrypt
安装完了之后它会自带PHP-FPM
启动PHP-FPM
加一个symlink先
ln -s /usr/local/opt/php71/sbin/php-fpm /usr/local/bin/php-fpm
还需要设置开机启动
ln -sfv /usr/local/opt/php71/*.plist ~/Library/LaunchAgents
启动
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php71.plist
停止
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.php71.plist
强杀
sudo pkill -INT -o php-fpm
确保它正常运行监听9000端口
sudo lsof -Pni4 | grep LISTEN | grep php
或者通过端口查看
sudo lsof -i tcp:9000
安装nginx
卸载
brew remove nginx
清除配置
sudo rm -rf /usr/local/etc/nginx/
安装
brew install nginx --with-http_geoip_module
开机启动
ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
启动
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
停止
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
监听80端口需要root权限执行,因此:
sudo chown root:wheel /usr/local/Cellar/nginx/1.12.0_1/bin/nginx
sudo chmod u+s /usr/local/Cellar/nginx/1.12.0_1/bin/nginx
配置
配置nginx.conf
创建需要用到的目录:
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 /jacklin
sudo chown :staff /jacklin
sudo chmod 777 /jacklin
这里的文件夹给777,方便平时开发
vim /usr/local/etc/nginx/nginx.conf 输入以下内容:
user root wheel; #默认的是nobody会导致403
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配置文件
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;
}
创建默认虚拟主机default
vim /usr/local/etc/nginx/sites-available/default输入:
server {
listen 80;
server_name jacklin.test.com admin.test.com;
root /jacklin;
access_log /usr/local/var/logs/nginx/default.access.log main;
index index.php index.html index.htm;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$query_string;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
重要
为了方便开发,将网站代码所在目录修改为 777权限
安装MySQL
brew install mysql
brew link mysql
brew unlink mysql && brew link mysql(如果提示这样的命令你就输入以下这个命令)
brew info mysql
brew services start mysql
查看一下MySQL运行情况
ps aux | grep mysql
测试连接MySQL
mysql -uroot -p
设置快捷服务控制命令
为了后面管理方便,将命令 alias 下,vim ~/.bash_aliases 输入一下内容:
alias nginx.start='launchctl load -w /usr/local/opt/nginx/homebrew.mxcl.nginx.plist'
alias nginx.stop='launchctl unload -w /usr/local/opt/nginx/homebrew.mxcl.nginx.plist'
alias nginx.restart='nginx.stop && nginx.start'
alias php-fpm.start="launchctl load -w /usr/local/opt/php71/homebrew.mxcl.php71.plist"
alias php-fpm.stop="launchctl unload -w /usr/local/opt/php71/homebrew.mxcl.php71.plist"
alias php-fpm.restart='php-fpm.stop && php-fpm.start'
alias mysql.start="launchctl load -w /usr/local/opt/mysql/homebrew.mxcl.mysql.plist"
alias mysql.stop="launchctl unload -w /usr/local/opt/mysql/homebrew.mxcl.mysql.plist"
alias mysql.restart='mysql.stop && mysql.start'
alias redis.start="launchctl load -w /usr/local/opt/redis/homebrew.mxcl.redis.plist"
alias redis.stop="launchctl unload -w /usr/local/opt/redis/homebrew.mxcl.redis.plist"
alias redis.restart='redis.stop && redis.start'
让快捷命令生效
echo "[[ -f ~/.bash_aliases ]] && . ~/.bash_aliases" >> ~/.bash_profile
source ~/.bash_profile
注意:考虑到部分服务不用开机启动,所以这里启动的路径都是程序目录。如果需要开机启动,将plist文件软连接到~/Library/LaunchAgents下。
比如:ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
创建站点目录到主目录,方便快捷访问
ln -sfv /var/www ~/htdocs