環境:ubuntu 12.0.4 LTS nginx(發音"engine x")是一個自由,開放源碼,高性能的HTTP server。Nginx以穩定性,豐富的功能集,簡單的配置,和低資源消耗而出名。本文將向你展現怎麼在ubuntu 12.0.4 LTS 上安裝Nginx,php5(及php-fpm),mysql。javascript
我使用的域名爲example.com,ip地址是218.198.177.252。你能夠視具體狀況更改這些設置。在下文中我將使用root權限安裝所需軟件,因此請先切換到root用戶:sudo suphp
apt-get install mysql-server mysql-clientcss
安裝過程會提示你爲MySQL root 用戶提供一個密碼----這個密碼對 root@localhost可用,同時對root@example.com也可用,所以你須要手動爲MySQL root用戶指定一個密碼: New password for the MySQL "root" user: <-- yourrootsqlpassword Repeat password for the MySQL "root" user: <-- yourrootsqlpasswordhtml
apt-get install nginxjava
1,啓動nginx /etc/init.d/nginx startnode
2,打開瀏覽器輸入http://127.0.0.1,若是看到Welcome to nginx!,則說明安裝成功,ubuntu 12.0.4 LTS上nginx默認的網站根目錄在 /usr/share/nginx/www。mysql
PHP5能夠在nginx上經過PHP-FPM(PHP—FPM(FastCGI Process Manager) 是一個可選的 FastCGI,添加了一些了一些頗有用的特性,特別是對於繁忙的站點)工做。 說明:Nginx不支持對外部程序的直接調用或解析,全部的外部程序(包括PHP)必須經過FastCGI接口調用。nginx
apt-get install php5-fpmweb
PHP-FPM是一個守護進程(init腳本文件在/etc/init.d/php5-fpm),它運行了一個FastCGI server,端口是 9000。sql
1,nginx的配置文件在/etc/nginx/nginx.conf, vim /etc/nginx/nginx.conf 以下:
<!-- lang: php --> user www-data; //指定Nginx Worker 進程運行用戶及用戶組 <!-- lang: php --> worker_processes 4; / /指定Nginx開啓的進程數,每一個Nginx進程平均耗費10M-20M內存。 <!-- lang: php --> pid /var/run/nginx.pid; //用來指定進程id的存儲文件的位置 <!-- lang: php --> <!-- lang: php --> events { //用來指定Nginx的工做模式,及鏈接上限數 <!-- lang: php --> use epoll; <!-- lang: php --> worker_connections 768; <!-- lang: php --> # multi_accept on; <!-- lang: php --> } <!-- lang: php --> <!-- lang: php --> http { <!-- lang: php --> <!-- lang: php --> ## <!-- lang: php --> # Basic Settings //基本的設置 <!-- lang: php --> ## <!-- lang: php --> <!-- lang: php --> sendfile on; <!-- lang: php --> tcp_nopush on; <!-- lang: php --> tcp_nodelay on; <!-- lang: php --> keepalive_timeout 65; <!-- lang: php --> types_hash_max_size 2048; <!-- lang: php --> # server_tokens off; <!-- lang: php --> <!-- lang: php --> # server_names_hash_bucket_size 64; <!-- lang: php --> # server_name_in_redirect off; <!-- lang: php --> <!-- lang: php --> include /etc/nginx/mime.types; <!-- lang: php --> default_type application/octet-stream; <!-- lang: php --> <!-- lang: php --> ## <!-- lang: php --> # Logging Settings //指定日誌的存放路徑 <!-- lang: php --> ## <!-- lang: php --> <!-- lang: php --> access_log /var/log/nginx/access.log; <!-- lang: php --> error_log /var/log/nginx/error.log; <!-- lang: php --> <!-- lang: php --> ## <!-- lang: php --> # Gzip Settings //開啓Gzip 壓縮 <!-- lang: php --> ## <!-- lang: php --> <!-- lang: php --> gzip on; <!-- lang: php --> gzip_disable "msie6"; <!-- lang: php --> <!-- lang: php --> gzip_vary on; <!-- lang: php --> gzip_proxied any; <!-- lang: php --> gzip_comp_level 6; <!-- lang: php --> gzip_buffers 16 8k; <!-- lang: php --> gzip_http_version 1.1; <!-- lang: php --> gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; <!-- lang: php --> <!-- lang: php --> ## <!-- lang: php --> # nginx-naxsi config <!-- lang: php --> ## <!-- lang: php --> # Uncomment it if you installed nginx-naxsi <!-- lang: php --> ## <!-- lang: php --> <!-- lang: php --> #include /etc/nginx/naxsi_core.rules; <!-- lang: php --> <!-- lang: php --> ## <!-- lang: php --> # nginx-passenger config <!-- lang: php --> ## <!-- lang: php --> # Uncomment it if you installed nginx-passenger <!-- lang: php --> ## <!-- lang: php --> <!-- lang: php --> #passenger_root /usr; <!-- lang: php --> #passenger_ruby /usr/bin/ruby; <!-- lang: php --> <!-- lang: php --> ## <!-- lang: php --> # Virtual Host Configs //虛擬主機的配置 <!-- lang: php --> ## <!-- lang: php --> <!-- lang: php --> include /etc/nginx/conf.d/*.conf; <!-- lang: php --> include /etc/nginx/sites-enabled/*; <!-- lang: php --> <!-- lang: php --> <!-- lang: php --> } <!-- lang: php --> #mail { <!-- lang: php --> # # See sample authentication script at: <!-- lang: php --> # # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript <!-- lang: php --> # <!-- lang: php --> # # auth_http localhost/auth.php; <!-- lang: php --> # # pop3_capabilities "TOP" "USER"; <!-- lang: php --> # # imap_capabilities "IMAP4rev1" "UIDPLUS"; <!-- lang: php --> # <!-- lang: php --> # server { <!-- lang: php --> # listen localhost:110; <!-- lang: php --> # protocol pop3; <!-- lang: php --> # proxy on; <!-- lang: php --> # } <!-- lang: php --> # <!-- lang: php --> # server { <!-- lang: php --> # listen localhost:143; <!-- lang: php --> # protocol imap; <!-- lang: php --> # proxy on; <!-- lang: php --> # } <!-- lang: php --> #} <!-- 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。
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,看下你安裝的包是否是已經被支持了。
vim /etc/php5/fpm/php-fpm.conf 或在 vim /etc/php5/fpm/conf.d/下作更詳細的配置,不懂真人的默認就好了 ,也不優化了。
個人配置文件:
server { listen 80 ; ## listen for ipv4; this line is default and implied
root /web/example; index index.php index.html index.htm; # Make site accessible from http://localhost/ server_name 218.198.177.252 example.com ; //這個和apache同樣的啦,寫域名就好了 location / { # First attempt to serve request as file, then # as directory, then fall back to index.html try_files $uri $uri/ /index.html; # Uncomment to enable naxsi on this location # include /etc/nginx/naxsi.rules } location /doc/ { alias /usr/share/doc/; autoindex on; allow 127.0.0.1; deny all; } # Only for nginx-naxsi : process denied requests #location /RequestDenied { # For example, return an error code #return 418; #} #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 /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; }
# deny access to .htaccess files, if Apache's document root # concurs with nginx's one # location ~ /\.ht { deny all; }
}
看下效果了,如過你的不成功,本身檢查下....
參考資料: 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服務器》