一、什麼是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!
原創文章,作者:RCMG,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/149481.html
微信掃一掃
支付寶掃一掃