Mac OSX El Capitan 10.11 安装nginx(http2) php7.0 mysql5.7 开发环境

来源:互联网 发布:青少年暴力犯罪数据 编辑:程序博客网 时间:2024/05/22 04:29

在本文中,我将安装以下软件:(基于Mac OSX El Capitan 10.11

PHP 7.0
MySQL服务器5.7

Nginx的1.10(与http2 / SSL)

首先,先启动终端,安装brew,一个很好用的安装包管理器。

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

MySQL

先从最简单的MySQL开始:

brew install mysql  

将安装最新版本的MySQL。默认安装MySQL服务器没有root密码(只有本地用户也可以访问它)。你可以运行mysql_secure_installation,安装后通过BREW提供。
输入以下命令测试MySQL的状态:

mysql -u root -e 'STATUS'  

PS: mysql -u root -p来输入密码登录至mysql.

PHP

其次,安装PHP,执行以下命令

brew install homebrew/php/php70 homebrew/php/php70-mcrypt homebrew/php/php70-gmagick homebrew/php/php70-opcache homebrew/php/php70-xdebug  

作为新版本的php-fpm与OSX有一定的关系,需要优先安装PHP7。通过改变路径进行检查是否已经完成,命令如下:

export PATH="/usr/local/sbin:$PATH"  echo 'export PATH="/usr/local/sbin:$PATH"' >> ~/.bash_profile  

安装完成后,使用php -v 和 php-fpm -v查看PHP的版本信息,如下图所示:

执行php-fpm的预设置命令:

php-fpm --fpm-config /usr/local/etc/php/7.0/php-fpm.conf -D  

你可能还需要安装Laravel框架或者其他的依赖项,可以执行以下命令:

curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer  

Nginx

现在开始安装Nginx的web服务器,使用以下命令进行安装:

brew install --with-http2 nginx  

To configure nginx. We set nginx to use a virtual directory of ~/Hosts/(domain), more will be explained on this later. You can copy this entire whole command block, or edit the config yourself.

配置nginx的相关信息,设置nginx的虚拟目录:~/Hosts/(domain)

sudo bash -c "cat > /usr/local/etc/nginx/nginx.conf" <<- EOM  user $(whoami) $(id -g -n);  worker_processes auto;events {      worker_connections  1024;}http {      include mime.types;    default_type application/octet-stream;    access_log /usr/local/var/log/nginx/access.log;    error_log /usr/local/var/log/nginx/error.log;    sendfile on;    keepalive_timeout 5;    gzip  on;    server {        listen 80 default_server;        listen 443 ssl http2 default_server;        server_name  _;        ssl_certificate ssl/nginx.crt;     #80端口必须配置        ssl_certificate_key ssl/nginx.key; #80端口必须配置        root /Users/$(whoami)/Hosts/\$host;        index index.php;        location / {            try_files \$uri \$uri/ /index.php\$is_args\$args;        }        location ~ \.php\$ {            fastcgi_pass 127.0.0.1:9000;            fastcgi_split_path_info ^(.+\.php)(/.+)$;            try_files \$fastcgi_script_name =404;            fastcgi_index index.php;            include fastcgi.conf;        }        location ~ /\. {            deny all;        }    }}EOM  

 如果需要使用80端口作为默认端口,则需要生成相应的证书和SSL密钥,命令如下:

sudo mkdir /usr/local/etc/nginx/ssl/  sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /usr/local/etc/nginx/ssl/nginx.key -out /usr/local/etc/nginx/ssl/nginx.crt  

启动nginx,后访问localhost看看是否已经成功。

sudo nginx  
curl --head http://localhost  

你将在终端看到类似的信息: Server: nginx/1.10.0.

以上测试完成后,可以创建一个首页来查看基本的php信息了,命令如下:

mkdir -p ~/Hosts/localhost/  echo '<?php phpinfo();' > ~/Hosts/localhost/index.php  

访问 http://localhost/ 你将会看到如下图所示的界面信息,证明已经配置成功。

增加一些额外的服务

通过以下命令增加额外的功能:

sudo nginx  php-fpm --fpm-config /usr/local/etc/php/7.0/php-fpm.conf -D  mysql.server start  

也可以使用别名的方式将服务新增至 .bash_profile 文件中。

alias servers.start='sudo nginx && php-fpm --fpm-config /usr/local/etc/php/7.0/php-fpm.conf -D && mysql.server start'  alias servers.stop='sudo bash -c "killall -9 php-fpm && mysql.server stop && nginx -s stop"'  alias nginx.logs='tail /usr/local/opt/nginx/access.log'  alias nginx.errors='tail /usr/local/opt/nginx/error.log'  

另外,也可以将这些服务设置为自动启动,命令如下:

# Auto-start nginxcp /usr/local/opt/nginx/homebrew.mxcl.nginx.plist ~/Library/LaunchAgents/  launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist  # Auto-start PHP-FPMcp /usr/local/opt/php70/homebrew.mxcl.php70.plist ~/Library/LaunchAgents/  launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php70.plist  # Auto-start MySQL Servercp /usr/local/opt/mysql/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/  launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist  

如果不需要自动启动则可以执行以下命令:

launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist  launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.php70.plist  launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist  rm ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist  rm ~/Library/LaunchAgents/homebrew.mxcl.php70.plist  rm ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist  

最后的说明

最后,我将告诉你一些关于安装动态主机的方法:

mkdir ~/Hosts/test.dev/  sudo bash -c "echo '127.0.0.1 test.dev' >> /etc/hosts"  

就到这里吧,开始你的PHP编程之旅吧!

0 0