`
lz1130
  • 浏览: 403665 次
  • 性别: Icon_minigender_1
  • 来自: 福建
社区版块
存档分类
最新评论

nginx php 安装与配置

阅读更多
nginx + php(fastcgi) + mysql + eaccelerator + ZendOptimizer + phpmyadmin
所需软件
spawn-fcgi-1.6.0.tar.gz
nginx-0.6.36.tar.gz
mysql-5.0.56.tar.gz
php-5.2.6.tar.gz
ZendOptimizer-3.3.3-linux-glibc23-x86_64.tar.gz
phpMyAdmin-3.1.3-all-languages.tar.bz2
suhosin-patch-5.2.6-0.9.6.2.patch.gz
eaccelerator-0.9.5.3.tar.bz2

自己整理的,可能会有些排版问题导致某些字符错误。附件有个精简版的,命令字符无误。

操作系统:
ubuntu 9.04 64位


Nginx安装
安装需要的库
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install zlib1g zlib1g-dev
sudo apt-get install libssl0.9.8 libssl-dev



建立用户
sudo groupadd www
sudo useradd -g www www


安装命令
tar -zxvf nginx-0.6.36.tar.gz
cd nginx-0.6.36/
./configure --user=www --group=www --prefix=/opt/web/nginx636 --with-http_stub_status_module --with-http_ssl_module
make
sudo make install


sudo ln -s /opt/web/nginx636/sbin/nginx /sbin/nginx


启动与重启
启动:sudo nginx
关闭:sudo pkill nginx
重启:sudo kill -HUP `cat /opt/web/nginx636/logs/nginx.pid`
测试配置文件:sudo nginx -t

存放网站的路径
sudo mkdir -p /var/www/bbs.sends.cc
sudo chmod +w /var/www/bbs.sends.cc
sudo chown -R www:www /var/www/bbs.sends.cc


sudo chown -R www:www /opt/web/nginx636/logs


配置配置文件
sudo vi /opt/web/nginx636/conf/nginx.conf

以下仅供参考,请认真阅读官方wiki后跟据自身需要配置

/opt/web/nginx636/conf/nginx.conf
#user  nobody;

user www www;

worker_processes  4;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

worker_rlimit_nofile 65535;

events {
 use epoll;   
 worker_connections  65535;
}


http {
    
    #my config
    charset  utf-8;
    server_names_hash_bucket_size 128; 
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 8m; 

    tcp_nodelay on; 
   

    #fastcgi
    fastcgi_connect_timeout 60;
    fastcgi_send_timeout 180;
    fastcgi_read_timeout 180;
    fastcgi_buffer_size 128k;
    fastcgi_buffers 4 128k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;
    fastcgi_temp_path /dev/shm;

    #gzip
    gzip on;
    gzip_min_length  1k;
    gzip_buffers     4 8k;
    gzip_http_version 1.1;
    gzip_types       text/plain application/x-javascript text/css text/html application/xml; 
   

    #my config end


    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  45;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;


        index index.html index.htm index.php;
        root /var/www/bbs.sends.cc;




        if (-d $request_filename)
        {
                    rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
        }


        location ~ .*\.php?$
        {
                  #fastcgi_pass  unix:/tmp/php-cgi.sock; 
                  include fastcgi.conf;      
                  fastcgi_pass  127.0.0.1:20080;
                  fastcgi_index index.php;
        }



       location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
       {
                  expires      30d;
       }

       location ~ .*\.(js|css)?$
       {
                 expires      1h;
       }





        #charset koi8-r;


        log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
                                   '$status $body_bytes_sent "$http_referer" '
                                   '"$http_user_agent" $http_x_forwarded_for';


        access_log  logs/bbs.access.log  access;



        location /NginxStatus {
                        stub_status             on;
                        access_log              off;
                        auth_basic              "NginxStatus";
                }







        #location / {
        #    root   html;
        #    index  index.html index.htm;
        #}

        #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;
        #    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  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}


/opt/web/nginx636/conf/fastcgi.conf
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;
 
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;
 
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
 
fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;
 
# PHP only, required if PHP was built with –enable-force-cgi-redirect
#fastcgi_param  REDIRECT_STATUS    200;


开机启动所有此次安装的服务(加入rc.local)
sudo /etc/init.d/mysqld start
sudo /opt/web/nginx636/sbin/spawn-fcgi -a 127.0.0.1 -p 20080 -C 30 -u www -f /opt/web/php/bin/php-cgi
sudo /opt/web/nginx636/sbin/nginx


优化
sudo vi /etc/security/limits.conf

加入两行:
* hard nofile 65535
* soft nofile 65535

sudo vi /etc/sysctl.conf

尾部加入:
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 5000    65000

查看正确配置与否
sudo sysctl -p


Mysql安装
sudo groupadd mysql
sudo useradd -g mysql mysql


安装相关库
sudo apt-get install libncurses5-dev
sudo apt-get install libncursesw5-dev


安装命令
tar zxvf mysql-5.0.56.tar.gz
cd mysql-5.0.56
./configure --prefix=/opt/web/mysql --enable-assembler --with-mysqld-user=mysql --with-mysqld-ldflags=-all-static --with-client-ldflags=-all-static --with-extra-charsets=all --with-charset=utf8 --with-collation=utf8_general_ci --enable-thread-safe-client --without-debug --without-bench
make
sudo make install



sudo cp support-files/my-medium.cnf /etc/my.cnf
sudo cp support-files/mysql.server /etc/init.d/mysqld
sudo chmod 700 /etc/init.d/mysqld
sudo ./scripts/mysql_install_db --user=mysql



设置用户访问权限
cd /opt/web/mysql
sudo chown -R root .
sudo chown -R mysql var
sudo chown -R mysql var/.
sudo chown -R mysql var/mysql/.
sudo chgrp -R mysql .



启动设置
sudo /opt/web/mysql/bin/mysqld_safe --user=mysql &
sudo mysqladmin -uroot password newpassword
sudo mysql -uroot -pnewpassword


mysql>use mysql;
mysql>delete from user where password=""; #删除用于本机匿名连接的空密码帐号 
mysql>flush privileges;
mysql>quit 



PHP安装

安装lighttpd的spawn-fcgi用于控制php-cgi
tar zxvf spawn-fcgi-1.6.0.tar.gz
cd spawn-fcgi-1.6.0
./configure
sudo cp src/spawn-fcgi /opt/web/nginx636/sbin/


安装相关库
sudo apt-get install libxml2-dev
sudo apt-get install libcurl4-dev
sudo apt-get install libjpeg-dev
sudo apt-get install libpng3-dev
sudo apt-get install libfreetype6-dev
sudo apt-get install libxslt1-dev




安装命令
gunzip suhosin-patch-5.2.6-0.9.6.2.patch.gz
tar zxvf php-5.2.6.tar.gz
cd php-5.2.6
patch -p1 < ../suhosin-patch-5.2.6-0.9.6.2.patch
./configure --prefix=/opt/web/php --with-mysql=/opt/web/mysql --with-config-file-path=/opt/web/nginx636/conf --with-openssl=/usr --with-curl --with-libxml-dir --enable-soap --with-xsl --with-gd --with-jpeg-dir --with-zlib-dir  --with-freetype-dir --with-png-dir --enable-mbstring --with-iconv --enable-magic-quotes --enable-inline-optimization --enable-ftp --enable-fastcgi -with-mcrypt --enable-force-cgi-redirect --enable-short-tags --disable-debug --with-mysqli=/opt/web/mysql/bin/mysql_config
make
sudo make test
sudo make install


配置文件修改
sudo cp php.ini-dist /opt/web/nginx636/conf/php.ini
sudo vi /opt/web/nginx636/conf/php.ini


;default_charset = "iso-8859-1"
#在这行下面加一行
default_charset = "utf-8"


安装zendOptimizer and phpmyadmin
tar zxvf ZendOptimizer-3.3.3-linux-glibc23-x86_64.tar.gz
cd ZendOptimizer-3.3.3-linux-glibc23-x86_64
./install.sh

安装步骤略
tar zxvf phpMyAdmin-3.1.3-all-languages.tar.bz2
sudo cp –rf phpMyAdmin-3.1.3-all-languages /var/www/phpadmin
cd /var/www/phpadmin
cp config.sample.inc.php config.inc.php
sudo vi config.inc.php


phpMyadmin可以各人发挥了,每个人的配置可能不一样。
$cfg['blowfish_secret'] = 'cookie';(使用cookie加密)
$cfg['Servers'][$i]['connect_type'] = 'socket';(以socket方式连接MySQL)
$cfg['Servers'][$i]['compress'] = TRUE;
(启用压缩以提高速度,如果访问不正常请改回FALSE)
$cfg['Servers'][$i]['auth_type'] = 'cookie';(使用cookie进行身份验证)

安装结束后可以用 http://localhost/phpadmin 测试phpmyadmin

安装eAccelerator
tar jxvf eaccelerator-0.9.5.3.tar.bz2
cd eaccelerator-0.9.5.3
./configure -enable-eaccelerator=shared -with-php-config=/opt/web/php/bin/php-config
make
sudo make install


安装完后记住扩展所在地址,如:/opt/web/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so

sudo vi /opt/web/nginx636/conf/php.ini


插入配置,注意:必须[Zend]前加入
[eAccelerator]
zend_extension="/opt/web/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="32"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="0"
eaccelerator.shm_prune_period="0"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"


[Zend]

zend_extension_manager.optimizer=/usr/local/Zend/lib/Optimizer-3.3.3
zend_extension_manager.optimizer_ts=/usr/local/Zend/lib/Optimizer_TS-3.3.3
zend_optimizer.version=3.3.3
zend_extension=/usr/local/Zend/lib/ZendExtensionManager.so
zend_extension_ts=/usr/local/Zend/lib/ZendExtensionManager_TS.so



启动php-cgi
nginx636/sbin/spawn-fcgi -a 127.0.0.1 -p 20080 -C 30 -u www -g www –f /opt/web/php/bin/php-cgi


检查配置是否正确
sudo /opt/web/php/bin/php -v



作者
2
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics