Using Brew to build PHP (LNMP/LAMP) Development environment _php techniques under Mac system

Source: Internet
Author: User
Tags brew install mongodb fpm install mongodb mkdir mongodb php cli php development environment phpmyadmin

It is easy to build a lamp development environment under Mac, with a XAMPP and mamp integrated environment. But the integrated environment is cumbersome for developers who often need custom configurations, and the Mac itself, with Apache and PHP, is easy to manually build with the help of Brew, which is highly controllable.


Brew for Mac, just like Apt-get for Ubuntu, the good helper to install software, not more convenient ...

Brew is installed in the following ways:

Copy Code code as follows:

Ruby-e "$ (curl-fssl"

Brew Common options

Copy Code code as follows:

Brew Install XXX
Brew Uninstall XXX
Brew List
Brew Update xxx

Apache | | Nginx


Apache's use of the Mac with the basic is enough, my system is 10.9, you can use the following command to control Apache

Copy Code code as follows:

sudo apachectl start
sudo apachectl restart
sudo apachectl stop

The only thing to change is the home directory, where the Mac defaults to a sites (site) directory under the Web, and the access path is

Copy Code code as follows:


This is not suitable for development, modify/etc/apache2/httpd.conf content

Copy Code code as follows:

DocumentRoot "/users/username/sites"
Options Indexes MultiViews
AllowOverride All
Order Allow,deny
Allow from all

So the Sites directory is the site root directory, the code is dropped


To use Nginx is also more convenient, first install

Copy Code code as follows:

Brew Install Nginx

The command to start shutdown Nginx is as follows (if you want to listen on port 80, you must run as an administrator)

Copy Code code as follows:

#打开 Nginx
sudo nginx
#重新加载配置 | reboot | stop | exit nginx
Nginx-s Reload|reopen|stop|quit

Configure Nginx

Copy Code code as follows:

mkdir CONF.D

modifying Nginx configuration Files

Copy Code code as follows:

Vim nginx.conf

The main modification location is the final include

Copy Code code as follows:

Worker_processes 1;

Error_log/usr/local/var/log/nginx/error.log warn;


Events {
Worker_connections 256;

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/log/nginx/access.log main;
Port_in_redirect off;
Sendfile on;
Keepalive_timeout 65;


Modify a custom file

Copy Code code as follows:


Add a listening port

Copy Code code as follows:

server {
Listen 80;
server_name localhost;

root/users/username/sites/; # This item is to be modified for you to store the path to the relevant page

Index index.php;
AutoIndex on;

#proxy the PHP scripts to PHP-FPM
Location ~ \.php$ {
Fastcgi_intercept_errors on;


The PHP site cannot be accessed at this time because the PHP-FPM is not yet open.

Although Mac 10.9 has its own php-fpm, because we use the latest php,php with PHP-FPM, we can use the PHP-FPM in PHP to guarantee a consistent version.

The commands here are executed after installing the next PHP

Copy Code code as follows:

sudo nginx
sudo php-fpm-d


PHP is installed by default under Mac, but it is not easy to control version, Brew can be installed with the latest version of Mac, or even multiple versions, I installed php5.5

Copy Code code as follows:

Brew Update
Brew Tap Homebrew/dupes
Brew Tap josegonzalez/homebrew-php
# Brew Install Php55--WITH-FPM #Nginx
Brew Install Php55 #Apache

Then modify the PHP CLI path and the PHP module used by Apache. Add the following in the. bashrc or ZSHRC.
Copy Code code as follows:

#export path= "$ (Brew--prefix josegonzalez/php/php55)/bin: $PATH"
Export path= "/usr/local/bin:/usr/local/sbin: $PATH"

The PHP version of the system default CLI is replaced with PHP just installed. And then under the/etc/apache2/httpd.conf, add
Copy Code code as follows:

LoadModule php5_module/usr/local/cellar/php55/5.5.8/libexec/apache2/

In this way, the PHP version used by Apache has also been modified.

Later will use MONGO and MySQL, so you can directly use the following command to install the PHP module, the other modules are similar

Copy Code code as follows:

Brew Install Php55-mysql
Brew Install Php55-mongo


Mac does not own MySQL, here needs to reinstall, the method is still very simple

Copy Code code as follows:

Brew Install MySQL
unset Tmpdir
mysql_install_db--verbose--user= ' WhoAmI '--basedir= "$ (brew--prefix MySQL)"--datadir=/usr/local/var/mysql--tmpdir =/tmp
sudo chown-r your_user/usr/local/var/mysql/

The first sentence is the installation, the following is to ensure normal use. And then the start command.

Copy Code code as follows:

Mysql.server start

The best way to set a password for MySQL is as follows
Copy Code code as follows:

mysqladmin-u root password ' xxx '

If you want to modify the MySQL configuration, create a my.cnf under/USR/LOCAL/ETC, such as adding log
Copy Code code as follows:

General_log_file =/usr/local/var/log/mysqld.log


MongoDB can be said to be the simplest one to execute directly

Copy Code code as follows:

Brew Install MongoDB

Startup method

Copy Code code as follows:



phpMyAdmin is almost the easiest Web application to manage MySQL, and every time I drop in.

1. To download the latest version of the official website
2. Extract to ~/sites/phpmyadmin under
3. Create a writable config directory under the phpMyAdmin directory
4. Open Http://localhost/phpmyadmin/setup, install a service, finally save (here only need to enter the account password is enough)
5. Move the generated under Config to the phpMyAdmin root directory
6. Delete Config

That's it, though it may be a little complicated, but it's a habit to come once.

Here is likely to encounter 2002 error, is not found mysql.sock problem, use the following method to solve

Copy Code code as follows:

sudo mkdir/var/mysql
sudo ln-s/tmp/mysql.sock/var/mysql/mysql.sock


Rockmongo is a very useful Web application for MongoDB, and it's easy to install

1. To download the latest version of the official website
2. Extract to ~/sites/rockmongo under
3. Run Http://localhost/rockmongo


This allows you to configure a PHP development environment under the MAC, enjoy it!

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: 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.