1. 创建系统网站用户
建立www用户
/usr/sbin/groupadd www /usr/sbin/useradd -g www www
建立网站文件目录
mkdir -p /website/www chmod +w /website/www
赋予用户www网站文件目录权限
chown www:www /website/www -R
2. 编译安装PHP,Nginx,Spawn-fcgi等
安装Nginx所需的pcre库
unzip pcre-8.12.zip cd pcre-8.12/ ./configure make && make install cd ../
编译安装 PHP
tar zxvf php-5.3.2.tar.gz cd php-5.3.2/ ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap make ZEND_EXTRA_LIBS='-liconv' make install cp php.ini-development /usr/local/php/etc/php.ini cd ../
编译安装 Nginx
tar zxvf nginx-1.0.4.tar.gz cd nginx-1.0.4/ ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module make && make install cd ../
编译安装 Spawn-fcgi
tar zxvf spawn-fcgi-1.6.2.tar.gz cd spawn-fcgi-1.6.2 ./configure --prefix=/usr/local/spawn make && make install cd ../
编译安装 Memcache
tar zxvf memcache-2.2.5.tgz cd memcache-2.2.5 /usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config make && make install cd ../
编译安装 PDO_MYSQL
tar zxvf PDO_MYSQL-1.0.2.tgz cd PDO_MYSQL-1.0.2/ /usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/local/mysql make && make install cd ../
编译安装 eaccelerator
unzip eaccelerator-0.9.6.1.zip cd eaccelerator-0.9.6.1/ /usr/local/php/bin/phpize ./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config make && make install cd ../
2. 修改配置文件
修改php.ini
查找/usr/local/php/etc/php.ini中的extension_dir = "./"
修改为extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"
并在此行后增加以下几行,然后保存:
extension = "memcache.so"
extension = "pdo_mysql.so"
再查找;cgi.fix_pathinfo=1
修改为cgi.fix_pathinfo=0,防止Nginx文件类型错误解析漏洞。
(1)Vi 内查找替换方式
:1,$s/; extension_dir = ".\/"/extension_dir = "\/usr\/local\/php\/lib\/php\/extensions\/no-debug-non-zts-20060613\/"^Mextension = "memcache.so"^Mextension = "pdo_mysql.so"/ PS:^M 为在Vi中用Ctrl+V+M获得 :1,$s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/
(2)Shell 方式
sed -i 's#; extension_dir = "./"#extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"\nextension = "memcache.so"\nextension = "pdo_mysql.so"#' /usr/local/php/etc/php.ini sed -i "s#;cgi.fix_pathinfo=1#cgi.fix_pathinfo=0#" /usr/local/php/etc/php.ini
3. 配置eAccelerator加速PHP
建立 eAccelerator 缓存目录
mkdir -p /website/eaccelerator_cache
再次修改 php.ini 配置文件
vi /usr/local/php/etc/php.ini
按shift+g键跳到配置文件的最末尾,加上以下配置信息
[eaccelerator] zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so" eaccelerator.shm_size="64" eaccelerator.cache_dir="/website/eaccelerator_cache" eaccelerator.enable="1" eaccelerator.optimizer="1" eaccelerator.check_mtime="1" eaccelerator.debug="0" eaccelerator.filter="" eaccelerator.shm_max="0" eaccelerator.shm_ttl="3600" eaccelerator.shm_prune_period="3600" eaccelerator.shm_only="0" eaccelerator.compress="1" eaccelerator.compress_level="9"
4. 配置Nginx:
创建Nginx日志目录
mkdir -p /website/logs chmod +w /website/logs chown -R www:www /website/logs
创建Nginx配置文件
rm -f /usr/local/nginx/conf/nginx.conf vi /usr/local/nginx/conf/nginx.conf
内容如下
user www www; worker_processes 8; error_log /website/logs/nginx_error.log crit; pid /usr/local/nginx/nginx.pid; #Specifies the value for maximum file descriptors that can be opened by this process. worker_rlimit_nofile 65535; events { use epoll; worker_connections 65535; } http { include mime.types; default_type application/octet-stream; #charset gb2312; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 8m; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; #limit_zone crawler $binary_remote_addr 10m; server { listen 80; server_name www.dapang.com; index index.html index.htm index.php; root /website/www/dapang; location ~ .*\.(php|php5)?$ { #fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fcgi.conf; } #log_format wwwlogs '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" $http_x_forwarded_for'; #access_log /website/logs/wwwlogs.log wwwlogs; } }
创建fcgi.conf文件
vi /usr/local/nginx/conf/fcgi.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;
5. 整合启动测试
启动Nginx
ulimit -SHn 65535 /usr/local/nginx/sbin/nginx
启动Spawn-fcgi
/usr/local/spawn/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www -g www -f /usr/local/php/bin/php-cgi
在website的www目录下创建一个dapang目录,并加入测试文件info.php
mkdir -p /website/www/dapang vi /website/www/dapang/info.php
info.php
<?php echo phpinfo(); ?>