相信很多使用过apache的同学应该都不陌生nginx,这个由俄罗斯呃呃呃人写的web服务器目前已经得到了大力的普及,而且在中国还有个著名的变种Tengine,这个是淘宝开发的基于nginx的web服务器,其实apache并不是不好,只是新的技术一定会伴随着我们的需求不断的诞生,推陈出新,更新换代是事物发展的必然规律,对于高速发展的计算机领域来说亦是如此。
很多年前(其实也没几年~)我们在念大学的时候,lamp大行其道,占据了互联网开发的半壁江山(何止半壁~),其中的a也就是apache,p就是php或者python或者perl,当然和apache一起占据市场的还有lighttpd,IIS等,今天我们就来简单说一下apache和php的一些运行机制,其实大部分情况下,我们的php都是作为apache的一个模块来安装的,apache每次启动都要加载一次所有已安装的模块,这样效率相对较低,而且apache挂了,php就shi了。至于一些其他特性在此暂且不提。
今天我们来安装nginx,并且一fastcgi模式安装php,在php5.3.3以后的版本fastcgi已经内置为php的核心,不需要单独安装了,不过学一下安装总是好的。写这篇文章时,php5.5.x已经开始露脸,不过使用的还是很好。
我们先来编译安装nginx,首先下载稳定版本1.4.2,解压之后编译这里没什么的,所有路径和选项默认即可,记住安装路径。[shell]
./configure –prefix=/usr/local/nginx
make && make install[/shell]
安装之后需要启动:[shell]
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf[/shell]
如果需要重启可以[shell]
ps -ef | grep nginx[/shell]
直接kill掉或者kill -HUP
重启可以:[shell]
/usr/local/nginx/sbin/nginx -s reload[/shell]
接下来我们安装php,你可以安装php5.3.x版本,这里我安装的是php5.3.25,这个版本没有集成fastcgi,因此我们需要单独安装php-fpm,至于安装php和mysql这里不多将,之前有篇文章的:centos下搭建lamp环境,这里建议使用https://www.atomicorp.com/channels/atomic/这个源工具安装,如果你有时间可以下载具体版本编译一下,这样子灵活一下。安装php-fpm这样安装:[shell]
yum install php-fpm[/shell]
这样就安装了php-fpm,需要启动[shell]
/etc/init.d/php-fpm start[/shell]
这里就要讲到为什么使用fastcgi模式安装php,因为nginx本身不解析php,他需要将php的请求交给gci的一个子进程来处理,然后在将其返回给请求者,因此nginx是一个典型的反向代理服务器,接下来我们配置一个ngin的虚拟主机,并且使其解析php,我们的nginx的配置文件[shell]
/usr/local/nginx/conf/nginx.conf[/shell]
其实对于nginx的这个配置文件,你大可以删掉自己重新配置过的,这里配置一个基于余名的虚拟主机:[shell]
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main ‘$remote_addr – $remote_user [$time_local] "$request" ‘
# ‘$status $body_bytes_sent "$http_referer" ‘
# ‘"$http_user_agent" "$http_x_forwarded_for"’;
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
root html;
index index.html index.htm index.php;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# 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 /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
server {
listen 80;
server_name test.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
root /usr/local/nginx/html/test;
index index.html index.htm index.php;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# 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 /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/test$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;
#}
}
}[/shell]
当然域名解析需要加入[shell]
/etc/hosts[/shell]
以上配置很简单,就是配置了fastcgi的路径和端口,因为php-fpm默认使用9000端口,你可以这样查看[shell]
lsof -i:9000[/shell]
okay这样的话,你就基本搭建好开发环境了。