mac搭建nginx和wordpress开发环境

news/2025/2/12 21:02:09/

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

第一步:关闭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

配置/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/wwwindex  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-opcacheBuild without Opcache extension
--homebrew-apxsBuild against apxs in Homebrew prefix
--with-apacheEnable building of shared Apache 2.0 Handler module, overriding any options which disable apache
--with-cgiEnable building of the CGI executable (implies --without-apache)
--with-debugCompile with debugging symbols
--with-enchantBuild with enchant support
--with-fpmEnable building of the fpm SAPI executable (implies --without-apache)
--with-gmpBuild with gmp support
--with-homebrew-curlInclude Curl support via Homebrew
--with-homebrew-libxsltInclude LibXSLT support via Homebrew
--with-homebrew-opensslInclude OpenSSL support via Homebrew
--with-imapInclude IMAP extension
--with-libmysqlInclude (old-style) libmysql support instead of mysqlnd
--with-mssqlInclude MSSQL-DB support
--with-pdo-ociInclude Oracle databases (requries ORACLE_HOME be set)
--with-phpdbgEnable building of the phpdbg SAPI executable (PHP 5.4 and above)
--with-postgresqlBuild with postgresql support
--with-thread-safetyBuild with thread safety
--with-tidyInclude Tidy support
--without-bz2Build without bz2 support
--without-mysqlRemove MySQL/MariaDB support
--without-pcntlBuild without Process Control support
--without-pearBuild without PEAR
--without-snmpBuild without SNMP support
--HEADInstall 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/wwwindex  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_nameinclude        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 thisclient_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/mywordpressindex index.php;include global/restrictions.conf;include global/wordpress.conf;
}

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


如有问题欢迎留言与我联系! 或者邮箱:linchen.1987@foxmail.com


http://www.ppmy.cn/news/862585.html

相关文章

小区物业管理系统需求分析

小区物业管理系统核心在于加强管理&#xff0c;提升效率&#xff0c;降低成本。实现物业核心业务信息化&#xff0c;为员工提供流畅运营模式&#xff0c;为业主提供高品质服务&#xff0c;有助于公司做强做大&#xff0c;系统优势主要有以下几方面&#xff1a; • 服务数字化&a…

替换空格反转字符串中的单词(LeetCode 剑指offer05 151)

题目 剑指 Offer 05. 替换空格 思路 遍历&#xff0c;使用新的字符串来接原字符串&#xff0c;如为空格&#xff0c;则加入%20&#xff0c;否则加入原字符串。 不过看了题解有另一种解法&#xff0c;由于空格转化为%20&#xff0c;设计到原字符存储空间的增加&#xff0c;因此…

android 安装在笔记本,笔记本电脑怎么装安卓系统_笔记本安装安卓教程-系统城...

现在电脑的功能越来越全面&#xff0c;我们甚至都可以在电脑上使用手机系统了。一些朋友就比较好奇&#xff0c;笔记本电脑该怎么安装安卓系统呢&#xff1f;其实很简单&#xff0c;接下来小编就给大家带来笔记本安装安卓系统的教程。 具体步骤如下&#xff1a; 1、首先大家需要…

联想笔记本fn键linux,Linux 系统下笔记本电脑的 Fn 键失效

luckykev1n&#xff1a;小弟我最近给自己的笔记本电脑(xps13)安装了 manjaro(linux)系统&#xff0c;安装完之后&#xff0c;发现 Fn 键失效了&#xff0c;不能愉快的通过 Fn 键快捷调节音量和键盘背光灯&#xff0c;有点苦恼。 各位大佬有遇到过这种情况吗&#xff0c;怎么解决…

笔记本电脑 F1 至 F12 是功能键,怎么转换成普通键

前言 最近要用到电脑的 F 键发现如果只按 F 键不会起到 F 键的作用&#xff0c;而是控制声音亮度之类的功能&#xff0c;上网搜索教程&#xff0c;发现一部分教程是进入电脑的 BIOS 中设置 Config&#xff0c;还有一部分是取消 F 键的功能变回普通键&#xff0c;后来发现下面这…

笔记本电脑f11功能键_笔记本电脑按键功能详细图解_笔记本电脑键盘功能详细介绍是什么-win7之家...

对于一些刚接触笔记本电脑的小伙伴们来说&#xff0c;熟知电脑键盘上各个按键的功能可以帮助我们日常使用电脑的时候更加安全&#xff0c;那么笔记本电脑键盘功能有哪些呢&#xff1f;针对这个问题&#xff0c;小编给大家整理了笔记本电脑按键功能详细图解&#xff0c;有一样需…

数据库的备份还原,视图以及索引

一、数据库备份还原 1、创建相关数据库以及数据表 /***************************样例表***************************/ CREATE DATABASE booksDB; use booksDB; CREATE TABLE books ( bk_id INT NOT NULL PRIMARY KEY, bk_title VARCHAR(50) NOT NULL…

Langchain-ChatGLM配置文件参数测试

1 已知可能影响对话效果的参数&#xff08;位于configs/model_config.py文件&#xff09;&#xff1a; # 文本分句长度 SENTENCE_SIZE 100# 匹配后单段上下文长度 CHUNK_SIZE 250 # 传入LLM的历史记录长度 LLM_HISTORY_LEN 3 # 知识库检索时返回的匹配内容条数 VECTO…