mac安装nginx、php和postgresql
概述
nginx:以高性能著称的web服务器。
php:流行的web后端开发语言。
postgresql:号称最高级的开源数据库。
本文介绍了在mac下安装nginx + php + postgresql的过程。
homebrew
homebrew(后文简称brew)是mac下很方便的包管理器,类似的还有macports和fink,利用brew可以很方便的安装nginx和PHP 。
命令行执行:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
即可安装brew。
以下是几个常用的brew命令:
brew install
安装软件包
brew uninstall
卸载软件包
brew reinstall
重新安装软件包
brew search
搜索软件包
brew options
查看软件包安装选项
nginx
安装nginx只需执行命令:brew install nginx
,
通过brew options nginx
可以查看更多的安装选项,以下命令增加了gunzip,http2等的支持:
brew install nginx --with-gunzip --with-http2
笔者使用的是上述带http2支持的命令,当前nginx版本1.10.1。
安装后的nginx位于/usr/local/opt/nginx目录(其实是一个链接),配置文件位于/usr/local/etc/nginx。
打开配置文件nginx.conf,作如下修改:
根据需要修改监听端口(可选),原配置文件里的内容这里
使用##注释掉(下同),也可以直接删掉,标记为绿色的是新增的内容:server {
## listen 8080;
listen 80 default_server;
listen 8080 default_server;
server_name localhost;修改文档根目录及增加php页面支持,这里的/Web/www是笔者本地的目录,需要修改为相应的目录:
location / {
## root html;
## index index.html index.htm;
root /Web/www;
index index.php index.html index.htm;
autoindex on; ## optional, Index of /
}这里增加的
autoindex on
(可选)用于访问文件系统,假设文档根目录下有一个autoindex_test的文件夹,里面放一个autoindex_works的文件,没有启用autoindex
时访问http://localhost/autoindex_test浏览器返回:403 Forbidden
nginx/1.10.1
启用时返回:
Index of /autoindex_test/
../
autoindex_works 29-Jan-2016 11:40 0增加php支持:
location ~ .php$ {
## root html;
root /Web/www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#### fastcgi_param SCRIPT_FILENAME /scripts $fastcgi_script_name;
## fastcgi_param SCRIPT_FILENAME $document_root $fastcgi_script_name;
## include fastcgi_params;
fastcgi_intercept_errors on; ## optional
include fastcgi.conf;
}include
的部分也可以简单的只是把/scripts
改成$document_root
。
fastcgi_intercept_errors on
选项是可选的。
将nginx加入path,打开~/.bash_profile,输入:
export NGINX_PATH=/usr/local/opt/nginx
export NGINX_CONF_PATH=/usr/local/etc/nginxexport PATH=$NGINX_PATH/bin:$PATH
警告:export PATH=$NGINX_PATH/bin:$PATH
一定要把:$PATH
添加回去,不然后果很严重。
启动nginx:
打开iTerm等命令行工具,输入source ~/.bash_profile
更新配置文件,再输入sudo nginx
即可启动nginx。
增加nginx的开机启动支持:
打开TextMate之类的文本编辑器,输入:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<