[置顶] 泰晓 RISC-V 实验箱,配套 30+ 讲嵌入式 Linux 系统开发公开课
Add CGI support for Nginx
by falcon of TinyLab.org 2013/12/26
Introduction
Nginx has no builtin CGI support for it cannot call external executables directly, but it allows to perform external executables via the FastCGI interface.
In Linux, FastCGI works in a socket, file socket: unix:/var/run/xxx.socket or Ip socket: 127.0.0.1:9000.
To add CGI support, some extra wrappers must be installed, the wrappers are servers who can fire a thread which execute the external programs, the architecture looks like:
Post
- Internet –> Nginx –> socket –> FastCGI/Wrappers –> CGI Applications
Get
- Internet <– Nginx <– socket <– FastCGI/Wrappers <– CGI Applications
Install FastCGI
First off, Nginx does not provide FastCGI for you, so you’ve got to have a way to spawn your own FastCGI processes, here install spawn-fcgi.
$ sudo apt-get install spawn-fcgi
PHP: php5-fpm
To support PHP, the php5-fpm should be installed:
$ sudp apt-get install php5-fpm
Then, configure your site’s configuration: /etc/nginx/site-available/default
server { ... index index.php index.html index.htm index.xml index.xhtml; ... location / { try_files $uri $uri/ /index.php?q=$request_uri; } ... # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; } }
Then, start the server:
$ sudo service fcgiwrap restart
Shell, Perl: fcgiwrap
To support the other CGI scripts, we can install fcgiwrap.
$ sudo apt-get install fcgiwrap
Configure it, first off, copy the example config to /etc/nginx:
$ dpkg -L fcgiwrap | grep nginx.conf /usr/share/doc/fcgiwrap/examples/nginx.conf $ cp /usr/share/doc/fcgiwrap/examples/nginx.conf /etc/nginx/fcgiwrap.conf
Then, include the file to the server note of your site’s configuration: /etc/nginx/site-available/default
server { ... include /etc/nginx/fcgiwrap.conf; }
The fcgiwrap.conf looks like:
# Include this file on your nginx.conf to support debian cgi-bin scripts using # fcgiwrap location /cgi-bin/ { # Disable gzip (it makes scripts feel slower since they have to complete # before getting gzipped) gzip off; # Set the root to /usr/lib (inside this location this means that we are # giving access to the files under /usr/lib/cgi-bin) root /usr/lib; # Fastcgi socket fastcgi_pass unix:/var/run/fcgiwrap.socket; # Fastcgi parameters, include the standard ones include /etc/nginx/fastcgi_params; # Adjust non standard parameters (SCRIPT_FILENAME) fastcgi_param SCRIPT_FILENAME /usr/lib$fastcgi_script_name; }
猜你喜欢:
- 我要投稿:发表原创技术文章,收获福利、挚友与行业影响力
- 知识星球:独家 Linux 实战经验与技巧,订阅「Linux知识星球」
- 视频频道:泰晓学院,B 站,发布各类 Linux 视频课
- 开源小店:欢迎光临泰晓科技自营店,购物支持泰晓原创
- 技术交流:Linux 用户技术交流微信群,联系微信号:tinylab
支付宝打赏 ¥9.68元 | 微信打赏 ¥9.68元 | |
请作者喝杯咖啡吧 |