Install nginx and php under mac

  • 2020-11-03 22:03:49
  • OfStack

1. Before the words:
1.1 Environment Selection:
Reconfigure php on mac. Originally, mac comes with apach, php and pgsql. If the picture is simple, you can just use it directly.
apache or nginx: I know little about both of them. Comparatively speaking, I have more contact with nginx and the server USES nginx, so I can't say that nginx has a high load. In order to better contact the environment of nginx, I decided to unify the environment of nginx for the convenience of future use, which is the main key this time.
Install php: Mac os comes with php, it looks like 5.3.8, the version is a bit behind, but it doesn't matter, because I used to manage php through ES20en-ES21en by Liip, so I don't worry about the version. However, php-ES26en by Liip seems to have no configuration related to fmp, so it will be relatively troublesome for me to start using nginx1, so I have to choose other schemes
Install mysql: There is no doubt about this. I will definitely not use pgsql at present. After all, I have no contact with pgsql, so there is a learning cost, so I have deleted all the parts about pgsql in this record
1.2 How to install:
Since I have denied the environment that comes with the system, there are other ways to install it, such as manually compiling. If you compile manually, you will encounter the following problems:
Compile trouble, php rely on so many procedures, 1 each installation, too tired!
What if I update it later? Disaster!!!!!
However, mac has its own third party management program: homebrew, and I think it may be superior to ES44en-ES45en and yum under linux in some aspects. At least a few of my problems could have been solved by installing and managing the running environment through homebrew:
Installation problems, I do not have to install the cumbersome dependency procedures
Upgrade problem, only need brew update1 upgrade all programs
Version problem, homebrew version is updated in a timely manner, while ES54en-ES55en and yum, this year's use is basically the stable version of a few years ago
OK, the following installation is based on homebrew, if you are not familiar with or do not have homebrew installed, you can check out this article about Homebrew developer's weapon -Homebrew installation.
2. Install nginx
2.1 Installation:
Install nignx with brew 1:
1
brew install nginx
If you need to install other versions of nginx, you can "brew edit nginx" to open the installation packet formula to modify nginx. By default, vi will be opened. At the beginning of the file, you can just change the download address of the corresponding version of nginx.
2.2, configuration,
After brew is executed, nginx is installed. nginx can be operated on using the following instructions:

# Open the  nginx
sudo nginx
 

# reload | restart | stop | exit nginx
nginx -s reload|reopen|stop|quit
After open nginx, the default access port 8080, if you want to instead of the commonly used port 80, then to modify "/ usr local etc/nginx/nginx conf" under surveillance (listen) port value.
Default file access directory (root) is "/ usr/local/Cellar/nginx / 1.4.2 / html" (1.4.2 is installed nginx version here, the folder name to install nginx version shall prevail).
2.3. Set nginx to boot up and run:

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 1 try, nginx failed to start when the power user logged in, but the normal user logged in, and the monitored port was below 1024 (for example, the default port 8080 was changed to port 80). Therefore, to boot UP nginx, it needs to be given administrator rights:

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:

brew install mysql

The version installed on homebrew is also the latest stable version. The installation process is relatively simple, but initialization is relatively troublesome, and I failed many times here.
3.2 Configuration:
3.2.1 Initialization
Some configuration databases for the initial installation of mysql (for example: 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 is a simple solution:
The system suggested that I add "WMwES138en_ES139en_ES140en_ES141en".
During the installation process, you will encounter several error, saying that several files of db do not exist;
The online search, are mostly recommended/usr local/var/mysql the directory permissions;
I found that this may not be the case after the operation. The problem is that I encountered an error in the previous operation and aborted, and an incomplete var directory was generated. Therefore, every time the installation failed, I deleted the following files and reinitialized them

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

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

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

After the execution, you can run "mysql" in the terminal and go directly to the mysql database. Yes, you don't have to enter a password, you can connect directly, and the initial default is anonymous access.
3.2.2. Change the password
The super user "root" also has no password. To set a password, you can execute the following command

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

Access to mysql can still be connected without a password. If you want to set some of the login password security access restrictions, you need to follow the mysql security installation instructions below

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

It mainly includes setting to modify the root password (setting can be omitted), deleting anonymous access, deleting root network access, and deleting test database. After the command is executed, login to mysql requires password verification

mysql -u root -p

3.3. Boot up 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 is installed.
4. Install php, php-ES188en...
4.1, install,
Mac is pre-installed, but many of the extensions are not installed. Visual inspection can only execute php instructions in the terminal, so I chose to reinstall php. Since brew defaults to no php installation, "brew tap" is used to install the third party program package of brew. The php installation package of josegonzalez is used here. The specific operation is as follows:

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

Once executed, you are ready to install php with brew. Here there are several versions of php that can be installed. Specifically, you can execute "brew search php" to see what version of php can be installed under 1. In general, there will be "php52, php53, php54, php55". As PHP5.5 has been embedded with FPM (FastCGI Process Manager), it should be indicated in the installation option. The installation and configuration instructions of php 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

Additional installation options are available at brew options php55.
Note: with-cgi cannot be installed with ES232en-fpm1. They conflict with each other
After the instructions are executed, php and ES236en-ES237en are installed.
4.2, configuration,
Since php is reinstalled, the php preinstalled by the previous system has not been uninstalled. Therefore, when the terminal calls php, php of the previous system will still be parsed, so path needs to be modified here to specify the parsed path of php. Add a line at the end of ~/.bashrc (not created) :

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

For those who do not have this file, please refer to this article for configuration: Mac system terminal command line does not execute the command always appears command not found solution
OK, php-ES257en installation completed.
To modify the configuration php or php - fpm, can modify "/ usr local etc/php / 5.5 / php ini", "/ usr local etc/php / 5.5 / php - fpm. conf".
Detailed configuration methods can be referred to the relevant information:
Details of es277EN-fpm startup parameters and important configuration
Linux smooth compilation upgrade php to 5.5.0
If you start php-ES284en, you will directly execute "ES285en-ES286en" in the terminal. If you open ES287en-ES288en by default, it will display 1 state of shell. You can also change the "daemonize = no" in the configuration file of ES290en-ES291en to "daemonize = yes", and it will start in the way of the station daemon. You can perform "php-ES297en-ES298en" to check if the configuration is correct.
4.3 Start up
Boot up php-fpm (5.5.3 below is the specific version number of php currently installed) :

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 an shell script to start, close, and restart php-ES308en:

#!/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
        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

5. Set the php-fpm configuration
Add the parse file type index index. html ES321en. htm index. php; to server block

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

Open php location Settings, which are commented out by default by nginx, and modify as follows (specific configuration parameters, such as path, here are based 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 and user groups:

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
8
OK, so you can execute the php file in the access directory (the default is /Library/WebServer/public_html). Well, just print 1 "phpinfo()"

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
9

Related articles: