相比于nginx,apache对perl的支持要更好一些(nginx需要自己写一个wrapper,而且网上down下来的wrapper的版本在post MIME类型的文件时会有问题)。在配置上,从我这个新手的角度来看,apache也是同样简单。
首先从apache官网http://apache.org/上下载httpd模块。用源文件编译就好,没有问题。安装后httpd命令启动http服务器进程。
接着去mysql官网http://mysql.cn/上下载binary或源码。
编译源码的方法如下(参考http://doc.mysql.cn/mysql5/refman-5.1-zh.html-chapter/installing.html#installing-source):
shell> groupadd mysql shell> useradd -g mysql mysql shell> gunzip < mysql-VERSION.tar.gz | tar -xvf - shell> cd mysql-VERSION shell> ./configure --prefix=/usr/local/mysql shell> make shell> make install shell> cp support-files/my-medium.cnf /etc/my.cnf shell> cd /usr/local/mysql shell> bin/mysql_install_db --user=mysql shell> chown -R root . shell> chown -R mysql var shell> chgrp -R mysql . shell> bin/mysqld_safe --user=mysql &
源码编译出来的东西基本上是不给力的。我在其中一台(ubuntu)机器上编译源码后,服务器端运行的没问题,但客户端运行命令mysql时就hang住了,我的解决方法是用client安装包里的binary替换现有的编译好的binary。另一台untuntu上编译出来的东西根本不能用,运行myqsl_install_db命令时就hang住,所以在这台机器上我使用的是安装包。
下载的安装包是mysql-server-VERSION.rpm和mysql-client-VERSION.rmp。在ubuntu上用alien命令转成deb文件再用dkpg命令安装就可以了。这种安装出来的东西是比较靠谱的。在使用前记得运行mysql_install_db(如果数据库里没有mysql数据库)。
用源码或安装包安装好后,运行/usr/local/mysql/mysqld_safe --usr=mysql &启动mysql服务进程。用mysqladmin -s (ping|shutdown|reload)等命令控制服务进程。在mysql服务进程正确启动后,输入命令mysql可以启动客户端输入sql语句。
要用perl连接mysql数据库,需要安装DBI和DBD::mysql两个模块,上CPAN上可以找到。在安装DBD::mysql时,如果用的mysql是用安装包安装的话,需要去下载mysql-devel-VERSION.rpm并安装。而且在安装完DBD::mysql后,在用代码连接数据库时可能会遇到这种错误:undefined symbol: __pure_virtual DynaLoader.pm line 230 mysql.so。解决方法是在编译DBD::mysql的模块前,做下面的事情(/usr/local/mysql部分取决于你实际的安装路径):
The following changes make the compilation of DBD-mysql-4.005 work with no problems on my Debian Etch servers using MySQL 5.0.[27|41]: Edit /usr/local/mysql/bin/mysql_config. Fix old arch declarations to new form. Change any occurance of "-mcpu" to "-march". Add -lmygcc declaration. Change the line that contains: libs=" $ldflags -L$pkglibdir -lmysqlclient -lz -lcrypt -lnsl -lm " To: libs=" $ldflags -L$pkglibdir -lmysqlclient -lz -lcrypt -lnsl -lm -lmygcc " Remove erroneous extra "/mysql" from libdir. Change the line that contains: pkglibdir='/usr/local/mysql/lib/mysql' To: pkglibdir='/usr/local/mysql/lib' Remove erroneous extra "/mysql" from includedir. Change the line that contains: pkgincludedir='/usr/local/mysql/include/mysql' To: pkgincludedir='/usr/local/mysql/include' Then everything works as expected wiht a standard compile. perl Makefile.PL --testuser=[testuser] --testdb=test --testpassword=[testpassword] make make test make install
至于怎么在服务器上运行perl脚本,参考http://httpd.apache.org/docs/2.2/howto/cgi.html。
至此,一切OK。(虽然在此轻描淡写几句话,但是为了在两台机器安装好mysql和DBD::mysql,我花了近两天的时间……)
顺带补充,apache支持同一台服务器上存放多个用户的网站。在conf/httpd.conf里编辑加入下面两行:
这样下列类型的域名http://your-webisite.net/~username就会解析到/home/tommy/somewhere/username/下。参考http://httpd.apache.org/docs/2.2/howto/public_html.html。对于不同的目录,可以使用.htaccess文件进行单独配置,具体参考http://httpd.apache.org/docs/2.2/howto/htaccess.html。UserDir /home/tommy/somewhere UserDir enabled username