天天看點

Ubuntu下安裝Nginx,PHP5(及PHP-FPM),MySQL

 環境: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可用,同時對[email protected]也可用,是以你需要手動為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 如下:

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:

#       # http://wiki.nginx.org/imapauthenticatewithapachephpscript

#

#       # auth_http localhost/auth.php;

#       # pop3_capabilities "top" "user";

#       # imap_capabilities "imap4rev1" "uidplus";

#       server {

#               listen     localhost:110;

#               protocol   pop3;

#               proxy      on;

#       }

#               listen     localhost:143;

#               protocol   imap;

#}

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 /p_w_picpaths {

                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 {

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中新增一個虛拟主機,看下效果。

我的配置檔案:

        listen   80 ; ## listen for ipv4; this line is default and implied

#       listen   [::]:80 default ipv6only=on; ## listen for ipv6

        root /web/example;

        server_name 218.198.177.252 example.com ;   //這個和apache一樣的啦,寫域名就行了

                # uncomment to enable naxsi on this location

                # include /etc/nginx/naxsi.rules

        location /doc/ {

                alias /usr/share/doc/;

        # only for nginx-naxsi : process denied requests

        #location /requestdenied {

                # for example, return an error code

                #return 418;

        #error_page 500 502 503 504 /50x.html;

                root /web/example;

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;

看下效果了,如過你的不成功,自己檢查下....

Ubuntu下安裝Nginx,PHP5(及PHP-FPM),MySQL

參考資料:

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伺服器》