广州linux

当前位置:   网站首页 >> 网络杂谈

mac 搭建 php环境 nginx+php+musql+redis

Nginx

安装

1
$ brew install nginx

Nginx监听80端口需要将Nginx加入到root用户组中

12
$ sudo cp -v /usr/local/opt/nginx/*.plist /Library/LaunchDaemons/$ sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

启动Nginx

1
$ sudo nginx

测试Nginx是否启动成功

1234567891011
$ curl -IL http://127.0.0.1:8080# 如果类似如下返回结果即成功HTTP/1.1 200 OKServer: nginx/1.8.1Date: Mon, 04 Apr 2016 05:45:26 GMTContent-Type: text/htmlContent-Length: 612Last-Modified: Tue, 26 Jan 2016 14:39:38 GMTConnection: keep-aliveETag: "56a7852a-264"Accept-Ranges: bytes

Nginx其它命令

1234
sudo nginx           # 启动nginxsudo nginx -s reload # 重新加载sudo nginx -s reopen # 重启sudo nginx -s quit   # 退出

PHP

添加PHP的源到Homebrew

12
$ brew tap homebrew/dupes$ brew tap homebrew/php

安装PHP和其它扩展

1
$ brew install php56 --whitout-apache --with-imap --with-tidy --with-debug --with-pgsql --with-mysql --with-fpm

设置PHP环境变量

123456789
# 如果使用bash的话$ vim ~/.bash_profile$ export PATH="/usr/local/sbin:$PATH"$ source ~/.bash_profile# 如果使用ZSH的话$ vim ~/.zshrc$ export PATH="/usr/local/sbin:$PATH"$ source ~/.zshrc

设置PHP开机自启动

123
$ mkdir -p ~/Library/LaunchAgents$ ln -sfv /usr/local/opt/php56/homebrew.mxcl.php56.plist ~/Library/LaunchAgents/$ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist

检测PHP是否启动成功

123456
$ lsof -Pni4 | grep LISTEN | grep php# 如果类似如下返回结果即成功php-fpm   59924  zmy    9u  IPv4 0xf206791c2adf7409      0t0  TCP 127.0.0.1:9000 (LISTEN)php-fpm   59930  zmy    0u  IPv4 0xf206791c2adf7409      0t0  TCP 127.0.0.1:9000 (LISTEN)php-fpm   59931  zmy    0u  IPv4 0xf206791c2adf7409      0t0  TCP 127.0.0.1:9000 (LISTEN)php-fpm   59932  zmy    0u  IPv4 0xf206791c2adf7409      0t0  TCP 127.0.0.1:9000 (LISTEN)

MySQL

安装

1
$ brew install mysql

设置MySQL开机自启动

12
$ ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

MySQL安全安装,可以更改root密码、删除匿名用户、关闭远程连接等

1234567
$ mysql_secure_installation> Enter current password for root (enter for none): # 默认没有密码,直接回车即可> Change the root password? [Y/n]                   # 是否更改root密码,选择是,然后输入并确认密码> Remove anonymous users? [Y/n]                     # 是否删除匿名用户,选择是> Disallow root login remotely? [Y/n]               # 是否禁止远程登录,选择是> Remove test database and access to it? [Y/n]      # 是否删除test数据库,选择是> Reload privilege tables now? [Y/n]                # 是否重载表格数据,选择是

OK!测试一下能不能连接上MySQL

1
$ mysql -uroot -p

配置Nginx

仿造UbuntuNginx目录结构创建一下目录

12345678
$ mkdir -p /usr/local/etc/nginx/logs$ mkdir -p /usr/local/etc/nginx/sites-available$ mkdir -p /usr/local/etc/nginx/sites-enabled$ mkdir -p /usr/local/etc/nginx/conf.d$ mkdir -p /usr/local/etc/nginx/ssl$ sudo mkdir -p /var/www$ sudo chown :staff /var/www$ sudo chmod 775 /var/www

修改Nginx配置文件

12345678910111213141516171819202122232425262728
$ vim /usr/local/etc/nginx/nginx.conf# 将配置内容替换为worker_processes  1;error_log /usr/local/etc/nginx/logs/error.log debug;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/etc/nginx/logs/access.log  main;sendfile on;keepalive_timeout 65;index index.html index.php;include /usr/local/etc/nginx/sites-enabled/*;}

创建PHP-FPM配置文件

12345678910
$ cd /usr/local/etc/nginx/conf.d/$ vim php-fpm# 配置内容如下location ~ \.php$ {try_files      $uri = 404;fastcgi_pass   127.0.0.1:9000;fastcgi_index  index.php;fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;include        fastcgi_params;}

加入站点配置文件

123456789101112131415161718192021222324
$ cd /usr/local/etc/nginx/sites-enabled$ vim default# 配置内容如下server {listen 80;server_name localhost;root /var/www/;access_log /usr/local/etc/nginx/logs/default.access.log;error_log /usr/local/etc/nginx/logs/default.error.log;location / {include /usr/local/etc/nginx/conf.d/php-fpm;}location = /info {allow 127.0.0.1;deny all;rewrite (.*) /.info.php;}error_page 404 /404.html;error_page 403 /403.html;}

测试

1234
$ cd /var/www$ vim info.php<?phpphpinfo();

重启Nginx之后访问http://localhost/info.php看看效果吧


发表我的评论

Hi,您需要填写昵称和邮箱!

  • 必填项