Home  >  Article  >  Backend Development  >  Tutorial on installing nginx and php_PHP under mac

Tutorial on installing nginx and php_PHP under mac

WBOY
WBOYOriginal
2016-07-13 10:25:341014browse

1. Preface:
1.1. Environment selection:
Re-configure PHP on the Mac. Originally, the Mac comes with apach, PHP and pgsql. If the diagram is simple, you can use it directly, but after installation I thought carefully about a few questions before:
Should I choose apache or nginx: I know less about both. Relatively speaking, I have more exposure to nginx. The server also uses nginx. There is no way to say that nginx has a high load. . In order to have better access to the nginx environment, I decided to unify the environment to facilitate future use. This is the main key this time;
Install php: Mac os comes with php, which seems to be 5.3.8, and the version is relatively backward. , but this doesn't matter, because I used to manage php through php-osx by Liip, so I don't worry about version issues. However, php-osx by Liip does not seem to have fmp-related configurations, so it will be relatively troublesome for me to use it with nginx. Then I can only choose other solutions
Install mysql: There is no doubt about this , I will definitely not use pgsql at the moment. After all, I have never been exposed to it and it requires learning costs, so I have removed all the parts about pgsql in this record
1.2. How to install it:
Since If I deny the system's own environment, then I have to install it through other methods, such as manual compilation. If you compile it manually, you will encounter the following problems:
Compilation is time-consuming, PHP relies on so many programs, and installing them one by one is too tiring!
What if it is updated in the future? disaster! !
But fortunately, there is its own third-party management program under Mac: homebrew, and relatively speaking, I think it may be superior to apt-get and yum under Linux in some aspects. If you use homebrew to install and manage the operating environment, at least a few of my problems can be solved:
Installation problems, I don’t have to go through the tedious process of installing dependent programs
Upgrade problems, I only need brew update to upgrade all programs at once
Regarding version issues, the homebrew version is updated in a timely manner. As for apt-get and yum, the stable versions used this year are basically the stable versions from a few years ago
OK, the following installations are all based on homebrew. If you are not familiar with it or have not installed it yet If you have homebrew, you can check out this article about: Mac Developer Tool-Homebrew Introduction and Installation.
2. 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 "Open and modify the nginx installation information package formula. By default, it will be opened with vi. Just modify the download address of the corresponding version of nginx at the beginning of the file.
2.2. Configuration
After brew is executed, nginx will be installed. You can use the following commands to operate nginx:

Copy the code The code is as follows:

#Open nginx
sudo nginx

#Reload|Restart|Stop|Quit nginx
nginx -s reload|reopen|stop|quit
After opening nginx, the default access port is 8080, if you want to change it to a commonly used one 80 port, you need to modify the listening port value under "/usr/local/etc/nginx/nginx.conf".
The default file access directory (root) is "/usr/local/Cellar/nginx/1.4.2/html" (1.4.2 here is the installed nginx version, and the folder name is based on the installed nginx version. allow).
2.3. Set nginx to start running at startup:
Copy the code The code is as follows:

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 after trying it, it is not a super user login, but a normal user login, and the listening port is below 1024 (for example, the default 8080 port is changed to 80 port), nginx cannot be started when booting. Therefore, to start nginx, you need to give it administrator rights:
Copy the code The code is as follows:

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

3. Install mysql
3.1. Installation:
Copy the code The code is as follows:

brew install The version installed by mysql

homebrew is also the latest stable version. The installation process is relatively simple, but the initialization is relatively troublesome. I failed many times here.
3.2. Configuration:
3.2.1. Initialization
Initial installation of some configuration databases of mysql (for example: information_schema, mysql)
Copy code The code is as follows:

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

I encountered two problems here, and the process was not recorded. Here is a brief solution:
The system suggested that I add "–explicit_defaults_for_timestamp";
I encountered several errors during the installation process, saying that Several files in db do not exist;
After searching online, most of the suggestions are to modify the permissions of the /usr/local/var/mysql directory;
After doing this, I found that this may not be the case. The problem is caused by My previous operation encountered an error and was aborted, generating an incomplete var directory, so every time the installation failed, I deleted the following files and re-initialized it.
Copy codeThe code is as follows:

sudo rm /usr/local/opt/mysql/my.cnf
sudo rm -R /usr/local/var/mysql/

After the installation is completed, a large piece of mysql related information will appear. Run the following command to start mysql
Copy the code The code is as follows:

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

After execution, you can run "mysql" in the terminal and directly enter the mysql database. Yes, you can connect directly without entering a password. The initial default is anonymous access.
3.2.2. Change the password
The super user "root" also does not have a password. If you want to set a password, you can execute the following command
Copy the code The code is as follows:

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

You can now access mysql without a password Connection, if you want to set some security access restrictions for login passwords, you need to execute the following mysql secure installation instructions
Copy code The code is as follows:

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

Mainly to set and modify the root password (you don’t need to set it if it is set, skip it), delete anonymous access, and delete the root network Access and delete the test database. After the command is executed, password verification is required to log in to mysql
Copy the code The code is as follows:

mysql -u root -p

3.3. Start mysql on boot
Copy the code The code is as follows:

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 is installed.
4. Install php, php-fpm...
4.1. Installation
Mac is pre-installed with php, but many extensions are not installed. Visual inspection can only execute php commands in the terminal, so I choose Reinstall php. Since brew does not have PHP installed by default, you need to use "brew tap" to install brew's third-party package. Here we use josegonzalez's PHP installation package. The specific operations are as follows:
Copy code The code is as follows:

brew tap homebrew/dupes
brew tap josegonzalez/homebrew-php

After execution, you can use brew PHP is installed. There are several versions of php that can be installed. Specifically, you can execute "brew search php" to see what php versions can be installed. Generally, there are "php52, php53, php54, php55" versions. I installed the latest php5.5 version. . Since the PHP5.5 version has embedded FPM (FastCGI Process Manager), just indicate it in the installation options. My PHP installation configuration instructions are as follows:
Copy code The code is 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 "brew options php55".
Tips: –with-cgi and –with-fpm cannot be installed together, as they conflict with each other.
After the command is executed, php and php-fpm will be installed.
4.2. Configuration
Since you are reinstalling PHP, the PHP pre-installed in the system has not been uninstalled yet. Therefore, when calling PHP from the terminal, the PHP version of the previous system is still used for analysis, so you need to modify the path here and specify PHP. parsing path. Add a line at the end of ~/.bashrc (create it if it doesn’t exist):
Copy the code The code is as follows:

export PATH=" $(brew --prefix php54)/bin:$PATH"
[html]
[code]
source ./.profile

If this file does not exist, please refer to this article to configure it: The Mac system terminal command line does not execute commands and the command not found solution always appears
OK, the php-fpm installation is completed.
If you want to modify the configuration of php or php-fpm, you can modify "/usr/local/etc/php/5.5/php.ini" and "/usr/local/etc/php/5.5/php-fpm.conf" .
For detailed configuration methods, please refer to relevant information:
Detailed explanation of php-fpm startup parameters and important configurations
Linux smooth compilation and upgrade php to 5.5.0
To start php-fpm, execute it directly in the terminal." php-fpm". By default, opening php-fpm will display a status shell. You can also change "daemonize = no" in the configuration file of php-fpm to "daemonize = yes", and it will start as a background daemon. , for the newly modified configuration file, you can execute "php-fpm -t" to check whether there are any problems with the configuration.
4.3. Start up
Start up php-fpm (5.5.3 below is the specific version number of PHP currently installed):
Copy code The code is as follows:

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, I wrote a startup, shutdown and restart php -fpm shell script:
Copy code The code is as follows:

#!/bin/sh

param=$1

start()
{
fpms=`ps aux | grep -i "php-fpm" | grep -v grep | awk '{print $2}'`
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 $2}'`
echo $ fpms|xargs kill -9

for pid in $fpms; do
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

5. Set the php-fpm configuration of nginx
Add the parsed file type "index index" in the server block .html index.htm index.php;”

Copy code The code is as follows:
server {
listen 80;
server_name localhost;
index index.html index.htm index.php;
......
}

Open nginx’s default commented out php location Settings, modify as follows (specific configuration parameters, such as paths, are subject to my local installation here):

Copy the code The code is as follows:
Location ~.*. (PHP | PHP5)? $ {
#Fastcgi_pass unix: /tmp/php-cgi.sock;
FastCGI_PASS 127.0.0.1:9000; P;
fastcgi_param SCRIPT_FILENAME /Library/WebServer/public_html$fastcgi_script_name;
include /usr/local/etc/nginx/fastcgi_params;
#include fcgi.conf;
}


Modify directory users and user groups:


Copy code The code is as follows:
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, quickly output "phpinfo()"~


Copy the code The code is as follows:
>phpinfo();

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825016.htmlTechArticle1. Preface: 1.1. Environment selection: Re-configure PHP on the mac, which originally came with the mac apach, php and pgsql, if the diagram is simple, you can just use it directly, but before installation I...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to store php Session in Redis_PHP TutorialNext article:How to store php Session in Redis_PHP Tutorial

Related articles

See more