Build a PHP (LNMP/LAMP) development environment using brew in Mac system

Source: Internet
Author: User
Tags brew install mongodb install mongodb php development environment
This article describes how to use brew to build a PHP (LNMPLAMP) development environment in Mac systems. This article describes how to use Brew to manually build a PHP development environment, including Apache, Nginx, PHP, MySQL, MongoDB, PHPMyAdmin, and other configurations. For more information about how to set up a lamp development environment on Mac, see xampp and mamp. However, the integration environment is very troublesome for developers who often need to customize some configurations. In addition, the Mac itself comes with apache and php. with brew's help, it is very easy to set up manually and highly controllable.


Brew for mac, like apt-get for ubuntu, a good helper for installing software, cannot be more convenient...

The brew installation method is as follows:

The code is as follows:

Ruby-e "$ (curl-fsSL )"

Brew common options

The code is as follows:

Brew install xxx
Brew uninstall xxx
Brew list
Brew update xxx

Apache | Nginx


For Apache, mac is enough. my system is 10.9. you can use the following command to control Apache.

The code is as follows:

Sudo apachectl start
Sudo apachectl restart
Sudo apachectl stop

The only change is the main directory. by default, mac has a directory named sites (site) under home. The access path is

The code is as follows:

Http: // localhost /~ User_name

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

The code is as follows:

DocumentRoot "/Users/username/Sites"

Options Indexes MultiViews
AllowOverride All
Order allow, deny
Allow from all

In this way, the sites directory is the root directory of the website, and all the code is lost.


Nginx is easy to use. first, install

The code is as follows:

Brew install nginx

Run the following command to disable Nginx: (to listen to port 80, run it as an administrator)

The code is as follows:

# Enable nginx
Sudo nginx
# Reload configuration | restart | stop | exit nginx
Nginx-s reload | reopen | stop | quit
# Test whether the configuration has syntax errors

Configure Nginx

The code is as follows:

Mkdir conf. d

Modify Nginx configuration file

The code is as follows:

Vim nginx. conf

The main modification location is the final include

The code is as follows:

Worker_processes 1;

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

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

Include/usr/local/etc/nginx/conf. d/*. conf;

Modify a custom file

The code is as follows:

Vim./conf. d/default. conf

Add a listening port

The code is as follows:

Server {
Listen 80;
Server_name localhost;

Root/Users/username/Sites/; # Modify the path for storing the relevant webpage.

Location /{
Index. php;
Autoindex on;

# Proxy the php scripts to php-fpm
Location ~ \. Php $ {
Include/usr/local/etc/nginx/fastcgi. conf;
Fastcgi_intercept_errors on;
Fastcgi_pass 9000;


At this time, you cannot access the php site because php-fpm has not been enabled.

Although mac 10.9 comes with php-fpm, because we use the latest PHP and PHP comes with php-fpm, using PHP-fpm in php can ensure version consistency.

The command here is executed after the next php step is installed.

The code is as follows:

Sudo nginx
Sudo php-fpm-D


PHP is installed on mac by default, but it is difficult to control the version. brew can be used to install the latest version on mac, or even multiple versions. I installed php5.5

The code is as follows:

Brew update
Brew tap homebrew/dupes
Brew tap maid/homebrew-php
# Brew install php55 -- with-fpm # Nginx
Brew install php55 # Apache

Then modify the cli path of php and the php module used by apache. Content in. bashrc or. zshrc

The code is as follows:

# Export PATH = "$ (brew -- prefix maid/php/php55)/bin: $ PATH"
Export PATH = "/usr/local/bin:/usr/local/sbin: $ PATH"

The php version just installed replaces the php version of the default cli of the system. Then add

The code is as follows:

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

In this way, the php version used by apache is also modified.

Mongo and mysql will be used later, so you can directly use the following command to install the php module. other modules are similar.

The code is as follows:

Brew install php55-mysql
Brew install php55-mongo


The mac does not contain mysql. you need to reinstall it here. the method is still very simple.

The code is as follows:

Brew install mysql
Mysql_install_db -- verbose -- user = 'whoam' -- basedir = "$ (brew -- prefix mysql)" -- datadir =/usr/local/var/mysql -- tmpdir =/tmp
Sudo chown-R your_user/usr/local/var/mysql/

The first sentence is installation, followed by ensuring normal use. Then start the command

The code is as follows:

Mysql. server start

Set a password for mysql as follows:

The code is as follows:

Mysqladmin-u root password 'XXX'

To modify the mysql configuration, create a my. cnf file under/usr/local/etc, for example, add log

The code is as follows:

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


MongoDB is the simplest and can be directly executed.

The code is as follows:

Brew install mongodb

Start method

The code is as follows:

Mongod -- fork


Phpmyadmin is almost the easiest web application to manage mysql.

1. go to the official website to download the latest version.
2. decompress the package ~ /Sites/phpmyadmin
3. create a writable config directory under The phpmyadmin Directory
4. open http: // localhost/phpmyadmin/setup, install a service, and save it. (you only need to enter the account password here)
5. move the config. inc. php generated under config to The phpmyadmin root directory.
6. delete config

In this way, it may be a little complicated, but you will get used to it once.

The error 2002 is likely to occur, that is, mysql. sock cannot be found. use the following method to solve the problem:

The code is as follows:

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


RockMongo is a good web application for MongoDB and is easy to install.

1. go to the official website to download the latest version.
2. decompress the package ~ /Sites/rockmongo
3. run http: // localhost/rockmongo.


In this way, a php development environment is configured in mac, enjoy it!

Related Article

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.