搜索
首页运维NginxLinux+Nginx+Php如何架设高性能WEB服务器

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

输入以下内容:




/usr/local/webserver/php/logs/php-fpm.pid
/usr/local/webserver/php/logs/php-fpm.log
notice
10
1m
5s
yes



default
127.0.0.1:9000

-1


0666


/usr/sbin/sendmail -t -i
1

www
www

static
128

20
5
35


0s
0s
logs/slow.log
51200
0


yes
500
127.0.0.1

$hostname
/usr/local/bin:/usr/bin:/bin
/tmp
/tmp
/tmp
$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 /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 $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;


八、编辑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 {
include mime.types;
default_type application/octet-stream;
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;

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 8 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;
#多个站点设置,站点文件夹必须放在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;
}

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

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

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;

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

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

location ~ .*\.(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 logs/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

做到这里,可以reboot重启linux了,不出意外的话应该可以顺利开启nginx

平滑重启nginx

ps aux | grep nginx
找到nginx的master process,假如为:5800,然后kill
kill –hup 5800

平滑重启php

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

php-fpm的其他命令:start | stop | quit | restart | reload | logrotate

以上是Linux+Nginx+Php如何架设高性能WEB服务器的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:亿速云。如有侵权,请联系admin@php.cn删除
NGINX单元:架构及其工作原理NGINX单元:架构及其工作原理Apr 23, 2025 am 12:18 AM

NGINXUnit通过其模块化架构和动态重配置功能提高了应用的性能和可管理性。1)模块化设计包括主控进程、路由器和应用进程,支持高效管理和扩展。2)动态重配置允许在运行时无缝更新配置,适用于CI/CD环境。3)多语言支持通过动态加载语言运行时实现,提升了开发灵活性。4)高性能通过事件驱动模型和异步I/O实现,即使在高并发下也保持高效。5)安全性通过隔离应用进程提高,减少应用间相互影响。

使用NGINX单元:部署和管理应用程序使用NGINX单元:部署和管理应用程序Apr 22, 2025 am 12:06 AM

NGINXUnit可用于部署和管理多种语言的应用。1)安装NGINXUnit。2)配置它以运行不同类型的应用,如Python和PHP。3)利用其动态配置功能进行应用管理。通过这些步骤,你可以高效地部署和管理应用,提升项目效率。

NGINX与Apache:Web服务器的比较分析NGINX与Apache:Web服务器的比较分析Apr 21, 2025 am 12:08 AM

NGINX更适合处理高并发连接,而Apache更适合需要复杂配置和模块扩展的场景。 1.NGINX以高性能和低资源消耗着称,适合高并发。 2.Apache以稳定性和丰富的模块扩展闻名,适合复杂配置需求。

NGINX单元的优势:灵活性和性能NGINX单元的优势:灵活性和性能Apr 20, 2025 am 12:07 AM

NGINXUnit通过其动态配置和高性能架构提升应用的灵活性和性能。1.动态配置允许在不重启服务器的情况下调整应用配置。2.高性能体现在事件驱动和非阻塞架构以及多进程模型上,能够高效处理并发连接和利用多核CPU。

NGINX与Apache:性能,可伸缩性和效率NGINX与Apache:性能,可伸缩性和效率Apr 19, 2025 am 12:05 AM

NGINX和Apache都是强大的Web服务器,各自在性能、可扩展性和效率上有独特的优势和不足。1)NGINX在处理静态内容和反向代理时表现出色,适合高并发场景。2)Apache在处理动态内容时表现更好,适合需要丰富模块支持的项目。选择服务器应根据项目需求和场景来决定。

终极摊牌:nginx vs. apache终极摊牌:nginx vs. apacheApr 18, 2025 am 12:02 AM

NGINX适合处理高并发请求,Apache适合需要复杂配置和功能扩展的场景。1.NGINX采用事件驱动、非阻塞架构,适用于高并发环境。2.Apache采用进程或线程模型,提供丰富的模块生态系统,适合复杂配置需求。

nginx行动:示例和现实应用程序nginx行动:示例和现实应用程序Apr 17, 2025 am 12:18 AM

NGINX可用于提升网站性能、安全性和可扩展性。1)作为反向代理和负载均衡器,NGINX可优化后端服务和分担流量。2)通过事件驱动和异步架构,NGINX高效处理高并发连接。3)配置文件允许灵活定义规则,如静态文件服务和负载均衡。4)优化建议包括启用Gzip压缩、使用缓存和调整worker进程。

NGINX单元:支持不同的编程语言NGINX单元:支持不同的编程语言Apr 16, 2025 am 12:15 AM

NGINXUnit支持多种编程语言,通过模块化设计实现。1.加载语言模块:根据配置文件加载相应模块。2.应用启动:调用语言运行时执行应用代码。3.请求处理:将请求转发给应用实例。4.响应返回:将处理后的响应返回给客户端。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具