Home > Article > Backend Development > How to use php7 to build LNMP environment on MAC
This article will introduce to you how to use php7 to build an LNMP environment on MAC. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Check MySQL available version information:
brew info mysql
The version I see here is 5.7.10 :
mysql: stable 5.7.10 (bottled)
Next install MySQL5.7.10:
brew install mysql
After the installation is complete, follow the prompts to put the plist file into ~/Library/LaunchAgents/ and load it, and set MySQL to start at boot:
ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
Start MySQL:
mysql.server start
After startup, since MySQL does not set a password by default, you need to set the root password:
mysql -uroot -p
When prompted to enter the password, just press Enter to log in. , after logging in to MySQL, the prompt is as follows:
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.10 Homebrew
Next, set the root password:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';
When setting the password, it is best to set a strong password. Regarding the rules of strong passwords, the official instructions are as follows:
Note MySQL's validate_password plugin is installed by default. This will require that passwords contain at least one upper case letter, one lower case letter, one digit, and one special character, and that the total password length is at least 8 characters.
For ease of use, we often create a root user for any connection:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'MyNewPass4!' WITH GRANT OPTION;
Refresh permissions to make the command effective:
flush privileges;
Exit MySQL: exit; PHP 7.1.0 -dev (cli) (built: Feb 4 2016 09:02:09) ( ZTS DEBUG ) Copyright (c) 1997-2016 The PHP Group Zend Engine v3.1.0-dev, Copyright (c) 1998-2016 Zend Technologies with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies Copy the mysql configuration file:
sudo cp /usr/local/Cellar/mysql/5.7.10/support-files/my-default.cnf /etc/my.cnf
Add lower_case_table_names=1 after [mysqld] in /etc/my.cnf, Restart the MYSQL service, and the setting has been successful: the table name is case-insensitive;
PS.lower_case_table_names parameter details: 0: case-sensitive, 1: case-insensitive
mkdir ~/php7 && cd ~/php7 git clone https://git.php.net/repository/php-src.git
cd php-src ./buildconf
PS. If when compiling If the memory is less than 1G, please add at the end: --disable-fileinfo,
When installing php7, you need to install re2c, bison, ffmpeg, mcrypt, libiconv, gd, openssl:
Install re2c:
brew install re2c
Install bison (3.0.4):
brew install bison brew switch bison 3.0.4 brew link bison --force sudo mv /usr/bin/bison /usr/bin/bison.orig sudo ln -s /usr/local/bin/bison /usr/bin/bison
Install ffmpeg:
brew install ffmpeg
Install openssl:
brew install openssl brew link openssl --force
Install mcrypt:
brew install mcrypt
Install libiconv:
brew install libiconv
If you want to use openssl, you have just installed openssl, but the system comes with openssl, so you need to replace the system’s own openssl with the installed openssl:
sudo ln -sf /usr/local/opt/openssl/bin/openssl /usr/bin/openssl
After the replacement is completed, enter openssl version and you can see that it is the openssl installed with brew above, because the header of openssl is needed during the process of compiling php, but it is not installed during installation.
Compile php7:
./configure --prefix=/usr/local/php7 --exec-prefix=/usr/local/php7 --bindir=/usr/local/php7/bin --sbindir=/usr/local/php7/sbin --includedir=/usr/local/php7/include --libdir=/usr/local/php7/lib/php --mandir=/usr/local/php7/php/man --with-config-file-path=/usr/local/php7/etc --enable-bcmath --enable-calendar --enable-debug --enable-exif --enable-fileinfo --enable-filter --enable-fpm --enable-ftp --enable-gd-jis-conv --enable-gd-native-ttf --enable-hash --enable-json --enable-libxml --enable-maintainer-zts --enable-mbregex --enable-mbstring --enable-mysqlnd --enable-opcache --enable-opcache-file --enable-pcntl --enable-pdo --enable-session --enable-shared --enable-shmop --enable-simplexml --enable-soap --enable-sockets --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-wddx --enable-xml --enable-zip --with-bz2 --with-curl --with-fpm-user=www --with-fpm-group=www --with-freetype-dir=/usr --with-gd --with-gettext --with-gmp --with-iconv --with-jpeg-dir=/usr --with-mcrypt=/usr/include --with-mhash --with-mysql-sock=/var/lib/mysql/mysql.sock --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-pear --with-png-dir=/usr --with-xmlrpc --with-zlib -with-libxml-dir=/usr
If you are prompted during the compilation process: Cannot locate header file libintl.h, please perform the following operations:
①, install gettext:
brew install gettext
②, modify configure File:
vi configure
Find the following file:
for i in $PHP_GETTEXT /usr/local /usr ; do
Replace with:
for i in $PHP_GETTEXT /usr/local /usr /usr/local/opt/gettext; do
If an openssl error is prompted, set the path to openssl during compilation,
--with-openssl=/usr/local/opt/openssl/
make && make install
If you try many methods and prompt ssl error, do not add openssl when compiling
sudo ln -s /usr/local/php7/bin/php* /usr/bin/ sudo ln -s /usr/local/php7/sbin/php-fpm /usr/bin cp php.ini-production /usr/local/php7/etc/php.ini cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf sudo ln -s /usr/local/php7/etc/php.ini /etc/php.ini sudo ln -s /usr/local/php7/etc/php-fpm.conf /etc/php-fpm.conf cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
There will be a prompt after the installation is completed:
You may want to add: /usr/local/php7/lib/php/php to your php.ini include_path
Next edit php.ini,
vi /etc/php.ini
Find include_path in php.ini Add include_path:
include_path = "/usr/local/php7/lib/php/php"
Check the php version:
php -v
The display result is as follows:
PHP 7.1.0-dev (cli) (built: Feb 4 2016 09:02:09) ( ZTS DEBUG ) Copyright (c) 1997-2016 The PHP Group Zend Engine v3.1.0-dev, Copyright (c) 1998-2016 Zend Technologies
Change the configuration so that php7 supports opcache. When the installation is completed, it will prompt:
Installing shared extensions: /usr/local/php7/lib/php/extensions/debug-zts-20151012/
This path is the extension package path. Copy the path, find extension_dir and add the just path to php.ini.
vi /etc/php.ini
Add extension_dir configuration to php.ini:
extension_dir = "/usr/local/php7/lib/php/extensions/debug-zts-20151012/"
Enable opcache extension:
Find opcache in php.ini, add opcache.so
sudo mkdir -p /var/log/opcache vi /etc/php.ini
Reference opcache.so:
zend_extension=opcache.so
and modify the configuration of opcache :
opcache.enable=1opcache.enable_cli=1opcache.file_cache="/var/log/opcache/"
Now check the php version information, the displayed results are as follows:
PHP 7.1.0-dev (cli) (built: Feb 4 2016 09:02:09) ( ZTS DEBUG ) Copyright (c) 1997-2016 The PHP Group Zend Engine v3.1.0-dev, Copyright (c) 1998-2016 Zend Technologies with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
Now that the opcache extension has been added, modify the configuration of php-fpm:
vi /etc/php-fpm.conf
Modify the configuration:
pid = run/php-fpm.pid error_log = log/php-fpm.log
Start php-fpm:
php-fpm -D
This will prompt two warnings:
[04-Feb-2016 09:45:25] NOTICE: [pool www] 'user' directive is ignored when FPM is not running as root [04-Feb-2016 09:45:25] NOTICE: [pool www] 'group' directive is ignored when FPM is not running as root
The command to stop php-fpm is as follows:
kill -INT `cat /usr/local/php7/var/run/php-fpm.pid`
Restart php- The fpm command is as follows:
kill -USR2 `cat /usr/local/php7/var/run/php-fpm.pid`
Next, start installing nginx:
brew install nginx
After the nginx installation is completed, the default root path is as follows:
The configuration file directory ofDocroot is: /usr/local/var/www
nginx is as follows:
/usr/local/etc/nginx/nginx.conf
nginx virtual site directory is as follows:
nginx will load all files in /usr/local/etc/nginx/servers/.
Start nginx on boot:
ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
Start nginx:
nginx
nginx requires root permissions to listen to port 80. Now nginx listens to port 8080 by default:
sudo chown root:wheel /usr/local/Cellar/nginx/1.8.1/bin/nginx sudo chmod u+s /usr/local/Cellar/nginx/1.8.1/bin/nginx
To configure nginx, first place the nginx configuration file under /etc:
sudo ln -s /usr/local/etc/nginx/nginx.conf /etcsudo ln -s /usr/local/etc/nginx/servers /etc/nginxservers
Modify the nginx listening port:
sudo vi /etc/nginx.conf
Modify the configuration file as follows:
#user nobody; worker_processes 4; error_log /usr/local/var/log/error.log; error_log /usr/local/var/log/error.log notice; error_log /usr/local/var/log/error.log info; pid /usr/local/var/run/nginx.pid; events { worker_connections 1024; } 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/access.log main; port_in_redirect off; sendfile on; tcp_nopush on; keepalive_timeout 65; gzip on; # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} include servers/*.conf; }
Then create default.conf under /etc/nginxservers/, edit default.conf, and add the following content:
server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_intercept_errors on; include /usr/local/etc/nginx/fastcgi.conf; } } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
At this point, LNMP has been set up, restart php-fpm and nginx.
Recommended learning: php video tutorial
The above is the detailed content of How to use php7 to build LNMP environment on MAC. For more information, please follow other related articles on the PHP Chinese website!