Build PHP Environment under Mac

Source: Internet
Author: User
Tags root access install redis

This article is mainly to share with you mac build PHP environment, the recent work environment switch to Mac, so take OS X Yosemite (10.10.1) as an example, to record the installation of the Mac LNMP Environment from scratch

Make sure that the system has Xcode installed, and then install the Dependency management tool homebrew using a single line of commands.

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

After that, you can use

Brew Install FORMULA

To install the required dependencies.

The naming of brew (meaning wine) is interesting and all uses the materials/utensils used in the brewing process, and the nouns correspond to the following concepts:

    • Formula (recipe) package definition, essentially a RB file

    • Installation path of the KEG (bucket) package

    • Cellar (Cellar) root directory for all packages (buckets)

    • The source of the tap (TAP) package

    • Bottle (bottle) compile packaged packages

The final compilation and installation of the program is a barrel of brewing good wine

For more detailed information refer to the official cookbook of Homebrew

So the common process for using homebrew is:

    1. Add a program source (add a tap)brew tap homebrew/php

    2. Update program Sourcebrew update

    3. Install package (according to recipe brewing)brew install git

    4. View the configuration brew config to see that the package is installed by default /usr/local/Cellar (wine barrels are placed in the cellar)

Mounting PHP5.6 (FPM mode)

First of all, join the official homebrew of several software sources

Brew tap Homebrew/dupesbrew tap homebrew/versionsbrew tap homebrew/php

PHP if installed by default configuration, will compile the mod_php module and only run in the Apache environment, in order to use Nginx, here need to compile php-fpm and disable Apache, mainly through the parameters --without-fpm --without-apache to implement. The Complete installation instructions are

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

Since OSX has its own PHP environment, it is necessary to modify the system path and first run the brew installation version, ~/.bashrc adding:

Export path= "/usr/local/bin:/usr/local/sbin: $PATH"

If you want to install a new PHP extension, you can install it directly without recompiling PHP every time, all extensions can be

Brew Search Php56

See, here is the extension I need to support the Phalcon framework:

Brew Install Php56-memcache php56-memcached php56-mongo  php56-phalcon php56-redis php56-xdebug-- Build-from-source

Load and start of PHP-FPM

After installation, you can start and stop php-fpm with the following command

Php-fpm-dkillall PHP-FPM

At the same time, PHP-FPM can be added to boot

Ln-sfv/usr/local/opt/php56/*.plist ~/library/launchagentslaunchctl Load ~/library/launchagents/ Homebrew.mxcl.php56.plist

Installing Nginx

Brew Install Nginx

After the installation is complete, you can

Nginxnginx-s quit

Startup and shutdown, and also support for operations such as overloading configuration files

Nginx-s Reload|reopen|stop|quit

Nginx installs the default listener 8080 port, can access the http://localhost:8080 viewing state. If you want to listen on port 80 requires root access, run

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

and start with root privileges

sudo nginx

Boot up

Ln-sfv/usr/local/opt/nginx/*.plist ~/library/launchagentslaunchctl Load ~/library/launchagents/ Homebrew.mxcl.nginx.plist

Nginx + PHP-FPM Configuration

Nginx typically runs multiple domain names, so here is a reference to the @fish method, according to the Ubuntu folder structure to store Nginx configuration files

Mkdir-p/usr/local/var/logs/nginxmkdir-p/usr/local/etc/nginx/sites-availablemkdir-p/usr/local/etc/nginx/ Sites-enabledmkdir-p/usr/local/etc/nginx/conf.dmkdir-p/usr/local/etc/nginx/ssl

Edit Nginx Global Configuration

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

In this way, you can first put some reusable configuration independent /usr/local/etc/nginx/conf.d , such as the fastcgi settings can be independent

vim/usr/local/etc/nginx/conf.d/php-fpm

Content is

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

Then the /usr/local/etc/nginx/sites-enabled directory can be a file corresponding to a domain name configuration, such as the Web server directory is/opt/htdocs

Vim/usr/local/etc/nginx/sites-enabled/default
server {    listen       ;    server_name  localhost;    Root         /opt/htdocs/;    Location/{        index  index.html index.htm index.php;        Include     /usr/local/etc/nginx/conf.d/php-fpm;    }}

When the PHP-FPM is started and nginx is activated, you can http://localhost run the PHP program.

Install MySQL

Brew Install MySQL

can be done by

Mysql.server Startmysql.server Stop

To start/stop, the default should be a blank password after startup, you can set a password via mysqladmin

Mysqladmin-uroot password "MyPassword"

However, when the operation of the empty password can not be logged in the situation, and ultimately only through the Mysqld_safe to set

sudo mysqld_safe--skip-grant-tablesmysql-u rootmysql> UPDATE mysql.user SET password=password (' MyPassword ') WHERE user= ' root ';mysql> FLUSH privileges;

Finally, MySQL is added to boot.

Cp/usr/local/cellar/mysql/5.6.22/homebrew.mxcl.mysql.plist ~/library/launchagents/

Memcache

Brew Install memcached

Start/Stop command

Memcached-dkillall memcached

Join boot Start

Cp/usr/local/cellar/memcached/1.4.20/homebrew.mxcl.memcached.plist ~/library/launchagents/

Redis

Brew Install Redis

The Redis default profile does not allow Deamon to run, so you need to modify the configuration file first

Vim/usr/local/etc/redis.conf

Modify the Daemonize to Yes and then load the configuration file to enable background process startup

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

Join boot Start

Cp/usr/local/cellar/redis/2.8.19/homebrew.mxcl.redis.plist ~/library/launchagents/

Setting aliases

Finally, you can set the alias for all services to start and stop. Easy operation

Vim ~/.bash_profile

Join

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 '

Install additional project support

Brew Install composer node

Install Oh My Zsh

Brew Install Zsh-completionschsh-s/usr/local/bin/zshvim ~/.zshenv

Join content

Export Path=/usr/local/bin: $PATH

And then

Vim ~/.ZSHRC

Join content

Fpath= (/usr/local/share/zsh-completions $fpath) Autoload-uz compinitcompinit-u

Last Run

Rm-f ~/.zcompdump; Compinit

View the shell being used

DSCL localhost-read local/default/users/$USER Usershell

Install Oh My Zsh

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.