>  기사  >  백엔드 개발  >  Mac에서 PHP 환경 구축

Mac에서 PHP 환경 구축

小云云
小云云원래의
2018-03-27 13:28:587709검색

이 기사에서는 Mac에서 PHP 환경을 구축하는 방법을 주로 공유합니다. 최근 작업 환경이 Mac으로 전환되었으므로 OS X Yosemite(10.10.1)를 예로 들어 설치 과정을 기록해 보세요. Mac의 LNMP 환경을 처음부터 새로

시스템에 xcode가 설치되어 있는지 확인한 다음 한 줄 명령을 사용하여 종속성 관리 도구인 Homebrew를 설치하세요.

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

그런 다음

brew install FORMULA

를 사용하여 필요한 종속성을 설치할 수 있습니다.

brew(양조를 의미함)라는 이름은 매우 흥미롭습니다. 양조 과정에 사용되는 모든 재료/도구는 다음 개념에 해당합니다.

  • Formula(공식) 프로그램 패키지 정의. rb 파일

  • Keg(배럴) 패키지 설치 경로

  • 모든 패키지(배럴)의 Cellar(cellar) 루트 디렉터리

  • Tap(수도꼭지) 패키지 소스

  • Bottle(병) 컴파일 및 패키지 the package

최종 컴파일되고 설치된 프로그램은 양조 와인 한 통입니다

자세한 내용은 Homebrew의 공식 Cookbook을 참조하세요

따라서 Homebrew를 사용하는 일반적인 프로세스는 다음과 같습니다.

  1. 프로그램 소스 추가 (새 수도꼭지 추가) brew tap homebrew/phpbrew tap homebrew/php

  2. 更新程序源 brew update

  3. 安装程序包(按照配方酿酒) brew install git

  4. 查看配置 brew config 可以看到程序包默认安装在/usr/local/Cellar下 (酒桶放在地窖内)

安装PHP5.6(FPM方式)

首先加入Homebrew官方的几个软件源

brew tap homebrew/dupes
brew tap homebrew/versions
brew tap homebrew/php

PHP如果采用默认配置安装,会编译mod_php模块并只运行在Apache环境下,为了使用Nginx,这里需要编译php-fpm并且禁用apache,主要通过参数--without-fpm --without-apache来实现。完整的安装指令为

brew install php56 \
--build-from-source \
--without-snmp \
--without-apache \
--with-fpm \
--with-intl \
--with-homebrew-curl \
--with-homebrew-libxslt \
--with-homebrew-openssl \
--with-imap \
--with-mysql \
--with-tidy

由于OSX已经自带了PHP环境,因此需要修改系统路径,优先运行brew安装的版本,在~/.bashrc

프로그램 소스 업데이트 brew update

프로그램 패키지 설치(레시피에 따라 브루잉) brew install git

🎜🎜구성 보기 brew config 패키지가 기본적으로 /usr/local/Cellar 아래에 설치되어 있는 것을 확인할 수 있습니다( 와인통은 지하실에 놓여있습니다)🎜🎜

PHP5.6 설치(FPM 방식)

🎜먼저 여러 공식 Homebrew 소프트웨어 소스를 추가하세요🎜
export PATH="/usr/local/bin:/usr/local/sbin:$PATH"
🎜PHP가 기본 구성으로 설치된 경우 mod_php code> 모듈이 컴파일되고 Apache 환경에서만 실행됩니다. Nginx를 사용하려면 php-fpm을 컴파일하고 Apache를 비활성화해야 합니다. 이는 주로 <code>--without-fpm --without 매개 변수를 통해 수행됩니다. -아파치. 전체 설치 지침은 🎜
brew search php56
🎜입니다. OSX에는 이미 PHP 환경이 제공되므로 시스템 경로를 수정하고 Brew가 설치한 버전을 먼저 실행해야 합니다. 원하는 경우 ~/.bashrc에 🎜<pre style="font-family:'Courier New';margin:5px 8px;padding:5px;" class="brush:php;toolbar:false;">brew install php56-memcache php56-memcached php56-mongo  php56-phalcon php56-redis php56-xdebug --build-from-source</pre>🎜를 추가하세요. 새로운 PHP 확장을 설치하세요. 매번 PHP를 다시 컴파일하지 않고 직접 설치할 수 있습니다. 모든 확장은 🎜
php-fpm -D
killall php-fpm
🎜를 통해 볼 수 있습니다. Phalcon 프레임워크를 지원하는 데 필요한 확장은 다음과 같습니다: 🎜
ln -sfv /usr/local/opt/php56/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php56.plist
🎜PHP-FPM 로드 시작🎜🎜설치가 완료된 후 다음 명령을 통해 php-fpm을 시작하고 중지할 수 있습니다🎜
php-fpm -D
killall php-fpm

同时可以将php-fpm加入开机启动

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

安装Nginx

brew install nginx

安装完毕后可以通过

nginx
nginx -s quit

启动和关闭,同时也支持重载配置文件等操作

nginx -s reload|reopen|stop|quit

nginx安装后默认监听8080端口,可以访问http://localhost:8080查看状态。如果要想监听80端口需要root权限,运行

sudo chown root:wheel /usr/local/Cellar/nginx/1.6.2/bin/nginx
sudo chmod u+s /usr/local/Cellar/nginx/1.6.2/bin/nginx

并使用root权限启动

sudo nginx

开机启动

ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

Nginx + PHP-FPM配置

Nginx一般都会运行多个域名,因此这里参考了@fish的方法,按Ubuntu的文件夹结构来存放Nginx的配置文件

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

编辑Nginx全局配置

vim /usr/local/etc/nginx/nginx.conf
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" $host $request_time $upstream_response_time $scheme '        '$cookie_evalogin';

    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/*;
}

这样一来首先可以把一些可复用配置独立出来放在/usr/local/etc/nginx/conf.d下,比如fastcgi的设置就可以独立出来

vim /usr/local/etc/nginx/conf.d/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;
}

然后/usr/local/etc/nginx/sites-enabled目录下可以一个文件对应一个域名的配置,比如web服务器目录是/opt/htdocs

vim /usr/local/etc/nginx/sites-enabled/default
server {
    listen       80;
    server_name  localhost;
    root         /opt/htdocs/;

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

此时启动了php-fpm并且启动了Nginx后,就可以通过http://localhost来运行php程序了

安装MySQL

brew install mysql

可以通过

mysql.server start
mysql.server stop

来启动/停止,启动后默认应为空密码,可以通过mysqladmin设置一个密码

mysqladmin -uroot password "mypassword"

但是在操作的时候出现了空密码无法登入的情况,最终只能通过mysqld_safe来设置

sudo mysqld_safe --skip-grant-tables
mysql -u root
mysql> UPDATE mysql.user SET Password=PASSWORD('mypassword') WHERE User='root';
mysql> FLUSH PRIVILEGES;

最后将MySQL加入开机启动

cp /usr/local/Cellar/mysql/5.6.22/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/

Memcache

brew install memcached

启动/停止指令

memcached -d
killall memcached

加入开机启动

cp /usr/local/Cellar/memcached/1.4.20/homebrew.mxcl.memcached.plist ~/Library/LaunchAgents/

Redis

brew install redis

Redis默认配置文件不允许以Deamon方式运行,因此需要先修改配置文件

vim /usr/local/etc/redis.conf

将daemonize修改为yes,然后载入配置文件即可实现后台进程启动

redis-server /usr/local/etc/redis.conf

加入开机启动

cp /usr/local/Cellar/redis/2.8.19/homebrew.mxcl.redis.plist ~/Library/LaunchAgents/

设置别名

最后可以对所有服务的启动停止设置别名方便操作

vim ~/.bash_profile

加入

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'

安装其他项目支持

brew install composer node

安装Oh My Zsh

brew install zsh-completions
chsh -s /usr/local/bin/zsh
vim ~/.zshenv

加入内容

export PATH=/usr/local/bin:$PATH

然后

vim ~/.zshrc

加入内容

fpath=(/usr/local/share/zsh-completions $fpath)
autoload -Uz compinit
compinit -u

最后运行

rm -f ~/.zcompdump; compinit

查看正在使用的shell

dscl localhost -read Local/Default/Users/$USER UserShell

安装Oh My Zsh

wget https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | sh

               

위 내용은 Mac에서 PHP 환경 구축의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.