首頁 > 後端開發 > PHP7 > 主體

MAC如何使用php7搭建LNMP環境

醉折花枝作酒筹
發布: 2023-02-18 06:22:01
轉載
1695 人瀏覽過

這篇文章要為大家介紹MAC使用php7來建構LNMP環境的方法。有一定的參考價值,有需要的朋友可以參考一下,希望對大家有幫助。

MAC如何使用php7搭建LNMP環境

1、安裝MySQL:

查看MySQL可用版本資訊:

brew info mysql
登入後複製

我這邊看到的版本是5.7.10 :

mysql: stable 5.7.10 (bottled)
登入後複製

接下來安裝MySQL5.7.10:

brew install mysql
登入後複製

安裝完成之後依照指示將plist檔案放入~/Library/LaunchAgents/中並load,設定MySQL開機啟動:

ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
登入後複製

啟動MySQL:

mysql.server start
登入後複製

啟動之後由於MySQL預設沒有設定密碼,所以要設定root的密碼:

mysql -uroot -p
登入後複製

提示輸入密碼的時候直接按回車就登入了,登入MySQL後提示如下:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.10 Homebrew
登入後複製

接下來設定root的密碼:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';
登入後複製

設定密碼的時候最好設定一個強密碼,關於強密碼的規則,官方有如下說明:

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.
登入後複製

為了方便使用,我們常常會建立任意連線的root使用者:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'MyNewPass4!' WITH GRANT OPTION;
登入後複製

刷新權限讓指令生效:

flush privileges;
登入後複製

退出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複製mysql設定檔:

sudo cp /usr/local/Cellar/mysql/5.7.10/support-files/my-default.cnf /etc/my.cnf
登入後複製

在/etc/my.cnf 的[mysqld]後面加入lower_case_table_names=1,重新啟動MYSQL服務,這時已設定成功:不區分錶名的大小寫;

PS.lower_case_table_names參數詳解: 0:區分大小寫,1:不區分大小寫

2、安裝php7:

①、下載php7:

mkdir ~/php7 && cd ~/php7
git clone https://git.php.net/repository/php-src.git
登入後複製

②、建置php7:

cd php-src
./buildconf
登入後複製

③、編譯php:

PS.編譯的時候如果記憶體1G以下請在結尾加上:--disable-fileinfo, 

#安裝php7時需要用安裝re2c、bison、ffmpeg、mcrypt、libiconv、gd、openssl: 

安裝re2c:

brew install re2c
登入後複製

安裝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
登入後複製

安裝ffmpeg:

brew install ffmpeg
登入後複製

安裝openssl:

brew install openssl
brew link openssl --force
登入後複製

安裝mcrypt:

brew install mcrypt
登入後複製

安裝libiconv:

brew install libiconv
登入後複製

如果想要用openssl,剛才已經安裝了openssl,但是系統自帶了openssl,所以要用安裝的openssl替換系統自帶的openssl:

sudo ln -sf /usr/local/opt/openssl/bin/openssl /usr/bin/openssl
登入後複製

替換完成之後輸入openssl version就可以看到是上面用brew安裝的openssl了,因為在編譯php過程中需要openssl的header,但是安裝的時候都沒有

#編譯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
登入後複製

如果編譯過程中提示:Cannot locate header file libintl.h,請執行以下操作:

①、安裝gettext:

brew install gettext
登入後複製

②、修改configure檔案:

vi configure
登入後複製

找到以下檔案:

for i in $PHP_GETTEXT /usr/local /usr ; do
登入後複製

取代為:

for i in $PHP_GETTEXT /usr/local /usr /usr/local/opt/gettext; do
登入後複製

如果提示openssl錯誤,在編譯的時候設定openssl的路徑,

--with-openssl=/usr/local/opt/openssl/
登入後複製

④、執行完畢之後進行編譯並安裝:

make && make install
登入後複製

如果嘗試很多辦法都提示ssl出錯,在編譯的時候就不要加上openssl了

#⑤、安裝完成之後配置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
登入後複製

在安轉完成之後會有提示:

You may want to add: /usr/local/php7/lib/php/php to your php.ini include_path
登入後複製

接下來編輯php.ini,

vi /etc/php.ini
登入後複製
登入後複製

找到include_path,在php.ini中加入include_path:

include_path = "/usr/local/php7/lib/php/php"
登入後複製

查看php版本:

php -v
登入後複製

顯示結果如下:

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
登入後複製

更改配置,使php7支援opcache,安裝完成時會提示:

Installing shared extensions:     /usr/local/php7/lib/php/extensions/debug-zts-20151012/
登入後複製

這個路徑是擴充包路徑,將路徑複製下來,找到extension_dir並將剛才的路徑加入php.ini中,

vi /etc/php.ini
登入後複製
登入後複製

在php.ini中加入extension_dir的設定:

extension_dir = "/usr/local/php7/lib/php/extensions/debug-zts-20151012/"
登入後複製

開啟opcache擴充:

在php.ini找到opcache,加入opcache.so

sudo mkdir -p /var/log/opcache
vi /etc/php.ini
登入後複製

引用opcache.so:

zend_extension=opcache.so
登入後複製

並修改opcache的配置:

opcache.enable=1opcache.enable_cli=1opcache.file_cache="/var/log/opcache/"
登入後複製

現在查看php版本信息,顯示結果如下:

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
登入後複製

現在opcache擴充已經加入了,修改php-fpm的設定:

vi /etc/php-fpm.conf
登入後複製

修改設定:

pid = run/php-fpm.pid
error_log = log/php-fpm.log
登入後複製

啟動php-fpm:

php-fpm -D
登入後複製

這樣會提示兩個警告:

[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
登入後複製

停止php-fpm的指令如下:

kill -INT `cat /usr/local/php7/var/run/php-fpm.pid`
登入後複製

重啟php- fpm的指令如下:

kill -USR2 `cat /usr/local/php7/var/run/php-fpm.pid`
登入後複製

接下來開始安裝nginx:

3、安裝nginx:

brew install nginx
登入後複製

安裝完成的nginx,預設的root路徑如下:

Docroot is: /usr/local/var/www
登入後複製

nginx的設定檔目錄如下:

/usr/local/etc/nginx/nginx.conf
登入後複製

nginx虛擬網站目錄如下:

nginx will load all files in /usr/local/etc/nginx/servers/.
登入後複製

開機啟動nginx:

ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
登入後複製

#啟動nginx:

nginx
登入後複製

nginx監聽80埠是需要root權限的,現在nginx預設監聽的是8080埠:

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
登入後複製

設定nginx,先將nginx的設定檔放到/etc下:

sudo ln -s /usr/local/etc/nginx/nginx.conf /etcsudo ln -s /usr/local/etc/nginx/servers /etc/nginxservers
登入後複製

修改nginx監聽埠:

sudo vi /etc/nginx.conf
登入後複製

修改設定檔如下:

#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;
}
登入後複製

然後在/etc/nginxservers/下建立default.conf,編輯default.conf,加入以下內容:

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;
        #}
    }
登入後複製

此時,LNMP已經搭建完畢,重啟php-fpm和nginx。

推薦學習:php影片教學

以上是MAC如何使用php7搭建LNMP環境的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:csdn.net
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!