LNMPPP:linux-nginx-mysql-postgresql-python-php(FastCGI)
前面有讲说要用nginx来配置python,这里记录下配置LNMPPP的过程,What's LNMPPP?有听过LAMP,LNMP吧?这里的LNMPPP是指Linux+Nginx+Mysql+Postgresql+Python+Php。说白了就是在System使用Linux, Web server 使用Nginx,支持Python和Php,数据库使用Postgresql和Mysql。
1、linux
fedora 13(其他版本Linux下面操作请做出相应变动)
2、安装nginx
yum install nginx #下载的是稳定版0.7.65,如果想使用其他版本下载安装即可。
添加到系统启动并运行nginx
chkconfig --levels 235 nginx on
/etc/init.d/nginx start
nginx已经安装完成,在浏览器里输入localhost试一下吧
3、安装Mysql
yum install mysql mysql-server
添加系统服务并启动
chkconfig --levels 235 mysqld on
/etc/init.d/mysqld start
检查是否支持网络连接
netstat -tap | grep mysql
应该会看到这样的状态
netstat -tap | grep mysql
tcp 0 0 *:mysql *:* LISTEN 1376/mysqld
如果不是的话,需要修改/etc/my.cnf文件来启用网络连接支持
sudo gedit /etc/my.cnf
将
[...]
#skip-networking
[...]
#去掉,然后重启mysql
/etc/init.d/mysqld restart
mysql默认root用户的密码为空,需要给root设置密码(* 代表密码字符)
mysqladmin -u root password *****
(*可选)当然你也可以为指定主机设置root的密码
mysqladmin -h example.com -u root password *****
4、安装Postgresql
yum install postgresql postgresql-server
然后需要初始化
Service postgresql initdb
打开/var/lib/pgsql/data/pg_hba.conf文件,将Ipv4 local connections一栏中数据改为:
host all all 0.0.0.0 trust
添加远程连接,将/var/lib/pgsql/data/postgresql.conf中的listen_sddress和port的注释删除,并改为:
listen_address = '*'
port = 5432
安装postgresql管理工具pgadmin3
yum install pgadmin3
即可在Applications——Programming中使用pgadmin3
添加对python的支持
yum install python-psycopg2
5、安装配置python
呃,Linux系统自带Python。但是需要安装flup(http://www.saddi.com/software/flup/dist/),下载解压进入目录安装:
python setup.py install
Nginx配置python
server {
listen 80;
server_name localhost;
# site_media - folder in uri for static files
location /static {
root /var/www/static;
}
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov) {
access_log off; # po co mi logi obrazków
expires 30d;
}
location / {
# host and port to fastcgi server
fastcgi_pass 127.0.0.1:8000;#域名端口,Python程序运行要对应
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_pass_header Authorization;
fastcgi_intercept_errors off;
}
#access_log /var/log/nginx/localhost.access_log main;
#error_log /var/log/nginx/localhost.error_log;
}
}
6、安装配置php
需要安装蛮多的,当然也可根据需要选择
yum install lighttpd-fastcgi php-cli php-mysql php-pgsql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-eaccelerator php-magickwand php-magpierss php-mapserver php-mbstring php-mcrypt php-mssql php-shout php-snmp php-soap php-tidy
打开/etc/php.ini在文件最后加入cgi.fix_pathinfo = 1:
sudo gedit /etc/php.ini
添加
cgi.fix_pathinfo = 1
以FCGI在127.0.0.1的9000端口启动php
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u nginx -g nginx -f /usr/bin/php-cgi -P /var/run/fastcgi-php.pid
当然了,谁也不想每次都这样来启动,需要更改以下文件来添加自动启动
sudo gedit /etc/rc.local
在文件结尾添加
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u nginx -g nginx -f /usr/bin/php-cgi -P /var/run/fastcgi-php.pid
Nginx配置Php
server {
listen 80;
server_name _;#“_”更换为域名
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
参考:
http://howtoforge.net/installing-nginx-with-php5-and-mysql-support-on-fedora-12
http://www.rkblog.rk.edu.pl/w/p/django-nginx/
http://wiki.nginx.org/NginxDjangoFastCGI
http://blog.csdn.net/moxien/archive/2009/12/17/5024705.aspx
很不错,借鉴一下试试!
Mark 一下,正有搭建类似服务器的想法。