首頁  >  文章  >  運維  >  Linux+Nginx+Php如何架設高效能WEB伺服器

Linux+Nginx+Php如何架設高效能WEB伺服器

WBOY
WBOY轉載
2023-05-20 23:19:551180瀏覽

nginx ("engine x") 是一個高效能的 http 和 反向代理 伺服器,也是 imap/pop3/smtp 代理伺服器 。 nginx 是由 igor sysoev 為俄羅斯訪問量第二的 rambler.ru 站點開發的,它已經在該站點運行超過兩年半了。 igor 將原始碼以類bsd許可證的形式發布。儘管還是測試版,但是,nginx 已經因為它的穩定性、豐富的功能集、範例設定檔和低系統資源的消耗而聞名了。

安裝步驟

一、 編譯安裝php5.2.9所需的支援函式庫

tar zxvf libiconv-1.13.tar.gz
cd libiconv-1.13/
./configure --prefix=/usr/local
make
make install
cd ../

tar zxvf libmcrypt-2.5.8.tar .gz
cd libmcrypt-2.5.8/
./configure
make
make install
/sbin/ldconfig
cd libltdl/
./configure --enable- ltdl-install
make
make install
cd ../../

tar zxvf mhash-0.9.9.9.tar.gz
#cd mhash-0.9.9.9/
./configure
make
make install
cd ../

#ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr /lib/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib/ libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0. 1 /usr/lib/libmhash.so.2.0.1

tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
/sbin/ldconfig
# ./configure
make
make install
cd ../


#二、編譯安裝mysql 5.1.34擴充函式庫

# /usr/sbin/groupadd mysql
/usr/sbin/useradd -g mysql mysql
tar zxvf mysql-5.1.34.tar.gz
cd mysql-5.1.34/
./configure --prefix=/usr/local/webserver/mysql/ --enable-assembler --with-extra-charsets=complex --enable-thread-safe-client --with-big-tables --with-readline -- with-ssl --with-embedded-server --enable-local-infile --with-plugins=innobase
make && make install
chmod w /usr/local/webserver/mysql
chown -r mysql:mysql /usr/local/webserver/mysql
cd ../

mysql的安裝此處就省略


##三、編譯安裝php(fastcgi模式)

tar zxvf php-5.2.9.tar.gz

gzip -cd php-5.2.9-fpm-0.5.10.diff.gz | patch -d php-5.2 .9 -p1
cd php-5.2.9/
./configure --prefix=/usr/local/webserver/php --with-config-file-path=/usr/local/webserver/php /etc --with-mysql=/usr/local/webserver/mysql --with-mysqli=/usr/local/webserver/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
make zend_extra_libs='-liconv'
make install
cp php.ini-dist /usr/local/webserver/php /etc/php.ini
cd ../


四、修改php-fpm設定檔

rm -f /usr/local/ webserver/php/etc/php-fpm.conf

vi /usr/local/webserver/php/etc/php-fpm.conf
##輸入以下內容:


<設定>
<節名稱=“global_options”>
<值名稱=“pid_file”>/ usr /local/webserver/php/logs/php-fpm.pid
/usr/local/webserver/php/logs/php-fpm.log ;
通知值>
10值>
1m
<值名稱=“process_control_timeout”>5s
<值名稱=“daemonize”>是



default
127.0.0.1:9000值>

-1值>














<值名稱=「群組」>
<值名稱=「模式」>0666
< /value>
<值名稱= “php_defines”>
<值名稱=“sendmail_path”>/usr/sbin/sendmail -t -i
<值名稱="display_errors">1

<值名稱=“用戶”>www
<值名稱=“組”> ​​www

<值名稱=“pm”>
<值名稱=“style”>靜態
<值名稱=“max_children”>128< ;/值>
<值名稱=“apache_like”>
<值名稱=“startservers”>20
<值名稱=“minspareservers”>5< /值>
<值名稱=“maxspareservers”>35


<值名稱=“request_terminate_timeout”> 0s
<值名稱=“request_slowlog_timeout”>0s
<值名稱=“slowlog”> logs/slow.log
<值名稱=“rlimit_files”>51200
<值名稱=“rlimit_core”>0
<值名稱=“chroot ”>
<值名稱=“chdir”>
<值名稱=“catch_workers_output”>是
<值名稱=“max_requests”>500< /值>

<值名稱=“allowed_clients”>127.0.0.1

<值名稱=“環境”><值名稱=“主機名稱”> $hostname /usr/local/bin:/usr/bin:/bin

/tmp

<值名稱=“tmpdir”>/tmp
<值名稱=“temp”>/tmp

<值name="ostype">$ostype

$machtype
2

#



##五、建立www用戶群組及www用戶

##/usr/sbin/groupadd www
#/ usr/sbin/useradd -g www www



#六、編譯安裝nginx

##tar zxvf nginx-0.6.35.tar.gz

#cd nginx-0.6.35/預設安裝:./configure

make && make install

預設情況接下來,nginx會被安裝在/usr/local/nginx。透過設定編譯選項,你可以改變這個設定。

指定編譯選項安裝,如:

./configure --user= www --group=www --prefix=/usr/local/webserver/nginx --with -http_stub_status_module --with-http_ssl_module

make && make install###cd ../######## #七、建立fcgi.conf檔案#########rm -f /###rm -f / usr/local/webserver/nginx/conf/fcgi.conf###vi /usr/local/webserver/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 th ;

fastcgi_param script_filename $document_root$fastcgi_script_name;

fastcgi_param script_name $fastcgi_script_name;

fastcgi_param request_uri $refast_uri;
1_param _param server_protocol $server_protocol;

fastcgi_param remote_addr $remote_addr;
fastcgi_param remote_port $remote_port;
fastcgi_param server_addrserver_addr_#_fast_fastcgi_#fastcgi_param server_addrserver_addr_#_fast_fastcgi_#_fastcgi_para

# 僅適用於php,如果使用--enable-force-cgi-redirect 建構php,則需要

fastcgi_param redirect_status 200;



#八、編輯nginx設定檔


rm -f /usr/local/webserver/nginx/conf/nginx.conf

#vi /usr/local/webserver/nginx/conf/nginx.conf


#輸入以下內容:user www www;

worker_processes 8;

error_log /usr/local/webserver/nginx/logs/logs/nginx_error.log crit;
pid /usr/ local/webserver/ nginx/logs/nginx.pid;

worker_rlimit_nofile 51200;

events {

use epoll;

worker_connections 51200;
}
##http {
##http {
include mime.types ;
default_type application/octet-stream;
charset utf-8;

server_names_hash_bucket_size 128;

client_header_buffer_size 32k;fclis_clis_s; ;


sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;

fastcgi_connect_timeout 300;
fastcgi_send_#fastcgi_connect_timeout 300;
fastcgi_send_時間##fastcgi_buffer_size 64k ;
fastcgi_buffers 8 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;

gzip;

#g #gzip_http_version 1.0;

gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;

#limit_zonecrawler $binary_remoler_addr 100個網站設置,網站資料夾必須放在nginx的html目錄,否則不支援多網站
server {
listen 80;

server_name www.v-ec.com;

root /usr/ local/webserver /nginx/html/www.v-ec.com;
#index index.php index.html;

location ~ .*\.(php|php5)?$ {
#fastcgi_pass unix :/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;

include fcgi.conf;

}



#.位置~ .* \.(gif|jpg|jpeg|png|bmp|swf)$ {
過期30d;
}

位置~ .*\.(js|css)?$ {
過期6 小時;
}

log_format veclog '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent " $http_x_forwarded_for';
access_log logs/vec.log veclog;
}
#多個網站設置,網站資料夾必須放在nginx的html目錄下,否則不支援多網站

server {

listen 80;
server_name www.w3cgroup.com;
root /usr/local/webserver/nginx/html/www.w3cgroup.com;

index index.php index.html;


位置~ .*\.(php|php5)?$ {
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index 索引。php;
include fcgi.conf;
}

位置~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
過期30 天;
}

位置~ .*\.(js|css)?$ {
expires 6h;
}

log_format w3cclog '$remote_addr - $remote_user [$time_local] " $request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log 日誌/w3c.log w3clog;## }
}

##九、設定開機自動啟動nginx php



##vi /etc/rc.local

在增加下增加以下內容:

ulimit -shn 51200
/usr/local/webserver/php/sbin/php-fpm start
/usr/local/webserver/nginx/sbin/nginx

十、最佳化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

>生效:
/sbin/sysctl –p

做到這裡,可以重啟重啟linux了,不出意外的話應該可以順利開啟nginx

#平滑重啟nginx


ps 輔助 | grep nginx
找到nginx的主程序,假設為:5800,然後kill
kill –hup 5800

平滑重啟php

/usr/local/webserver/php/sbin /php-fpm restart

php-fpm的其他指令:start |停止|退出 |重啟|重新加載 |對數旋轉

以上是Linux+Nginx+Php如何架設高效能WEB伺服器的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除