多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## 安装Nginx ``` brew install nginx --with-http_geoip_module ``` Nginx启动关闭命令: ``` #测试配置是否有语法错误 nginx -t #打开 nginx sudo nginx #重新加载配置|重启|停止|退出 nginx nginx -s reload|reopen|stop|quit #停止nginx sudo pkill -9 nginx #也可以使用Mac的launchctl来启动|停止 launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist ``` Nginx开机启动 ``` ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist ``` Nginx监听80端口需要root权限执行,因此:(可选) ``` sudo chown root:wheel /usr/local/Cellar/nginx/1.6.0_1/bin/nginx sudo chmod u+s /usr/local/Cellar/nginx/1.6.0_1/bin/nginx ``` ## 配置nginx.conf 创建需要用到的目录: ``` mkdir -p /usr/local/var/logs/nginx //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 ``` `vim /usr/local/etc/nginx/nginx.conf `输入以下内容: ``` worker_processes 1; error_log /usr/local/var/logs/nginx/error.log debug; pid /usr/local/var/run/nginx.pid; events { worker_connections 256; } 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/var/logs/access.log main; sendfile on; keepalive_timeout 65; port_in_redirect off; include /usr/local/etc/nginx/servers/*; } ``` 设置nginx php-fpm配置文件 ``` vim /usr/local/etc/nginx/conf.d/php-fpm #proxy the php scripts to php-fpm location ~ \.php$ { try_files $uri = 404; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_intercept_errors on; include /usr/local/etc/nginx/fastcgi.conf; } ``` nginx虚拟主机准备工作 ``` #创建 info.php index.html 404.html 403.html文件到 /var/www 下面 vi /var/www/info.php vi /var/www/index.html vi /var/www/403.html vi /var/www/404.html ``` 创建默认虚拟主机default `vim /usr/local/etc/nginx/sites-available/default`输入: ``` server { listen 80; server_name localhost; root /var/www/; access_log /usr/local/var/logs/nginx/default.access.log main; location / { index index.html index.htm index.php; autoindex on; 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; } ``` 创建ssl默认虚拟主机default-ssl `vim /usr/local/etc/nginx/sites-available/default-ssl`输入: ``` server { listen 443; server_name localhost; root /var/www/; access_log /usr/local/var/logs/nginx/default-ssl.access.log main; ssl on; ssl_certificate ssl/localhost.crt; ssl_certificate_key ssl/localhost.key; ssl_session_timeout 5m; ssl_protocols SSLv2 SSLv3 TLSv1; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; 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; } ``` 创建phpmyadmin虚拟主机 `vim /usr/local/etc/nginx/sites-available/phpmyadmin` #输入以下配置 ``` server { listen 306; server_name localhost; root /usr/local/share/phpmyadmin; error_log /usr/local/var/logs/nginx/phpmyadmin.error.log; access_log /usr/local/var/logs/nginx/phpmyadmin.access.log main; ssl on; ssl_certificate ssl/phpmyadmin.crt; ssl_certificate_key ssl/phpmyadmin.key; ssl_session_timeout 5m; ssl_protocols SSLv2 SSLv3 TLSv1; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { index index.html index.htm index.php; include /usr/local/etc/nginx/conf.d/php-fpm; } } ``` 设置SSL ``` mkdir -p /usr/local/etc/nginx/ssl openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=US/ST=State/L=Town/O=Office/CN=localhost" -keyout /usr/local/etc/nginx/ssl/localhost.key -out /usr/local/etc/nginx/ssl/localhost.crt openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=US/ST=State/L=Town/O=Office/CN=phpmyadmin" -keyout /usr/local/etc/nginx/ssl/phpmyadmin.key -out /usr/local/etc/nginx/ssl/phpmyadmin.crt ``` 创建虚拟主机软连接,开启虚拟主机 ``` ln -sfv /usr/local/etc/nginx/sites-available/default /usr/local/etc/nginx/sites-enabled/default ln -sfv /usr/local/etc/nginx/sites-available/default-ssl /usr/local/etc/nginx/sites-enabled/default-ssl ln -sfv /usr/local/etc/nginx/sites-available/phpmyadmin /usr/local/etc/nginx/sites-enabled/phpmyadmin ``` 启动|停止Nginx ``` launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist ``` 接下来你可以通过下面这些连接访问: ``` http://localhost/ -> index.html http://localhost/info -> info.php via phpinfo(); http://localhost/404 -> 404.html https://localhost/ -> index.html(SSL) https://localhost/info -> info.php via phpinfo();(SSL) https://localhost/404 -> 404.html(SSL) https://localhost:306 -> phpmyadmin(SSL) ```