Install and configure nginx, mysql, and php-fpm through homebrew

Source: Internet
Author: User
Tags phpinfo fpms

This article introduces how to install and configure nginx, mysql, and php-fpm instances through homebrew.

1.1. Environment selection:
Re-configure php on mac. Originally, the mac was equipped with apach, php, and pgsql. If the figure is simple, you can use it directly, however, before installation, I carefully thought about several issues:

Apache or nginx: I know a little about both of them. I have a lot of access to nginx, and my servers use nginx. No one can call it nginx with a high load. In order to better access the nginx environment, I decided to unify the environment for future use, which is the key of this time;
Install php: Mac OS comes with php, which seems to be 5.3.8. The version is lagging behind, but this is not in the way. In the past, I used php-osx by Liip to manage php, so you don't have to worry about version issues. However, php-osx by Liip does not seem to have any fmp-related configuration, so it is relatively troublesome for me to use it with nginx, so I can only choose other solutions.
Installing mysql: there is no doubt about this. At present, I certainly will not use pgsql. After all, I have never been touched and need to have learning costs, so I removed all pgsql in this record.
1.2. How to install:
Since I deny the environment that comes with the system, I have to install it in other ways, such as manual compilation. For manual compilation, you may encounter the following problems:

Compilation is troublesome. php depends on so many programs. It is too tired to install them one by one!
What if it is updated later? Disaster !!
Fortunately, there is a third-party hypervisor on mac: homebrew, and I think he may be superior to apt-get and yum in some aspects in linux. If you use homebrew to install and manage the runtime environment, you can solve at least a few of my problems:

Installation problems. I don't need to install the dependency program.
For upgrade problems, only brew update is required to upgrade all programs once.
Version problems: homebrew versions are updated in a timely manner. For apt-get and yum, this year is basically a stable version a few years ago.
OK. The following installation is based on homebrew. If you are not familiar with it or have not installed homebrew, you can refer to this article: Introduction and installation of Homebrew, a tool for Mac developers.

Ii. Install nginx
2.1 installation:
Use brew to install nginx with one click:

1 brew install nginx

If you need to install other nginx versions, you can "brew edit nginx" to open and modify the nginx installation information package formula, which will be opened by vi by default, modify the nginx version at the beginning of the file.

2.2 Configuration
After brew is executed, nginx is installed. You can use the following command to operate nginx:

# Enable nginx
Sudo nginx

# Reload | restart | stop | exit nginx
Nginx-s reload | reopen | stop | quit

After nginx is enabled, the default access port is 8080. To change to the commonly used port 80, modify "/usr/local/etc/nginx. the value of the listener port under conf.

The default file access directory (root) is "/usr/local/Cellar/nginx/1.4.2/html" (here 1.4.2 is the installed nginx version, the folder name is based on the installed nginx version ).

2.3 set nginx to start running at startup:
Mkdir-p ~ /Library/LaunchAgents/

Cp/usr/local/Cellar/nginx/1.4.2/homebrew. mxcl. nginx. plist ~ /Library/LaunchAgents/
Launchctl load-w ~ /Library/LaunchAgents/homebrew. mxcl. nginx. plist

However, after a try, it is not a superuser login, but a normal user login, and the listening port is below 1024 (for example, the default port 8080 is changed to port 80 ), nginx cannot be started at startup. Therefore, to start nginx at startup, You need to grant it administrator privileges:

12 sudo chown root: wheel/usr/local/Cellar/nginx/1.4.2/bin/nginxsudo chmod u + s/usr/local/Cellar/nginx/1.4.2/bin/nginx

Iii. Install mysql
3.1 installation:
1 brew install mysql

The homebrew installation version is also the latest stable version. The installation process is relatively simple, but Initialization is much more troublesome. I have failed many times here.

3.2 Configuration:
3.2.1 Initialization
Some configuration databases for initial installation of mysql (such as information_schema and mysql)

Sudo mysql_install_db
-- Verbose -- user = 'whoam'
-- Basedir = "$ (brew -- prefix mysql )"
-- Datadir =/usr/local/var/mysql
-- Tmpdir =/tmp
-- Explicit_defaults_for_timestamp

I have encountered two problems, but the process has not been recorded. Here we will provide a simple solution:

We recommend that you add "-explicit_defaults_for_timestamp ";
Several errors may occur during the installation process, indicating that several db files do not exist;
After searching for the directory on the Internet, we recommend that you modify the permission for the/usr/local/var/mysql directory;
I found that this was not the case after the operation. This problem occurs because I aborted the error and generated an incomplete var directory, so every time the installation fails, I will delete the following files and Reinitialize them.
12 sudo rm/usr/local/opt/mysql/my. cnf sudo rm-R/usr/local/var/mysql/

After the installation is complete, a large piece of mysql-related information will appear. Run the following command to start mysql

1/usr/local/opt/mysql/bin/mysqld_safe &

After execution, you can run "mysql" on the terminal and directly enter the mysql database. Yes. You can connect directly without entering a password. By default, the password can be accessed anonymously.

3.2.2 Change Password
The Super User "root" does not have a password. To set a password, run the following command:

1/usr/local/opt/mysql/bin/mysqladmin-u root password 'new-password'

Currently, you can connect to mysql without a password. To set security access restrictions for Logon passwords, run the following mysql security installation command.


It is mainly used to set and modify the root password (you do not need to set it, skip it), delete anonymous access, delete root network access, and delete the test database. After the command is executed, login to mysql requires password verification.

1 mysql-u root-p

3.3 start mysql
Mkdir-p ~ /Library/LaunchAgents/

Cp/usr/local/Cellar/mysql/5.6.13/homebrew. mxcl. mysql. plist ~ /Library/LaunchAgents/
Launchctl load-w ~ /Library/LaunchAgents/homebrew. mxcl. mysql. plist

So far, mysql has been installed.

4. install php, php-fpm...
4.1 Installation
Mac is pre-installed with php, but many extensions are not installed. Visual Testing can only execute php commands in the terminal at most, so I chose to reinstall php. Because brew is not installed in php by default, "brew tap" is used to install a third-party brew package. Here, the php installation package of Joseph gonzarez is used. The specific operations are as follows:
Brew tap homebrew/dupes
Brew tap maid/homebrew-php

After execution, you can use brew to install php. Several php versions can be installed here. You can run "brew search php" to check which php versions can be installed. Generally, "php52, php53, php54, and php55" are available, I installed the latest php5.5 version. As FPM (FastCGI Process Manager) has been embedded in PHP5.5, just mark it in the installation options. My php installation configuration command is as follows:

Sudo brew install php55
-- With-debug
-- With-fpm
-- With-gmp
-- With-homebrew-openssl
-- With-imap -- with-intl
-- With-libmysql
-- Without-mysql
-- Without-pcntl
-- Without-pear

For more installation options, see "brew options php55.

Tip:-with-cgi cannot be installed with-fpm and conflict with each other

After the command is executed, php and php-fpm are installed.

4.2 Configuration
Because php is reinstalled, php pre-installed in the system has not been uninstalled, so when the terminal calls php, it is still parsed using the php version of the previous system. Therefore, you need to modify the path here, specifies the php parsing path. In ~ /. Add the last line to bashrc (if no value exists:

1 export PATH = "$ (brew -- prefix php54)/bin: $ PATH"

Add and execute source to make it take effect.

1 source./. profile

If this file does not exist, please refer to this article for configuration: the command line of the Mac system terminal does not execute the command. The command not found solution always appears.

OK, php-fpm installation is complete.

To modify the configuration of php or php-fpm, you can modify "/usr/local/etc/php/5.5/php. ini ","/usr/local/etc/php/5.5/php-fpm.conf ".

For detailed configuration methods, refer to the relevant information:

Php-fpm startup parameters and important configurations
Upgrade php to 5.5.0 through Linux smooth Compilation
Start php-fpm and execute "php-fpm" directly on the terminal. By default, php-fpm is enabled and a shell is displayed, you can also change "daemonize = no" in the configuration file of php-fpm to "daemonize = yes" to start the daemonize process later. For the configuration file you just modified, run "php-fpm-t" to check whether the configuration is correct.

4.3 boot
Start php-fpm at startup (the following 5.5.3 is the current php version number ):

Mkdir-p ~ /Library/LaunchAgents/

Cp/usr/local/Cellar/php54/5.5.3/homebrew-php.josegonzalez.php55.plist ~ /Library/LaunchAgents/
Launchctl load-w ~ /Library/LaunchAgents/homebrew-php.josegonzalez.php55.plist

For convenience, write a shell script to start, shut down, and restart php-fpm:

#! /Bin/sh

Param = $1

Start ()
Fpms = 'ps aux | grep-I "php-fpm" | grep-v grep | awk '{print $2 }''
If [! -N "$ fpms"]; then
Echo "PHP-FPM Start"
Echo "The PHP-FPM Already Start"

Stop ()
Fpms = 'ps aux | grep-I "php-fpm" | grep-v grep | awk '{print $2 }''
Echo $ fpms | xargs kill-9

For pid in $ fpms; do
If echo $ pid | egrep-q '^ [0-9] + $'; then
Echo "PHP-FPM Pid $ pid Kill"
Echo "$ pid IS Not A PHP-FPM Pid"

Case $ param in
'Start ')
Start ;;
'Stop ')
Stop ;;
'Restart ')
Start ;;
Echo "Usage:./phpfpm. sh start | stop | restart ";;

5. Set the php-fpm configuration for nginx
Add the resolution file type "index index.html index.htm index. php;" to the server block ;"

Server {
Listen 80;
Server_name localhost;
Index index.html index.htm index. php;

Open the php location setting commented out by nginx by default, and modify the following (the specific configuration parameters, such as the path, here refer to my local installation ):

Location ~ . *. (Php | php5 )? $ {
# Fastcgi_pass unix:/tmp/php-cgi.sock;
Fastcgi_pass 9000;
Fastcgi_index index. php;
Fastcgi_param SCRIPT_FILENAME/Library/WebServer/public_html $ fastcgi_script_name;
# Include fcgi. conf;

Modify directory users and user groups:

Sudo chown www: www/Library/WebServer/public_html

OK, so that you can execute the php file in the Access Directory (/Library/WebServer/public_html by default. Well, output "phpinfo ()" quickly ~

<? Php
Phpinfo ();

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.