0

0

MAC如何使用php7搭建LNMP环境

醉折花枝作酒筹

醉折花枝作酒筹

发布时间:2021-06-11 09:27:02

|

1876人浏览过

|

来源于CSDN

转载

本篇文章给大家介绍一下mac使用php7搭建lnmp环境的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

MAC如何使用php7搭建LNMP环境

1、安装MySQL:

查看MySQL可用版本信息:

brew info mysql

我这边看到的版本是5.7.10:

mysql: stable 5.7.10 (bottled)

接下来安装MySQL5.7.10:

立即学习PHP免费学习笔记(深入)”;

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了

情感家园企业站5.0 多语言多风格版
情感家园企业站5.0 多语言多风格版

一套面向小企业用户的企业网站程序!功能简单,操作简单。实现了小企业网站的很多实用的功能,如文章新闻模块、图片展示、产品列表以及小型的下载功能,还同时增加了邮件订阅等相应模块。公告,友情链接等这些通用功能本程序也同样都集成了!同时本程序引入了模块功能,只要在系统默认模板上创建模块,可以在任何一个语言环境(或任意风格)的适当位置进行使用!

下载

⑤、安装完成之后配置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视频教程

相关文章

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
高德地图升级方法汇总
高德地图升级方法汇总

本专题整合了高德地图升级相关教程,阅读专题下面的文章了解更多详细内容。

4

2026.01.16

全民K歌得高分教程大全
全民K歌得高分教程大全

本专题整合了全民K歌得高分技巧汇总,阅读专题下面的文章了解更多详细内容。

3

2026.01.16

C++ 单元测试与代码质量保障
C++ 单元测试与代码质量保障

本专题系统讲解 C++ 在单元测试与代码质量保障方面的实战方法,包括测试驱动开发理念、Google Test/Google Mock 的使用、测试用例设计、边界条件验证、持续集成中的自动化测试流程,以及常见代码质量问题的发现与修复。通过工程化示例,帮助开发者建立 可测试、可维护、高质量的 C++ 项目体系。

10

2026.01.16

java数据库连接教程大全
java数据库连接教程大全

本专题整合了java数据库连接相关教程,阅读专题下面的文章了解更多详细内容。

33

2026.01.15

Java音频处理教程汇总
Java音频处理教程汇总

本专题整合了java音频处理教程大全,阅读专题下面的文章了解更多详细内容。

15

2026.01.15

windows查看wifi密码教程大全
windows查看wifi密码教程大全

本专题整合了windows查看wifi密码教程大全,阅读专题下面的文章了解更多详细内容。

42

2026.01.15

浏览器缓存清理方法汇总
浏览器缓存清理方法汇总

本专题整合了浏览器缓存清理教程汇总,阅读专题下面的文章了解更多详细内容。

7

2026.01.15

ps图片相关教程汇总
ps图片相关教程汇总

本专题整合了ps图片设置相关教程合集,阅读专题下面的文章了解更多详细内容。

9

2026.01.15

ppt一键生成相关合集
ppt一键生成相关合集

本专题整合了ppt一键生成相关教程汇总,阅读专题下面的的文章了解更多详细内容。

6

2026.01.15

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PostgreSQL 教程
PostgreSQL 教程

共48课时 | 7.2万人学习

好课诞生记
好课诞生记

共20课时 | 6万人学习

swift开发文档
swift开发文档

共33课时 | 19.6万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号