念念不忘
必有回响🎉

在macOS上搭建LNMP环境

最近一直想学着开发一个WordPress插件,大概看了几个教程感觉也不是很难,于是就在电脑上搭建环境试试,在Mac上,我是用的包管理工具是brew,那么我就是通过brew配置的LNMP。

1.安装PHP

在macOS上会自带一个PHP,但是其版本太低,不符合我这个追新的口味,于是通过brew安装最新版的php

brew install [email protected]

安装完成后,在/usr/local/etc/php/7.0可以找到php的相关配置文件。比如php.ini,php-fpm等。
启动php-fpm服务。

brew services restart php70

查看服务进程

lsof -Pni4 | grep LISTEN | grep php
php-fpm   90919 liyang    7u  IPv4 0xca0cc3f188209c89      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   92210 liyang    0u  IPv4 0xca0cc3f188209c89      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   92223 liyang    0u  IPv4 0xca0cc3f188209c89      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   93933 liyang    0u  IPv4 0xca0cc3f188209c89      0t0  TCP 127.0.0.1:9000 (LISTEN)

如果要调试redis等服务,还可以安装php的扩展

brew install [email protected]  #按tab 可以显示如下一些库
[email protected]           [email protected] fpm        [email protected] mysql      [email protected] sqlite3
[email protected] bcmath     [email protected] gd         [email protected] odbc       [email protected] sybase
[email protected] bz2        [email protected] gmp        [email protected] opcache    [email protected] tidy
[email protected] cgi        [email protected] imap       [email protected] pgsql      [email protected] xml
[email protected] cli        [email protected] interbase  [email protected] phpdbg     [email protected] xmlrpc
[email protected] common     [email protected] intl       [email protected] pspell     [email protected] xsl
[email protected] curl       [email protected] json       [email protected] readline   [email protected] zip
[email protected] dba        [email protected] ldap       [email protected] recode     [email protected] memcached
[email protected] dev        [email protected] mbstring   [email protected] snmp       [email protected] xdebug   
[email protected] enchant    [email protected] mcrypt     [email protected] soap       [email protected] redis
[email protected] yaf

2.安装nginx

macOS上默认会安装低版本的Apache httpd,但是我个人比较喜欢nginx,从建站开始就一直使用lnmp架构来运行WordPress,所以还需要安装最新版的nginx。

brew install nginx

安装完成后,在/usr/local/etc/nginx/nginx.conf文件中进行nginx的相关配置。启动nginx后,其默认端口为8080。
其启动、重新加载、停止的命令如下:

sudo nginx
sudo nginx -s reload
sudo nginx -s stop

3.安装mysql

brew install mysql

安装完成后,启动mysql

brew mysql.service start

对mysql进行配置

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

4.配置Nginx

使其支持PHP

    server {
        listen       8080;
        server_name  localhost;

        charset utf-8;
        root /Users/liyang/Documents/Personal/wwwroot;
        #access_log  logs/host.access.log  main;
        index index.php index.html index.htm;
        location / {
           # root   html;
           # index  index.html index.htm;
           try_files $uri $uri/ /index.php?$query_string;
        }

        #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;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80

        location ~ \.php$ {
            try_files $uri /index.php =404;
            fastcgi_split_path_info ^(. \.php)(/. )$;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
            deny  all;
        }

重新启动Nginx,此时LNMP便已经配置完毕,在wwwroot目录中,将WordPress解压到该目录即可。

赞(0) 打赏
未经允许不得转载:Charles's Blog » 在macOS上搭建LNMP环境

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏