环境:Ubuntu 12.0.4 LTS
nginx(发音"engine x")是一个自由,开放源码,高性能的HTTP server。Nginx以稳定性,丰富的功能集,简单的配置,和低资源消耗而出名。本文将向你展示怎么在ubuntu 12.0.4 LTS 上安装Nginx,php5(及php-fpm),MySQL。
一:安装前做个简单的说明
我使用的域名为example.com,ip地址是218.198.177.252。你可以视具体情况更改这些设置。在下文中我将使用root权限安装所需软件,所以请先切换到root用户:sudo su
二:安装MySQL
apt-get install mysql-server mysql-client
安装过程会提示你为MySQL root 用户提供一个密码----这个密码对 root@localhost可用,同时对root@example.com也可用,因此你需要手动为MySQL root用户指定一个密码:
New password for the MySQL "root" user: <-- yourrootsqlpassword
Repeat password for the MySQL "root" user: <-- yourrootsqlpassword
三:安装Nginx
apt-get install nginx
1,启动nginx
/etc/init.d/nginx start
2,打开浏览器输入,如果看到Welcome to nginx!,则说明安装成功,ubuntu 12.0.4 LTS上nginx默认的网站根目录在 /usr/share/nginx/www。
四:安装PHP5
PHP5可以在nginx上通过PHP-FPM(PHP—FPM(FastCGI Process Manager) 是一个可选的 FastCGI,添加了一些了一些很有用的特性,特别是对于繁忙的站点)工作。
说明:Nginx不支持对外部程序的直接调用或解析,所有的外部程序(包括PHP)必须通过FastCGI接口调用。
apt-get install php5-fpm
PHP-FPM是一个守护进程(init脚本文件在/etc/init.d/php5-fpm),它运行了一个FastCGI server,端口是 9000。
五:配置 nginx,以下是我本机的配置文件。
1,nginx的配置文件在/etc/nginx/nginx.conf, vim /etc/nginx/nginx.conf 如下:
user www-data; //指定Nginx Worker 进程运行用户及用户组
worker_processes 4; / /指定Nginx开启的进程数,每个Nginx进程平均耗费10M-20M内存。
pid /var/run/nginx.pid; //用来指定进程id的存储文件的位置
events { //用来指定Nginx的工作模式,及连接上限数
use epoll;
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings //基本的设置
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings //指定日志的存放路径
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings //开启Gzip 压缩
##
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-Javascript text/xml application/xml application/xml+rss text/javascript;
##
# nginx-naxsi config
##
# Uncomment it if you installed nginx-naxsi
##
#include /etc/nginx/naxsi_core.rules;
##
# nginx-passenger config
##
# Uncomment it if you installed nginx-passenger
##
#passenger_root /usr;
#passenger_ruby /usr/bin/ruby;
##
# Virtual Host Configs //虚拟主机的配置
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# #
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
2,虚拟主机被定义在server{}中,默认文件在/etc/nginx/sites-available/default,vim /etc/nginx/sites-available/default。
server {
listen 80; ## listen for ipv4; this line is default and implied
listen [::]:80 default ipv6only=on; ## listen for ipv6
root /usr/share/nginx/www;
index index.php index.html index.htm;
# Make site accessible from
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ /index.html;
}
location /doc {
root /usr/share;
autoindex on;
allow 127.0.0.1;
deny all;
}
location /images {
root /usr/share;
autoindex off;
}
#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 /usr/share/nginx/www;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass ;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
3,保存文件,使配置生效 /etc/init.d/nginx reload
4,在Nginx的默认网站根目录创建一个PHP的测试文件 vim /usr/share/nginx/www/info.php
<? php
phpinfo();
?>
5,打开浏览器输入
你可以看见PHP5已经通过FPM/FastCGI工作了,具体可看Server API那行。向下滚动可以看见所有的模块在PHP5中都是可用的,MySQL还没有被列出来,意味着MySQL还没支持PHP5。