环境: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,打开浏览器输入http://127.0.0.1,如果看到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 如下:
1<!-- lang: php --> 2user www-data; //指定Nginx Worker 进程运行用户及用户组 3<!-- lang: php --> 4worker_processes 4; / /指定Nginx开启的进程数,每个Nginx进程平均耗费10M-20M内存。 5<!-- lang: php --> 6pid /var/run/nginx.pid; //用来指定进程id的存储文件的位置 7<!-- lang: php --> 8 9<!-- lang: php --> 10events { //用来指定Nginx的工作模式,及连接上限数 11<!-- lang: php --> 12 use epoll; 13<!-- lang: php --> 14 worker_connections 768; 15<!-- lang: php --> 16 # multi_accept on; 17<!-- lang: php --> 18} 19<!-- lang: php --> 20 21<!-- lang: php --> 22http { 23<!-- lang: php --> 24 25<!-- lang: php --> 26 ## 27<!-- lang: php --> 28 # Basic Settings //基本的设置 29<!-- lang: php --> 30 ## 31<!-- lang: php --> 32 33<!-- lang: php --> 34 sendfile on; 35<!-- lang: php --> 36 tcp_nopush on; 37<!-- lang: php --> 38 tcp_nodelay on; 39<!-- lang: php --> 40 keepalive_timeout 65; 41<!-- lang: php --> 42 types_hash_max_size 2048; 43<!-- lang: php --> 44 # server_tokens off; 45<!-- lang: php --> 46 47<!-- lang: php --> 48 # server_names_hash_bucket_size 64; 49<!-- lang: php --> 50 # server_name_in_redirect off; 51<!-- lang: php --> 52 53<!-- lang: php --> 54 include /etc/nginx/mime.types; 55<!-- lang: php --> 56 default_type application/octet-stream; 57<!-- lang: php --> 58 59<!-- lang: php --> 60 ## 61<!-- lang: php --> 62 # Logging Settings //指定日志的存放路径 63<!-- lang: php --> 64 ## 65<!-- lang: php --> 66 67<!-- lang: php --> 68 access_log /var/log/nginx/access.log; 69<!-- lang: php --> 70 error_log /var/log/nginx/error.log; 71<!-- lang: php --> 72 73<!-- lang: php --> 74 ## 75<!-- lang: php --> 76 # Gzip Settings //开启Gzip 压缩 77<!-- lang: php --> 78 ## 79<!-- lang: php --> 80 81<!-- lang: php --> 82 gzip on; 83<!-- lang: php --> 84 gzip_disable "msie6"; 85<!-- lang: php --> 86 87<!-- lang: php --> 88 gzip_vary on; 89<!-- lang: php --> 90 gzip_proxied any; 91<!-- lang: php --> 92 gzip_comp_level 6; 93<!-- lang: php --> 94 gzip_buffers 16 8k; 95<!-- lang: php --> 96 gzip_http_version 1.1; 97<!-- lang: php --> 98 gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; 99<!-- lang: php --> 100 101<!-- lang: php --> 102 ## 103<!-- lang: php --> 104 # nginx-naxsi config 105<!-- lang: php --> 106 ## 107<!-- lang: php --> 108 # Uncomment it if you installed nginx-naxsi 109<!-- lang: php --> 110 ## 111<!-- lang: php --> 112 113<!-- lang: php --> 114 #include /etc/nginx/naxsi_core.rules; 115<!-- lang: php --> 116 117<!-- lang: php --> 118 ## 119<!-- lang: php --> 120 # nginx-passenger config 121<!-- lang: php --> 122 ## 123<!-- lang: php --> 124 # Uncomment it if you installed nginx-passenger 125<!-- lang: php --> 126 ## 127<!-- lang: php --> 128 129<!-- lang: php --> 130 #passenger_root /usr; 131<!-- lang: php --> 132 #passenger_ruby /usr/bin/ruby; 133<!-- lang: php --> 134 135<!-- lang: php --> 136 ## 137<!-- lang: php --> 138 # Virtual Host Configs //虚拟主机的配置 139<!-- lang: php --> 140 ## 141<!-- lang: php --> 142 143<!-- lang: php --> 144 include /etc/nginx/conf.d/*.conf; 145<!-- lang: php --> 146 include /etc/nginx/sites-enabled/*; 147<!-- lang: php --> 148 149<!-- lang: php --> 150 151<!-- lang: php --> 152} 153<!-- lang: php --> 154#mail { 155<!-- lang: php --> 156# # See sample authentication script at: 157<!-- lang: php --> 158# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript 159<!-- lang: php --> 160# 161<!-- lang: php --> 162# # auth_http localhost/auth.php; 163<!-- lang: php --> 164# # pop3_capabilities "TOP" "USER"; 165<!-- lang: php --> 166# # imap_capabilities "IMAP4rev1" "UIDPLUS"; 167<!-- lang: php --> 168# 169<!-- lang: php --> 170# server { 171<!-- lang: php --> 172# listen localhost:110; 173<!-- lang: php --> 174# protocol pop3; 175<!-- lang: php --> 176# proxy on; 177<!-- lang: php --> 178# } 179<!-- lang: php --> 180# 181<!-- lang: php --> 182# server { 183<!-- lang: php --> 184# listen localhost:143; 185<!-- lang: php --> 186# protocol imap; 187<!-- lang: php --> 188# proxy on; 189<!-- lang: php --> 190# } 191<!-- lang: php --> 192#} 193<!-- lang: php -->
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 http://localhost/ 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 http://127.0.0.1; #} # 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,打开浏览器输入http://127.0.0.1/info.php
你可以看见PHP5已经通过FPM/FastCGI工作了,具体可看Server API那行。向下滚动可以看见所有的模块在PHP5中都是可用的,MySQL还没有被列出来,意味着MySQL还没支持PHP5。
六:让MySQL支持PHP5
1,让MySQL支持PHP5,我们可以安装php5-mysql包。其余的包,我们可以按需安装所需要的包,用apt-cache search php5列出PHP的包,看下那个是你所需要的。
2,选择一些你所需要的包,象这样安装: apt-get install php5-mysql php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl
3,重启PHP-FPM /etc/init.d/php5-fpm restart
4,打开浏览器,输入http://127.0.0.1/info.php,看下你安装的包是不是已经被支持了。
七:配置PHP-FPM
vim /etc/php5/fpm/php-fpm.conf 或在 vim /etc/php5/fpm/conf.d/下做更详细的配置,不懂真人的默认就行了 ,也不优化了。
八:在/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
1 root /web/example; 2 index index.php index.html index.htm; 3 4 # Make site accessible from http://localhost/ 5 server_name 218.198.177.252 example.com ; //这个和apache一样的啦,写域名就行了 6 7 location / { 8 # First attempt to serve request as file, then 9 # as directory, then fall back to index.html 10 try_files $uri $uri/ /index.html; 11 # Uncomment to enable naxsi on this location 12 # include /etc/nginx/naxsi.rules 13 } 14 15 location /doc/ { 16 alias /usr/share/doc/; 17 autoindex on; 18 allow 127.0.0.1; 19 deny all; 20 } 21 22 # Only for nginx-naxsi : process denied requests 23 #location /RequestDenied { 24 # For example, return an error code 25 #return 418; 26 #} 27 28 #error_page 404 /404.html; 29 30 # redirect server error pages to the static page /50x.html 31 # 32 #error_page 500 502 503 504 /50x.html; 33 location = /50x.html { 34 root /web/example; 35 }
location ~ .php$ { //nginx处理静态的页面,动态的转给FastCGI处理 # fastcgi_split_path_info ^(.+.php)(/.+)$; # # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini # # # With php5-cgi alone: fastcgi_pass 127.0.0.1:9000; # # With php5-fpm: # fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; }
1 # deny access to .htaccess files, if Apache's document root 2 # concurs with nginx's one 3 # 4 location ~ /\.ht { 5 deny all; 6 }
}
看下效果了,如过你的不成功,自己检查下....
参考资料: 1,http://www.howtoforge.com/installing-nginx-with-php5-and-php-fpm-and-mysql-support-on-ubuntu-11.10 2,http://www.howtoforge.com/installing-nginx-with-php5-and-php-fpm-and-mysql-support-on-ubuntu-11.10-p2 3,张宴的《实战Nginx:取代Apache的高性能Web服务器》