Tải bản đầy đủ (.pdf) (13 trang)

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

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (427.45 KB, 13 trang )

Sử dụng CGI Script với Nginx trên Fedora 15
Common Gateway Interface (CGI) là 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 do người dùng điền vào trên trang
Web được chuyển cho ứng dụng CGI, ứng dụng này sau đó sẽ gửi trả nội
dung Web được tạo ra theo yêu cầu ngược về cho trình duyệt của người
dùng. Trong khi bản thân nginx không đáp ứng CGI, chúng ta có hai giải pháp để
giải quyết vấn đề này: đầu tiên là yêu cầu proxy cho các script CGI tới Thttpd
(một máy chủ nhỏ hỗ trợ CGI), thứ hai là sử dụng một CGI wrapper để đáp ứng
CGI script.
Trong bài hướng dẫn dưới đây chúng tôi sẽ giúp người dùng Fedora 15 có thể đáp
ứng được CGI script (Perl script). Lưu ý rằng thư mục root của website
www.example.com làm minh họa là /var/www/www.example.com/web/; cấu hình
vhost nằm trong /etc/nginx/conf.d/www.example.com.vhost.
1. Sử dụng Thttpd
Đầu tiên chúng tôi sẽ mô tả cách cấu hình nginx để yêu cầu proxy cho CGI script
(có phần mở rộng .cgi hoặc .pl) tới Thttpd. Và cấu hình Thttpd chạy trên cổng
8000.
Hãy bắt đầu bằng việc cài đặt Thttpd. Là một gói cho Fedora 15, nhưng
ThttpdCGI cần được vá, do đó chúng ta tải về gói src.rpm cho Fedora 15, vá nó và
xây dựng một gói rpm mới.
Cài đặt các công cụ cần thiết cho việc xây dựng mội gói rpm mới bằng lệnh sau:
yum groupinstall 'Development Tools'
Cài đặt yum-utils (gói chứa công cụ yumdownloader cho phép chúng ta tải về một
src.rpm).
yum install yum-utils


Tiếp theo chúng ta tải về 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. 2 root root

4096 May 18 13:33 debug

drwxr-xr-x. 3 root root

4096 May 25 16:00 kernels

-rw-r--r--

1 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 có thể 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 giờ chúng ta tải về các bản vá tới thư mục /root/rpmbuild/SOURCES/ và
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
/>cd /root/rpmbuild/SPECS/
vi thttpd.spec
Thêm vào dòng Patch3: thttpd-2.25b-ipreal.patch và %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 như sau:
rpmbuild -ba thttpd.spec
Thttpd rpm sẽ được khởi tạo trong /root/rpmbuild/RPMS/x86_64
(/root/rpmbuild/RPMS/i386 nếu hệ thống của bạn là i386) vì vậy hãy vào đó:
cd /root/rpmbuild/RPMS/x86_64
ls -l
[root@server1 x86_64]# ls -l
total 196
-rw-r--r-- 1 root root

68909 Oct

5 17:16 thttpd-

-rw-r--r-- 1 root root 129701 Oct

5 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 đó chúng ta sao lưu tập tin gốc /etc/thttpd.conf và tạo một cái mới như 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 này sẽ làm cho Thttpd nghe trên cổng 8000 của địa chỉ 127.0.0.1; thư mục

root là /var/www.
Tạo hệ thống liên kết khởi động cho Thttpd:
chkconfig --levels 235 thttpd on
và 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 giờ mở tập tin cấu hình vhost:
vi /etc/nginx/conf.d/www.example.com.vhost
và thêm location /cgi-bin {} vào nội dung của 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 vì thư mục root của Thttpd là /var/www, location /cgi-bin được chuyển tới
/var/www/cgi-bin (điều này đúng cho toàn bộ vhost của bạn, do đó mỗi vhost phải
đặt các CGI script của nó trong /var/www/cgi-bin; như vậy môi trường chia sẻ
hosting sẽ bị hạn chế. Giải pháp là chúng ta sử dụng CGI wrapper được trình bày
trong mục 2 của bài viết).
Tạo thư mục:
mkdir /var/www/cgi-bin
và đặt các CGI script trong đó rồi thực thi chúng. Vì mục đích thử nghiệm nên
chúng tôi sẽ 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/cgi-bin/hello_world.cgi
#!/usr/bin/perl -w

# Tell perl to send a html header.
# So your browser gets the output
# rather then <stdout>(command line
# on the server.)
print "Content-type: text/html\n\n";

# print your basic html tags.
# and the content of them.
print "<html><head><title>Hello World!!
</title></head>\n";
print "<body>

Hello world

</body></html>\n";


chmod 755 /var/www/cgi-bin/hello_world.cgi
Mở trình duyệt của bạn lên và thử nghiệm script:
/>Nếu mọi thứ đều ok bạn sẽ thấy trang sau:


2. Sử dụng Fcgiwrap
Fcgiwrap là một CGI wrapper có thể được sử dụng trong môi trường chia sẻ
hosting bởi nó cho phép mỗi vhost sử dụng thư mục cgi-bin riêng.
Gói fcgiwrap không có sẵn cho Fedora, bạn cần xây dựng bằng tay. Đầu tiên là cài
đặt một số điều kiện:
yum groupinstall 'Development Tools'
yum install fcgi-devel
Bây giờ chúng ta có thể 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 cài trong /usr/local/sbin/fcgiwrap.
Tiếp theo cài đặt gói spawn-fcgi để cho phép chạy fcgiwrap như một daemon:
yum install spawn-fcgi
Mở /etc/sysconfig/spawn-fcgi...
vi /etc/sysconfig/spawn-fcgi
… và sửa lại tập tin như dưới đâ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 1 -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 1 -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
… và khởi động nó:
/etc/init.d/spawn-fcgi start
Bạn có thể tìm thấy socket fcgiwrap trong /var/run/fcgiwrap.socket, thuộc sở hữu
của user và group nginx.
Mở tập tin cấu hình vhost của bạn:
vi /etc/nginx/conf.d/www.example.com.vhost
và thêm location /cgi-bin {} trong nội dung của 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 giờ chúng ta tạo thư mục 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 -w

# Tell perl to send a html header.
# So your browser gets the output
# rather then <stdout>(command line
# on the server.)
print "Content-type: text/html\n\n";

# print your basic html tags.
# and the content of them.
print "<html><head><title>Hello World!!
</title></head>\n";
print "<body>

Hello world

</body></html>\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:
/>Nếu mọi thứ đều ổn bạn sẽ thấy trang sau:




×