Apache2とPythonをUbuntu10.10で
Ubuntu10.10でPythonCGIをやってみようということで
Apache2のインストールと設定をしてみました。
hostsファイルの設定
hosts
192.168.64.21 hoge # Added by NetworkManager 127.0.0.1 localhost.localdomain localhost ::1 hoge.localdomain localhost6.localdomain6 localhost6 127.0.1.1 hoge.localdomain hoge 127.0.0.1 hoge.localdomain hoge
最下行 127.0.0.1 を追加したことと
- 1のIPv6用ループバックアドレスの設定を変更しました。
デフォルト?では
::1 hoge localhost6.localdomain6 localhost6
となっていたため
apacheをリロードするときに
* Restarting web server apache2 apache2: Could not reliably determine the server's fully qualified domain name, using ::1 for ServerName ... waiting apache2: Could not reliably determine the server's fully qualified domain name, using ::1 for ServerName
というエラーが表示されていたためです。
問題はないようですが、ちゃんとFQDNを指定しなさいよ。ということらしいです。
そのためhogeをhoge.localdomainにしました。
/etc/apache2/sites-availableの設定
/etc/apache2/sites-availableにdefaultというファイルがあるので
コピーしてmydomainというファイルを作成しました。
その中で次の内容を変更しました。
<VirtualHost hoge:80> # <-- ココを変更 ServerAdmin hoge@localhost # <-- ココを変更 DocumentRoot /home/fuga/www # <-- ココを変更 ServerName hoge.localdomain # <-- ココを追加 <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory /var/www/> Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all </Directory> ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ <Directory "/usr/lib/cgi-bin"> AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg. LogLevel warn CustomLog ${APACHE_LOG_DIR}/access.log combined Alias /doc/ "/usr/share/doc/" <Directory "/usr/share/doc/"> Options Indexes MultiViews FollowSymLinks AllowOverride None Order deny,allow Deny from all Allow from 127.0.0.0/255.0.0.0 ::1/128 </Directory> </VirtualHost>
あとは特に変えていません。
Ubuntuでは/usr/lib/cgi-binにcgiファイルを置くことを推奨されているようなので
そのままにしました。
設定を有効にする
$ sudo a2ensite mydomain $ sudo service apache2 reload $ sudo service apache2 restart
pythonテストファイルの作成
pytes.py
#!/usr/bin/python # -*- coding: utf-8 -*- ''' 文字列(hello world.)を表示する ''' print "Content-Type: text/plain" print print "hello world."
ブラウザからは
http://hoge/cgi-bin/pytes.py
で表示できます。