Install, configure Nginx, MySQL, php-fpm via homebrew

Source: Internet
Author: User
Tags anonymous fpm install php mkdir phpinfo what php fpms

1.1. Environment Choice:
Re-configure PHP on the Mac, the original Mac on the Apach, PHP and Pgsql, if the diagram is simple, direct use on it, but before the installation I carefully thought about a few questions:

Choose Apache or Nginx: Two I understand are relatively few, relatively I contact Nginx more, the server is also Nginx, no way who call Nginx load high. In order to better contact the Nginx environment, I decided to unify the environment, convenient for future use, this is the main key;
Install Php:mac OS with PHP, it seems to be 5.3.8, the version is relatively backward, but this does not matter, because before I was through the PHP-OSX by LIIP to manage PHP, so do not worry about version problems. However Php-osx by LIIP seems to have no FMP related configuration, so I want to use with Nginx with the words is relatively difficult, then I can only choose other options
Install MySQL: This is no good question, at present I will not go to use pgsql, after all, did not contact, need to have learning costs, so I in this record on the pgsql of the parts are removed
1.2. How to install it:
Since I deny the system's own environment, it is necessary to install it by other means, such as manual compilation. If you compile it manually, you will experience the following problems:

Compile trouble, PHP relies on so many programs, installation, too tired!
What if you update later? Disaster!!
But fortunately Mac has its own Third-party management program: Homebrew, and relatively I think he may be in some ways better than Linux under the Apt-get, yum. If you install and manage your environment through homebrew, you can at least solve a few of my problems:

Installation problem, I do not have to cumbersome to install the dependency program
Upgrade issue, only brew update is required to upgrade all programs at once
Version problem, homebrew version update more timely, and Apt-get, Yum, this year with the basic is a few years ago the stable version of the
OK, the following installation is based on homebrew, if unfamiliar or have not yet installed homebrew, you can view this article on: Mac developer-homebrew Introduction and installation.

Second, install Nginx
2.1. Installation:
Install NIGNX with Brew one-click:

1 Brew Install Nginx

If you need to install a different Nginx version, you can "brew edit nginx" To open the Modify Nginx installation packet formula, the default will be opened with VI, at the beginning of the file to modify the Nginx corresponding version of the download address on the line.

2.2, configuration
After the brew is done, the Nginx is installed. Nginx can be manipulated with the following directives:

#打开 Nginx
sudo nginx

#重新加载 | reboot | stop | exit nginx
Nginx-s Reload|reopen|stop|quit

After opening nginx, the default access port 8080, if you want to change to a common 80 port, modify the/usr/local/etc/nginx/nginx.conf listener (listen) port value.

The default file access directory (root) is "/usr/local/cellar/nginx/1.4.2/html" (1.4.2 is the version of the installed Nginx and the folder name is based on the installed Nginx version).

2.3, the Nginx set to boot operation:
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

But try it, not the superuser login, but the normal user login, and the listening port under 1024 (for example, the default 8080 port to 80 port), Nginx boot is not started. Therefore, to nginx boot, you need to give IT administrator privileges:

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

Third, the installation of MySQL
3.1. Installation:
1 Brew Install MySQL

Homebrew installed version is also the latest stable version, the installation process is relatively simple, but the initialization is relatively troublesome, here I failed many times.

3.2. Configuration:
3.2.1, initialization
Some configuration databases that initially install MySQL (e.g. information_schema, MySQL)

sudo mysql_install_db
--verbose--user= ' WhoAmI '
--basedir= "$ (brew--prefix MySQL)"
--datadir=/usr/local/var/mysql
--tmpdir=/tmp
--explicit_defaults_for_timestamp

Here I encountered two problems, the process is not recorded, here simple to mention the solution:

System suggested that I add "–explicit_defaults_for_timestamp";
Installation process will encounter a few error, said that a few files of DB does not exist;
Online search, most of it is recommended to/usr/local/var/mysql this directory to modify the permissions;
I found that may not be the case, causing the problem is that I encountered the previous operation of the error aborted, generated an incomplete var directory, so each installation failure I will delete the following files, reinitialization is good
sudo rm/usr/local/opt/mysql/my.cnf sudo rm-r/usr/local/var/mysql/

After installation, there will be a large section of MySQL-related information, run the following command to start MySQL

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

After execution, you can run "MySQL" in the terminal, directly into the MySQL database. Yes, without entering the password, you can connect directly, the initial default is anonymous access.

3.2.2, modify password
Super user "Root" is also not set password, to set the password can be executed following instructions

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

Now access to MySQL or password can be connected, if you want to set some login password security access restrictions, you need to perform the following MySQL security installation instructions

1/usr/local/opt/mysql/bin/mysql_secure_installation

The main is to set the Modify root password (settings can not be set, skip), delete anonymous access, delete root network access, delete the test database. After the command is executed, login MySQL requires a password verification

1 Mysql-u root-p

3.3, boot 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

At this point, MySQL installation completed.

Four, install PHP, php-fpm ...
4.1, installation
Mac is pre-installed PHP, but many extensions are not installed, the most visual can only be executed in the terminal PHP instructions, so I chose to reinstall PHP. Because brew defaults to no PHP installation, use "Brew tap" to install the brew's Third-party package, using the Josegonzalez PHP installation package, as follows:
Brew Tap Homebrew/dupes
Brew Tap josegonzalez/homebrew-php

When you're done, you can install PHP with brew. There are several versions of PHP here can be installed, you can perform "Brew search PHP" to see what PHP version can be installed, generally there will be "php52, php53, PHP54, php55" version, I installed the latest php5.5 version. Since the PHP5.5 version has been embedded in the FPM (FastCGI Process Manager), in the installation option to mark the line, my PHP installation configuration instructions are as follows:

sudo brew install Php55
--with-debug
--with-fpm
--with-gmp
--with-homebrew-openssl
--with-imap--with-intl
--with-libmysql
--without-bz2
--without-mysql
--without-pcntl
--without-pear

More installation options can be viewed through the brew options Php55″.

Tip: –with-cgi cannot be installed with –WITH-FPM, conflicting

After the instructions have been executed, PHP and PHP-FPM are installed.

4.2, configuration
Because it is a reload of PHP, before the system pre-installed PHP has not been uninstalled, so in the terminal invoke PHP, or the system before the PHP version to do parsing, so here need to modify path, specify the resolution of PHP path. Add the last line to the ~/.BASHRC (not created):

1 Export path= "$ (Brew--prefix php54)/bin: $PATH"

Add and then run source again to make it effective

1 source./.profile

No friends with this file please refer to this article for configuration: Mac system terminal command line does not execute command always appears command not found solution

OK,PHP-FPM installation completed.

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

Detailed configuration methods can refer to the relevant information:

PHP-FPM start parameter and important configuration detailed
Linux smooth compilation upgrades PHP to 5.5.0
Start PHP-FPM the word directly in the terminal to perform "PHP-FPM", the default open PHP-FPM will show a state shell out, you can also php-fpm configuration file "daemonize = no" to "daemonize = yes", Will be started as a background daemon, for the configuration file just modified, you can perform "php-fpm-t" to detect the configuration has no problem.

4.3, boot up
Boot PHP-FPM (The following 5.5.3 is the specific version number of the currently installed PHP):

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 that starts, shuts down, and restarts PHP-FPM:

#!/bin/sh

Param=$1

Start ()
{
fpms= ' ps aux | Grep-i "PHP-FPM" | Grep-v grep | awk ' {print $} '
if [!-n "$fpms"]; Then
php-fpm
echo "PHP-FPM Start"
Else
echo "PHP-FPM already Start"
Fi
}

Stop ()
{
fpms= ' ps aux | Grep-i "PHP-FPM" | Grep-v grep | awk ' {print $} '
echo $fpms | Xargs kill-9

for PID in $fpms; Todo
If echo $pid | Egrep-q ' ^[0-9]+$ '; Then
echo "PHP-FPM Pid $pid Kill"
Else
echo "$pid is not A php-fpm pid"
Fi
Done
}

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

V. Setting the PHP-FPM configuration of Nginx
Add a parse file type to the server block "index index.html index.htm index.php;"

server {
Listen 80;
server_name localhost;
Index index.html index.htm index.php;
......
}

Open the Nginx default annotation of the PHP location settings, modify the following (specific configuration parameters, such as the path, here on my local installation):

Location ~. *. (PHP|PHP5)? $ {
#fastcgi_pass Unix:/tmp/php-cgi.sock;
Fastcgi_pass 127.0.0.1:9000;
Fastcgi_index index.php;
Fastcgi_param Script_filename/library/webserver/public_html$fastcgi_script_name;
Include/usr/local/etc/nginx/fastcgi_params;
#include fcgi.conf;
}

Modify directory Users, user groups:

sudo chown www:www/library/webserver/public_html

OK, so you can execute the PHP file in the Access directory (default is/library/webserver/public_html). Well, just export the "phpinfo".

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