Sử dụng CGI script với nginx trên fedora 15

13 236 0
Sử dụng CGI script với nginx trên fedora 15

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Sử dụng CGI Script với Nginx Fedora 15 Common Gateway Interface (CGI) chuẩn để kết nối chương trình ứng dụng với Web server Dữ liệu từ bảng biểu người dùng điền vào trang Web chuyển cho ứng dụng CGI, ứng dụng sau gửi trả nội dung Web tạo theo yêu cầu ngược cho trình duyệt người dùng Trong thân nginx không đáp ứng CGI, có hai giải pháp để giải vấn đề này: yêu cầu proxy cho script CGI tới Thttpd (một máy chủ nhỏ hỗ trợ CGI), thứ hai sử dụng CGI wrapper để đáp ứng CGI script Trong hướng dẫn giúp người dùng Fedora 15 đáp ứng CGI script (Perl script) Lưu ý thư mục root website www.example.com làm minh họa /var/www/www.example.com/web/; cấu hình vhost nằm /etc/nginx/conf.d/www.example.com.vhost Sử dụng Thttpd Đầu tiên mô tả cách cấu hình nginx để yêu cầu proxy cho CGI script (có phần mở rộng cgi pl) tới Thttpd Và cấu hình Thttpd chạy cổng 8000 Hãy bắt đầu việc cài đặt Thttpd Là gói cho Fedora 15, ThttpdCGI cần vá, tải gói src.rpm cho Fedora 15, vá xây dựng gói rpm Cài đặt công cụ cần thiết cho việc xây dựng mội gói rpm lệnh sau: yum groupinstall 'Development Tools' Cài đặt yum-utils (gói chứa công cụ yumdownloader cho phép tải src.rpm) yum install yum-utils Tiếp theo tải gói Thttpd src.rpm Fedora 15 cd /usr/src yumdownloader source thttpd ls -l [root@server1 src]# ls -l total 160 drwxr-xr-x root root 4096 May 18 13:33 debug drwxr-xr-x root root 4096 May 25 16:00 kernels -rw-r r root root 155581 Feb 12 2011 thttpd- 2.25b-24.fc15.src.rpm [root@server1 src]# rpm -ivh thttpd-2.25b-24.fc15.src.rpm Bạn bỏ qua cảnh báo sau: [root@server1 src]# rpm -ivh thttpd-2.25b24.fc15.src.rpm 1:thttpd warning: user mockbuild doe s not exist - using root warning: group mockbuild does not exist - using root warning: user mockbuild does not exist - using root warning: group mockbuild does not exist - using root warning: user mockbuild does not exist - using root warning: group mockbuild does not exist - using root warning: user mockbuild does not exist - using root warning: group mockbuild does not exist - using root warning: user mockbuild does not exist - using root warning: group mockbuild does not exist - using root warning: user mockbuild does not exist - using root warning: group mockbuild does not exist - using root warning: user mockbuild does not exist - using root warning: group mockbuild does not exist - using root warning: user mockbuild does not exist - using root warning: group mockbuild does not exist - using root warning: user mockbuild does not exist - using root warning: group mockbuild does not exist - using root ########################################### [100%] [root@server1 src]# Bây tải vá tới thư mục /root/rpmbuild/SOURCES/ chỉnh sửa tập tin /root/rpmbuild/SPECS/thttpd.spec phù hợp cd /root/rpmbuild/SOURCES/ wget -O thttpd-2.25b-ipreal.patch http://www.danielclemente.com/amarok/ip_real.txt cd /root/rpmbuild/SPECS/ vi thttpd.spec Thêm vào dòng Patch3: thttpd-2.25b-ipreal.patch %patch3 -p1 -b ipreal: [ ] Patch0: thttpd-2.25b-CVE-2005-3124.patch Patch1: thttpd-2.25b-fixes.patch Patch2: thttpd-2.25b-getline.patch Patch3: thttpd-2.25b-ipreal.patch [ ] %prep %setup -q %patch0 -p1 -b CVE-2005-3124 %patch1 -p1 -b fixes %patch2 -p1 -b getline %patch3 -p1 -b ipreal [ ] Xây dựng gói Thttpd rpm sau: rpmbuild -ba thttpd.spec Thttpd rpm khởi tạo /root/rpmbuild/RPMS/x86_64 (/root/rpmbuild/RPMS/i386 hệ thống bạn i386) vào đó: cd /root/rpmbuild/RPMS/x86_64 ls -l [root@server1 x86_64]# ls -l total 196 -rw-r r root root 68909 Oct 17:16 thttpd- -rw-r r root root 129701 Oct 17:16 thttpd- 2.25b-24.fc15.x86_64.rpm debuginfo-2.25b-24.fc15.x86_64.rpm [root@server1 x86_64]# Cài đặt Thttpd: rpm -ivh thttpd-2.25b-24.fc15.x86_64.rpm Sau lưu tập tin gốc /etc/thttpd.conf tạo sau: mv /etc/thttpd.conf /etc/thttpd.conf_orig vi /etc/thttpd.conf # BEWARE : No empty lines are allowed! # This section overrides defaults # This section _documents_ defaults in effect # port=80 # nosymlink # default = !chroot # novhost # nocgipat # nothrottles # host=0.0.0.0 # charset=iso-8859-1 host=127.0.0.1 port=8000 user=thttpd logfile=/var/log/thttpd.log pidfile=/var/run/thttpd.pid dir=/var/www cgipat=**.cgi|**.pl Điều làm cho Thttpd nghe cổng 8000 địa 127.0.0.1; thư mục root /var/www Tạo hệ thống liên kết khởi động cho Thttpd: chkconfig levels 235 thttpd on chạy nó: /etc/init.d/thttpd start Tạo file /etc/nginx/proxy.conf: vi /etc/nginx/proxy.conf proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; Bây mở tập tin cấu hình vhost: vi /etc/nginx/conf.d/www.example.com.vhost thêm location /cgi-bin {} vào nội dung server {}: server { [ ] location /cgi-bin { include proxy.conf; proxy_pass http://127.0.0.1:8000; } [ ] } Khởi động lại nginx: /etc/init.d/nginx reload Bởi thư mục root Thttpd /var/www, location /cgi-bin chuyển tới /var/www/cgi-bin (điều cho toàn vhost bạn, vhost phải đặt CGI script /var/www/cgi-bin; môi trường chia sẻ hosting bị hạn chế Giải pháp sử dụng CGI wrapper trình bày mục viết) Tạo thư mục: mkdir /var/www/cgi-bin đặt CGI script thực thi chúng Vì mục đích thử nghiệm nên tạo Perl script nhỏ có tên Hello World (thay hello_world.cgi bạn dùng phần mở rộng pl -> hello_world.pl): vi /var/www/cgi-bin/hello_world.cgi #!/usr/bin/perl -w # Tell perl to send a html header # So your browser gets the output # rather then (command line # on the server.) print "Content-type: text/html\n\n"; # print your basic html tags # and the content of them print "Hello World!! \n"; print "Hello world\n"; chmod 755 /var/www/cgi-bin/hello_world.cgi Mở trình duyệt bạn lên thử nghiệm script: http://www.example.com/cgi-bin/hello_world.cgi Nếu thứ ok bạn thấy trang sau: Sử dụng Fcgiwrap Fcgiwrap CGI wrapper sử dụng môi trường chia sẻ hosting cho phép vhost sử dụng thư mục cgi-bin riêng Gói fcgiwrap sẵn cho Fedora, bạn cần xây dựng tay Đầu tiên cài đặt số điều kiện: yum groupinstall 'Development Tools' yum install fcgi-devel Bây cài đặt fcgiwrap: cd /usr/local/src/ git clone git://github.com/gnosek/fcgiwrap.git cd fcgiwrap autoreconf -i /configure make make install fcgiwrap sẽn cài /usr/local/sbin/fcgiwrap Tiếp theo cài đặt gói spawn-fcgi phép chạy fcgiwrap daemon: yum install spawn-fcgi Mở /etc/sysconfig/spawn-fcgi vi /etc/sysconfig/spawn-fcgi … sửa lại tập tin đây: # You must set some working options before the "spawnfcgi" service will work # If SOCKET points to a file, then this file is cleaned up by the init script # # See spawn-fcgi(1) for all possible options # # Example : #SOCKET=/var/run/php-fcgi.sock #OPTIONS="-u apache -g apache -s $SOCKET -S -M 0600 -C 32 -F -P /var/run/spawn-fcgi.pid /usr/bin/php-cgi" FCGI_SOCKET=/var/run/fcgiwrap.socket FCGI_PROGRAM=/usr/local/sbin/fcgiwrap FCGI_USER=nginx FCGI_GROUP=nginx FCGI_EXTRA_OPTIONS="-M 0700" OPTIONS="-u $FCGI_USER -g $FCGI_GROUP -s $FCGI_SOCKET S $FCGI_EXTRA_OPTIONS -F -P /var/run/spawn-fcgi.pid - $FCGI_PROGRAM" Tạo hệ thống liên kết khởi động cho spawn-fcgi chkconfig levels 235 spawn-fcgi on … khởi động nó: /etc/init.d/spawn-fcgi start Bạn tìm thấy socket fcgiwrap /var/run/fcgiwrap.socket, thuộc sở hữu user group nginx Mở tập tin cấu hình vhost bạn: vi /etc/nginx/conf.d/www.example.com.vhost thêm location /cgi-bin {} nội dung server {}: server { [ ] 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/cgibin) root /var/www/www.example.com; # 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 $document_root$fastcgi_script_name; } [ ] } Khởi động lại nginx: /etc/init.d/nginx reload Bây tạo thư mục cgi-bin (/var/www/www.example.com/cgi-bin) cần định nghĩa root /var/www/www.example.com; nội dung location /cgi-bin {}: mkdir /var/www/www.example.com/cgi-bin Sau đặt CGI script vào thực thi chúng Để thử nghiệm tạo Perl script nhỏ có tên Hello World (thay hello_world.cgi bạn dùng phần mở rộng pl -> hello_world.pl): vi /var/www/www.example.com/cgi-bin/hello_world.cgi #!/usr/bin/perl -w # Tell perl to send a html header # So your browser gets the output # rather then (command line # on the server.) print "Content-type: text/html\n\n"; # print your basic html tags # and the content of them print "Hello World!! \n"; print "Hello world\n"; chmod 755 /var/www/www.example.com/cgibin/hello_world.cgi Mở trình duyệt bạn lên thử nghiệm script: http://www.example.com/cgi-bin/hello_world.cgi Nếu thứ ổn bạn thấy trang sau: [...]... /usr/lib/cgibin) root /var/www/www.example.com; # 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 $document_root$fastcgi _script_ name; } [ ] } Khởi động lại nginx: /etc/init.d /nginx reload Bây giờ chúng ta tạo thư mục cgi- bin (/var/www/www.example.com /cgi- bin)... (/var/www/www.example.com /cgi- bin) bởi vì cần định nghĩa root /var/www/www.example.com; trong nội dung của location /cgi- bin {}: mkdir /var/www/www.example.com /cgi- bin Sau đó đặt CGI script vào và thực thi chúng Để thử nghiệm chúng tôi tạo ra một Perl script nhỏ có tên Hello World (thay vì hello_world .cgi bạn có thể dùng phần mở rộng pl -> hello_world.pl): vi /var/www/www.example.com /cgi- bin/hello_world .cgi #!/usr/bin/perl... print "Hello World!! \n"; print "Hello world\n"; chmod 755 /var/www/www.example.com/cgibin/hello_world .cgi Mở trình duyệt của bạn lên và thử nghiệm script: http://www.example.com /cgi- bin/hello_world .cgi Nếu mọi thứ đều ổn bạn sẽ thấy trang sau: ... /usr/bin/php -cgi" FCGI_SOCKET=/var/run/fcgiwrap.socket FCGI_PROGRAM=/usr/local/sbin/fcgiwrap FCGI_USER =nginx FCGI_GROUP =nginx FCGI_EXTRA_OPTIONS="-M 0700" OPTIONS="-u $FCGI_USER -g $FCGI_GROUP -s $FCGI_SOCKET... /var/www /cgi- bin/hello_world .cgi Mở trình duyệt bạn lên thử nghiệm script: http://www.example.com /cgi- bin/hello_world .cgi Nếu thứ ok bạn thấy trang sau: Sử dụng Fcgiwrap Fcgiwrap CGI wrapper sử dụng. .. parameters (SCRIPT_ FILENAME) fastcgi_param SCRIPT_ FILENAME $document_root$fastcgi _script_ name; } [ ] } Khởi động lại nginx: /etc/init.d /nginx reload Bây tạo thư mục cgi- bin (/var/www/www.example.com /cgi- bin)

Ngày đăng: 04/12/2015, 17:22

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan