mac搭建nginx和wordpress开发环境

对于不懂后端的我,做这件事真是受尽折磨。 在不懈努力下,终于成功。 下面写下笔记,与大家分享。

第一步:关闭Apache及开机启动

要使用nginx,最好停用mac中自带的Apache。停用很简单:

sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist

第二步:安装homebrew

homebrew是mac下的包管理器,类似于linux下的yumapt。使用homebrew安装nginxphpmysql要比手动安装方便很多。官网地址:http://brew.sh/index_zh-cn.html

安装:

sudo ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

第三步:安装nginx

安装:brew install nginx

启动:sudo nginx

停止:sudo nginx -s quit

配置nginx:

/usr/local/var/log/nginx/下,新建文件:access.logerror.log

扫描二维码关注公众号,回复: 2207917 查看本文章

配置/usr/local/etc/nginx/nginx.conf

#user  nobody;
worker_processes  1;

error_log  /usr/local/var/log/nginx/error.log;

pid        /usr/local/var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    access_log  /usr/local/var/log/nginx/access.log;
    sendfile        on;
    keepalive_timeout  65;

    #
    include conf.d/*.conf;
}

/usr/local/etc/nginx/下,新建文件夹conf.d。在conf.d/下,新建文件default.conf,配置default.conf

    server {
        listen       80  default_server;
        server_name  localhost;    #域名,自定义
        root   网站根目录;    #自定义,如/var/www
        index  index.html index.htm
    }

测试:在你设定的根目录下(例如/var/www/),新建一个静态页index.html,启动nginx,在浏览器中输入localhost,成功看到静态页内容。

第四步:安装php

首先,在brew中添加php的源:

brew tap josegonzalez/php

brew tap homebrew/dupes

查看已添加的源:brew tap

搜索可安装的php:brew search php
一看结果,我靠,怎么这么多!不懂php的我,完全不懂这些是什么东西。 不过不用管他们。只要知道该安装哪些就好。

看到别人安装最多的是php55,安装前首先查看一下安装相关参数的说明:

brew search php55

结果:

--disable-opcache
    Build without Opcache extension
--homebrew-apxs
    Build against apxs in Homebrew prefix
--with-apache
    Enable building of shared Apache 2.0 Handler module, overriding any options which disable apache
--with-cgi
    Enable building of the CGI executable (implies --without-apache)
--with-debug
    Compile with debugging symbols
--with-enchant
    Build with enchant support
--with-fpm
    Enable building of the fpm SAPI executable (implies --without-apache)
--with-gmp
    Build with gmp support
--with-homebrew-curl
    Include Curl support via Homebrew
--with-homebrew-libxslt
    Include LibXSLT support via Homebrew
--with-homebrew-openssl
    Include OpenSSL support via Homebrew
--with-imap
    Include IMAP extension
--with-libmysql
    Include (old-style) libmysql support instead of mysqlnd
--with-mssql
    Include MSSQL-DB support
--with-pdo-oci
    Include Oracle databases (requries ORACLE_HOME be set)
--with-phpdbg
    Enable building of the phpdbg SAPI executable (PHP 5.4 and above)
--with-postgresql
    Build with postgresql support
--with-thread-safety
    Build with thread safety
--with-tidy
    Include Tidy support
--without-bz2
    Build without bz2 support
--without-mysql
    Remove MySQL/MariaDB support
--without-pcntl
    Build without Process Control support
--without-pear
    Build without PEAR
--without-snmp
    Build without SNMP support
--HEAD
    Install HEAD version

没搞明白这些是什么意思,先这么装吧:

brew install php55 --with-fpm, --with-enchant, --with-debug

成功安装后,启动php-fpm:(php-fpm相当于一个接口,nginx和php之间通信通过php-fpm这个东西)

launchctl load -w /usr/local/opt/php55/homebrew.mxcl.php55.plist

停止php-fpm:

launchctl unload -w /usr/local/opt/php55/homebrew.mxcl.php55.plist

设置快捷指令:打开~/.bash_profile,添加

alias php55.start=launchctl\ load\ -w\ /usr/local/opt/php55/homebrew.mxcl.php55.plist
alias php55.stop=launchctl\ unload\ -w\ /usr/local/opt/php55/homebrew.mxcl.php55.plist

快捷指令设置之后,重启shell,就可以用php55.startphp55.stop来启动和停止php-fpm了。

重新配置nginx:配置文件/usr/local/etc/nginx/conf.d/default.conf

    server {
        listen       80  default_server;
        server_name  localhost;    #域名,自定义
        root   网站根目录;    #自定义,如/var/www
        index  index.html index.htm

        # pass the PHP scripts to FastCGI slinerver listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME   网站根目录$fastcgi_script_name;   #如/var/www$fastcgi_script_name
            include        fastcgi_params;
            fastcgi_intercept_errors on;
        }
    }

测试:重启nginx,启动php-fpm后,在网站根目录下新建文件index.php,设置index.php的内容:<?php phpinfo(); ?>。然后浏览器中输入:localhost/index.php,看到php信息,成功。

第五步:安装mysql

安装:brew install mysql

启动:launchctl load -w /usr/local/opt/mysql/homebrew.mxcl.mysql.plist

停止:launchctl unload -w /usr/local/opt/php55/homebrew.mxcl.php55.plist

设置快捷指令:同php-fpm快捷指令一样,打开~/.bash_profile,添加

alias mysql.start=launchctl\ load\ -w\ /usr/local/opt/mysql/homebrew.mxcl.mysql.plist
alias mysql.stop=launchctl\ unload\ -w\ /usr/local/opt/mysql/homebrew.mxcl.mysql.plist

初始化mysql:运行/usr/local/opt/mysql/bin/mysql_secure_installation 将有一个向导知道你一步一步设定安全和配置信息。

安装phpmyadmin: 在http://www.phpmyadmin.net/home_page/downloads.php下载最新版的phpmyadmin,解压后将目录下的所有文件放到网站根目录/phpmyadmin下(如/var/www/phpmyadmin),然后浏览器中输入localhost/phpmyadmin出现首页,输入数据库账号(root)和密码,登陆成功。

第六步:安装wordpress

下载:从https://wordpress.org/download/上下载最新版的wordpress。
解压后将目录下的所有文件放到网站根目录/wordpress下(如/var/www/wordpress)。

设置本地域名:打开文件/etc/hosts,另起一行输入127.0.0.1 mywordpress,保存文件。

我遇到一件很头疼的事:明明nginx,php,mysql都没有问题,可是每当初始化wordpress总会遇到nginx报错:

google一查,原来wordpress官方专门有个配置nginx的教程:http://codex.wordpress.org/Nginx

重新配置nginx

配置/usr/local/etc/nginx.conf

#user  nobody;
worker_processes  1;

error_log  /usr/local/var/log/nginx/error.log;

pid        /usr/local/var/run/nginx.pid;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    access_log  /usr/local/var/log/nginx/access.log;
    sendfile        on;
    keepalive_timeout  65;

    #php max upload limit cannot be larger than this
    client_max_body_size 13m;
    index              index.php index.html index.htm;

    # Upstream to abstract backend connection(s) for PHP.
    upstream php {
      #this should match value of "listen" directive in php-fpm pool
      #server unix:/tmp/php-fpm.sock;
        server 127.0.0.1:9000;
    }

    #
    #
    include conf.d/*.conf;
}

/usr/local/etc/nginx/下新建目录global,在/usr/local/etc/nginx/global/下新建文件添加文件restrictions.confwordpress.conf,分别添加如下内容:

restrictions.conf:

# Global restrictions configuration file.
# Designed to be included in any server {} block.</p>
location = /favicon.ico {
  log_not_found off;
  access_log off;
}

location = /robots.txt {
  allow all;
  log_not_found off;
  access_log off;
}

# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
location ~ /\. {
  deny all;
}

# Deny access to any files with a .php extension in the uploads directory
# Works in sub-directory installs and also in multisite network
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
location ~* /(?:uploads|files)/.*\.php$ {
  deny all;
}

wordpress.conf:

# WordPress single blog rules.
# Designed to be included in any server {} block.

# This order might seem weird - this is attempted to match last if rules below fail.
# http://wiki.nginx.org/HttpCoreModule
location / {
  try_files $uri $uri/ /index.php?$args;
}

# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;

# Directives to send expires headers and turn off 404 error logging.
location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
       access_log off; log_not_found off; expires max;
}

# Uncomment one of the lines below for the appropriate caching plugin (if used).
#include global/wordpress-wp-super-cache.conf;
#include global/wordpress-w3-total-cache.conf;

# Pass all .php files onto a php-fpm/php-fcgi server.
location ~ [^/]\.php(/|$) {
  fastcgi_split_path_info ^(.+?\.php)(/.*)$;
  if (!-f $document_root$fastcgi_script_name) {
    return 404;
  }
  # This is a robust solution for path info security issue and works with "cgi.fix_pathinfo = 1" in /etc/php.ini (default)

  include fastcgi.conf;
  fastcgi_index index.php;
#   fastcgi_intercept_errors on;
  fastcgi_pass php;
}

/usr/local/etc/nginx/conf.d/下新建文件 mywordpress.conf,配置文件内容

server {
  server_name mywordpress;
  root 网站根目录/mywordpress;  #自定义,如/var/www/mywordpress

  index index.php;

  include global/restrictions.conf;
  include global/wordpress.conf;
}

最后一步:重启nginx,启动php-fpm和mysql,在浏览其中输入mywordpress,出现初始化向导,按照向导设置数据库信息,然后成功。


如有问题欢迎留言与我联系! 或者邮箱:[email protected]

猜你喜欢

转载自blog.csdn.net/mvplinchen888/article/details/43803565
今日推荐