一、什么是nginxmac
nginxmac是MacOS下一个基于nginx的一款Web服务器,它提供了在本地环境中开发和测试Web应用程序的便捷性。它支持多种语言,比如PHP、Python、Ruby等,并且可以执行FastCGI,使得开发者们可以轻松地进行开发、测试和调试工作。 此外,nginxmac使用了最先进的Web技术,如HTTP/2,TLS/SSL,WebSocket等。
二、如何安装nginxmac
1、下载nginxmac客户端
wget https://github.com/fideloper/nginx-for-devs/releases/download/v1.13.6/nginx-for-devs.zip
2、解压缩该文件并将其放置到目标文件夹中
unzip nginx-for-devs.zip -d nginx-for-devs
mv nginx-for-devs /usr/local
3、将nginxmac的二进制文件路径添加到环境变量(PATH)中
echo 'export PATH="/usr/local/nginx-for-devs:$PATH"' >> ~/.bash_profile
source ~/.bash_profile
三、如何使用nginxmac部署静态文件
1、在nginxmac的目录下,创建一个新目录作为网站的根目录
mkdir /usr/local/nginx-for-devs/myapp
2、将静态文件放置在该目录下 3、创建一个nginx配置文件
touch /usr/local/nginx-for-devs/myapp/nginx.conf
4、编写nginx配置文件,并保存
server {
listen 80;
server_name localhost;
root /usr/local/nginx-for-devs/myapp;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
}
5、启动nginxmac服务器
nginx
6、在浏览器中打开localhost即可看到网站界面
四、如何使用nginxmac部署PHP应用
1、安装PHP
brew install php
2、创建一个php 应用,其中index.php是该应用的入口点
mkdir /usr/local/nginx-for-devs/myapp
touch /usr/local/nginx-for-devs/myapp/index.php
3、编写php代码,并保存在index.php文件中
<?php
echo "Hello nginxmac!";
?>
4、创建nginx配置文件,配置php-fpm FastCGI
server {
listen 80;
server_name localhost;
root /usr/local/nginx-for-devs/myapp;
index index.php;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
5、启动nginx和php-fpm服务
nginx
php-fpm
6、在浏览器中打开localhost即可看到Hello nginxmac!
五、如何使用nginxmac部署Python应用
1、安装Python
brew install python
2、创建一个Python应用
mkdir /usr/local/nginx-for-devs/flaskapp
cd /usr/local/nginx-for-devs/flaskapp
touch app.py requirements.txt
3、编写Python代码,保存在app.py文件中
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello nginxmac!'
if __name__ == '__main__':
app.run()
4、安装必要的依赖
pip install -r requirements.txt
5、创建nginx配置文件,配置uwsgi FastCGI
upstream app {
server 127.0.0.1:5000;
}
server {
listen 80;
server_name localhost;
root /usr/local/nginx-for-devs/flaskapp;
location / {
try_files $uri @app;
}
location @app {
include uwsgi_params;
uwsgi_pass app;
}
}
6、启动nginx和uwsgi服务
nginx
uwsgi --http 127.0.0.1:5000 --wsgi-file /usr/local/nginx-for-devs/flaskapp/app.py
7、在浏览器中打开localhost即可看到Hello nginxmac!