nginx安裝及配置支援php的教程(全)

來源:互聯網
上載者:User

標籤:

本文的實驗環境為:Centos4.5,nginx版本為:nginx-0.7.26 

pcre-7.8.tar.gz  Regex:ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 
nginx-0.7.26.tar:http://www.nginx.net/  
php-5.2.6.tar.bz2:http://www.php.net/releases/ 
php-5.2.6-fpm-0.5.9.diff.gz 
php-fpm是為PHP打的一個FastCGI管理補丁,可以平滑變更php.ini配置而無需重啟php-cgi:http://php-fpm.anight.org/ 
註:PHP的版本要和fpm的版本一致mysql-5.0.67.tar.gz 
Discuz!_6.0.0_SC_UTF8.zip

1、安裝pcre

 

複製代碼 代碼如下:
# tar -zxvf pcre-7.8.tar.gz 
# cd pcre-7.8 
# ./configure 
# make && make install

 

2、安裝Nginx

 

複製代碼 代碼如下:
# tar -zxvf nginx-0.7.26.tar.gz 
# cd nginx-0.7.26 
# ./configure --prefix=/usr/local/nginx 
# make && make install
啟動nginx# /usr/local/nginx/sbin/nginx 
停止nginx# kill -QUIT `cat /usr/local/nginx/logs/nginx.pid` 
重啟nginxkill -HUP `cat /usr/local/nginx/logs/nginx.pid` 
添加到自啟動# echo "/usr/local/nginx/sbin/nginx">>/etc/rc.local

 

3、安裝mysql

 

複製代碼 代碼如下:
# tar -zxvf mysql-5.0.67.tar.gz 
# cd mysql-5.0.67 
# groupadd mysql 
# useradd -g mysql -s /sbin/nologin -M mysql 
# ./configure --prefix=/usr/local/mysql --with-charset=gbk --with-extra-charset=all --enable-hread-safe-client
--enable-local-infile --with-low-memory 
# make && make install 
# cp support-files/my-medium.cnf  /etc/my.cnf 
# chown -R mysql.mysql /usr/local/mysql/ 
# /usr/local/mysql/bin/mysql_install_db --user=mysql
# chown -R root.root /usr/local/mysql/ 
# chown -R mysql.mysql /usr/local/mysql/var/
啟動資料庫服務,並添加到自啟動:
# /usr/local/mysql/bin/mysqld_safe --user=mysql & 
#cp  support-files/mysql.server  /etc/rc.d/init.d/mysqld 
#chmod  755  /etc/rc.d/init.d/mysqld
加入自動啟動服務隊列:
#chkconfig --add mysqld 
#chkconfig  --level  345  mysqld  on添加root密碼 
# /usr/local/mysql/bin/mysqladmin -u root password "123456" 
測試一下:# /usr/local/mysql/bin/mysql -u root -p輸入密碼:123456,看能不能進入到資料庫
配置庫檔案搜尋路徑:
# echo "/usr/local/mysql/lib/mysql">>/etc/ld.so.conf 
# ldconfig 
# ldconfig -v 
添加/usr/local/mysql/bin到環境變數PATH中 
#echo "export PATH=$PATH:/usr/local/mysql/bin">>/etc/profile 
#source /etc/profile

 

4、安裝PHP

這裡產生的是可執行檔,和apache的不一樣,和apache結合的時候產生的是動態庫

複製代碼 代碼如下:
# tar -jxvf php-5.2.6.tar.bz2 
# gzip -cd php-5.2.6-fpm-0.5.9.diff.gz |patch -d php-5.2.6 -p1 
# cd php-5.2.6 
# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --enable-fastcgi --enable-fpm  
--with-config-file-path=/usr/local/php/etc --enable-force-cgi-redirect 
# make && make install 
# cp php.ini-recommended /usr/local/php/etc/php.ini 
# vi /usr/local/php/php-fpm.conf

 

(1)<value name="listen_address">127.0.0.1:9000</value>修改為<value name="listen_address">IP:9000</value> 
//本機就用預設的127.0.0.1 

(2)下面這兩行去掉注釋並修改
<value name="sendmail_path">/usr/sbin/sendmail -t -i</value>
<value name="display_errors">1</value>
(3)<value name="user">nobody</value>   //去注釋 
(4)<value name="group">nobody</value>  //去注釋 
(5)<value name="allowed_clients">127.0.0.1</value>   //允許串連的PC,本機就用127.0.0.1
啟動php-fpm# /usr/local/php/sbin/php-fpm start添加到自啟動# echo "/usr/local/php/sbin/php-fpm start">>/etc/rc.local

5、修改Linux Nginx的設定檔,支援PHP

 

複製代碼 代碼如下:
# vi /usr/local/nginx/conf/nginx.conf 
user  nobody; 
worker_processes  8; 
pid  /usr/local/nginx/logs/nginx.pid; 
worker_rlimit_nofile 1024; 
events 
{use epoll; 
worker_connections 1024;} 
http{ 
include   mime.types; 
default_type  application/octet-stream; 
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; 

server { 
listen   80; 
server_name  www.abcdefg.com; 
root   /var/www/blog; 
index  index.html index.htm index.php; 
location ~ .*\.(php|php5)?$ { 
root   html; 
fastcgi_pass   127.0.0.1:9000; 
fastcgi_index  index.php; 
fastcgi_param  SCRIPT_FILENAME  /var/www/blog$fastcgi_script_name; 
includefastcgi_params;} 
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ 
{expires  30d;} 
location ~ .*\.(js|css)?$ 
{expires  1h;}
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  /var/logs/access.log  access;}}

 

註:server部分為PHP虛擬機器主機127.0.0.1:9000為fastcgi的PC,我這裡用的本機/var/www/blog$fastcgi_script_name; 為PHP網頁儲存的目錄測試組態檔案:
# /usr/local/nginx/sbin/nginx -t

6、最佳化Linux核心參數
# 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 = 500065000

 

使配置立即生效:# /sbin/sysctl -p。

nginx安裝及配置支援php的教程(全)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.