Home > Backend Development > PHP7 > How to use php7 to build LNMP environment on MAC

How to use php7 to build LNMP environment on MAC

醉折花枝作酒筹
Release: 2023-02-18 06:22:01
forward
1819 people have browsed it

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.

How to use php7 to build LNMP environment on MAC

1. Install MySQL:

Check MySQL available version information:

brew info mysql
Copy after login

The version I see here is 5.7.10 :

mysql: stable 5.7.10 (bottled)
Copy after login

Next install MySQL5.7.10:

brew install mysql
Copy after login

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
Copy after login

Start MySQL:

mysql.server start
Copy after login

After startup, since MySQL does not set a password by default, you need to set the root password:

mysql -uroot -p
Copy after login

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
Copy after login

Next, set the root password:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';
Copy after login

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.
Copy after login

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;
Copy after login

Refresh permissions to make the command effective:

flush privileges;
Copy after login

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
Copy after login

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

2, Install php7:

①, download php7:

mkdir ~/php7 && cd ~/php7
git clone https://git.php.net/repository/php-src.git
Copy after login

②, build php7:

cd php-src
./buildconf
Copy after login

③, compile php:

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
Copy after login

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
Copy after login

Install ffmpeg:

brew install ffmpeg
Copy after login

Install openssl:

brew install openssl
brew link openssl --force
Copy after login

Install mcrypt:

brew install mcrypt
Copy after login

Install libiconv:

brew install libiconv
Copy after login

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
Copy after login

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
Copy after login

If you are prompted during the compilation process: Cannot locate header file libintl.h, please perform the following operations:

①, install gettext:

brew install gettext
Copy after login

②, modify configure File:

vi configure
Copy after login

Find the following file:

for i in $PHP_GETTEXT /usr/local /usr ; do
Copy after login

Replace with:

for i in $PHP_GETTEXT /usr/local /usr /usr/local/opt/gettext; do
Copy after login

If an openssl error is prompted, set the path to openssl during compilation,

--with-openssl=/usr/local/opt/openssl/
Copy after login

④. Compile and install after completion:

make && make install
Copy after login

If you try many methods and prompt ssl error, do not add openssl when compiling

⑤. Installation After completion, configure php7:

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
Copy after login

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
Copy after login

Next edit php.ini,

vi /etc/php.ini
Copy after login
Copy after login

Find include_path in php.ini Add include_path:

include_path = "/usr/local/php7/lib/php/php"
Copy after login

Check the php version:

php -v
Copy after login

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
Copy after login

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/
Copy after login

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
Copy after login
Copy after login

Add extension_dir configuration to php.ini:

extension_dir = "/usr/local/php7/lib/php/extensions/debug-zts-20151012/"
Copy after login

Enable opcache extension:

Find opcache in php.ini, add opcache.so

sudo mkdir -p /var/log/opcache
vi /etc/php.ini
Copy after login

Reference opcache.so:

zend_extension=opcache.so
Copy after login

and modify the configuration of opcache :

opcache.enable=1opcache.enable_cli=1opcache.file_cache="/var/log/opcache/"
Copy after login

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
Copy after login

Now that the opcache extension has been added, modify the configuration of php-fpm:

vi /etc/php-fpm.conf
Copy after login

Modify the configuration:

pid = run/php-fpm.pid
error_log = log/php-fpm.log
Copy after login

Start php-fpm:

php-fpm -D
Copy after login

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
Copy after login

The command to stop php-fpm is as follows:

kill -INT `cat /usr/local/php7/var/run/php-fpm.pid`
Copy after login

Restart php- The fpm command is as follows:

kill -USR2 `cat /usr/local/php7/var/run/php-fpm.pid`
Copy after login

Next, start installing nginx:

3. Install nginx:

brew install nginx
Copy after login

After the nginx installation is completed, the default root path is as follows:

The configuration file directory of
Docroot is: /usr/local/var/www
Copy after login

nginx is as follows:

/usr/local/etc/nginx/nginx.conf
Copy after login

nginx virtual site directory is as follows:

nginx will load all files in /usr/local/etc/nginx/servers/.
Copy after login

Start nginx on boot:

ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
Copy after login

Start nginx:

nginx
Copy after login

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
Copy after login

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
Copy after login

Modify the nginx listening port:

sudo vi /etc/nginx.conf
Copy after login

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;
}
Copy after login

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;
        #}
    }
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template