2014-02-25 08:42:01
来 源
IT技术网
Nginx
本文介绍CentOS6.4系统安装配置Nginx1.2.4+PHP5.3.17+MySQL高性能web,希望对于初学Nginx服务器相关的朋友有帮助,更多Nginx安装、配置、报错处理等资源请本站内搜索。。

1.安装操作系统

附:CentOS 6.3下载地址

32位:http://mirror.centos.org/centos/6.3/isos/i386/CentOS-6.3-i386-bin-DVD1to2.torrent

64位:http://mirror.centos.org/centos/6.3/isos/x86_64/CentOS-6.3-x86_64-bin-DVD1to2.torrent

说明:

1、CentOS 6.3系统镜像有两个,安装系统只用到第一个镜像即CentOS-6.3-i386-bin-DVD1.iso(32位)或者CentOS-6.3-x86_64-bin-DVD1.iso(64位),第二个镜像是系统自带软件安装包

2、安装CentOS 6.3系统的计算机内存必须等于或者大于628M(最小内存628M),才能启用图形安装模式

3、CentOS 6.3文本安装模式不支持自定义分区,建议使用图形安装模式安装

4、CentOS 6.3的系统安装方式分为:图形安装模式和文本安装模式

5、CentOS 6.3的系统运行方式分为:带图形界面、可以用鼠标操作的图形化方式和不带图形界面、直接用命令行操作的文本方式(具体的系统运行方式,可以在系统安装的过程中自定义选择)

2.升级操作系统

安装完毕后,按照实际网络环境配置IP 网关 DNS等,确认可以正常访问公网后,命令行执行yum -y update升级操作系统。

1.安装nginx第三方更新源

根据操作系统从上面下载正确的release rpm包,将rpm上传到服务器,使用root用户命令行终端执行rpm -ivh nginx-release-centos-6-0.el6.ngx.noarch.rpm (根据实际安装包名称修改rpm名)

2.安装nginx

使用root用户登录服务器,在命令行终端执行yum -y install nginx

3.配置nginx

配置/etc/nginx/nginx.conf(此处可配置优化参数)

参考如下:

#######################################################################

#

# This is the main Nginx configuration file.

#

# More information about the configuration options is available on

# * the English wiki – http://wiki.nginx.org/Main

# * the Russian documentation – http://sysoev.ru/nginx/

#

#######################################################################

#———————————————————————-

# Main Module – directives that cover basic functionality

#

# http://wiki.nginx.org/NginxHttpMainModule

#

#———————————————————————-

user nginx;

worker_processes 2;

error_log /var/log/nginx/error.log;

#error_log /var/log/nginx/error.log notice;

#error_log /var/log/nginx/error.log info;

pid /var/run/nginx.pid;

worker_rlimit_nofile 65535;

#———————————————————————-

# Events Module

#

# http://wiki.nginx.org/NginxHttpEventsModule

#

#———————————————————————-

events {

use epoll;

worker_connections 65535;

}

#———————————————————————-

# HTTP Core Module

#

# http://wiki.nginx.org/NginxHttpCoreModule

#

#———————————————————————-

http {

include /etc/nginx/mime.types;

default_type application/octet-stream;

charset utf-8;

log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘

‘$status $body_bytes_sent “$http_referer” ‘

‘”$http_user_agent” “$http_x_forwarded_for”‘;

server_names_hash_bucket_size 128;

client_header_buffer_size 16k;

large_client_header_buffers 4 16k;

client_max_body_size 8m;

sendfile on;

tcp_nopush on;

keepalive_timeout 60;

tcp_nodelay on;

#以下为php fastcgi优化配置

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;

fastcgi_temp_path /usr/share/nginx;

fastcgi_cache_path /usr/share/nginx/ngx_fcgi_cache levels=2:2 keys_zone=ngx_fcgi_cache:128m inactive=1d max_size=256m;

fastcgi_cache_valid 200 301 302 1d;

fastcgi_cache_use_stale error timeout invalid_header http_500;

fastcgi_cache_key $request_method://$host$request_uri;

fastcgi_cache ngx_fcgi_cache;

open_file_cache max=65535 inactive=20s;

open_file_cache_min_uses 1;

open_file_cache_valid 30s;

#以下为gzip优化配置

gzip on;

gzip_http_version 1.0;

gzip_comp_level 2;

gzip_min_length 1k;

gzip_buffers 4 16k;

gzip_types text/plain application/xhtml+xml text/css application/xml application/xml+rss text/javascript application/javascript application/x-javascr$

gzip_proxied any;

gzip_disable “MSIE [1-6].”;

# Load config files from the /etc/nginx/conf.d directory

# The default server is in conf.d/default.conf

include /etc/nginx/conf.d/*.conf;

}

配置/etc/nginx/conf.d/default.conf

参考如下:

server {

listen 80;

server_name test.com ;

root /usr/share/nginx/html;

index index.html index.php;

# autoindex on;

access_log off;

#以下为nginx rewrite策略

location / {

if ( $args ~ “mosConfig_[a-zA-Z_]{1,21}(=|%3d)” ) {

set $args “”;

rewrite ^.*$ http://$host/index.php last;

return 403;}

if ( $args ~ “base64_encode.*(.*)”) {

set $args “”;

rewrite ^.*$ http://$host/index.php last;

return 403;}

if ( $args ~ “(<|%3C).*script.*(>|%3E)”) {

set $args “”;

rewrite ^.*$ http://$host/index.php last;

return 403;}

if ( $args ~ “GLOBALS(=|[|%[0-9A-Z]{0,2})”) {

set $args “”;

rewrite ^.*$ http://$host/index.php last;

return 403;}

if ( $args ~ “_REQUEST(=|[|%[0-9A-Z]{0,2})”) {

set $args “”;

rewrite ^.*$ http://$host/index.php last;

return 403;}

if (!-e $request_filename) {

rewrite ^(.*)$ /index.php?s=$1 last;

break;}

}

error_page 404 /404.html;

location = /404.html {

root /usr/share/nginx/html;

}

# redirect server error pages to the static page /50x.html

#

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root /usr/share/nginx/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

#以下为nginx php处理策略

location ~ .php$ {

root /usr/share/nginx/html;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fastcgi_params;

}

#以下为提高访问速度缓存机制

location ~ .*.(ico|flv||gif|jpg|jpeg|png|bmp|swf)$

{

expires 30d;

}

location ~ .*.(js|css)?$

{

expires 12h;

}

# deny access to .htaccess files, if Apache’s document root

# concurs with nginx’s one

#

location ~ /.ht {

deny all;

}

}

1.安装第三方更新源

使用root登录服务器后,在命令行终端执行yum -y install yum-priorities升级yum

然后执行以下命令加载第三方源

rpm -Uvh http://mirrors.ustc.edu.cn/fedora/epel/6/x86_64/epel-release-6-7.noarch.rpm

rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

(注意以上文件均为centos 6 对应版本,其他版本操作系统可访问相应网站自行查找)

2.安装php

yum –enablerepo=remi install php php-fpm php-cli php-pdo php-mysql php-mcrypt php-mbstring php-gd php-tidy php-xml php-xmlrpc php-pear php-devel

注意本方法安装的php为官方最新稳定版本

3.安装php加速模块memcache和eaccelerator

yum –enablerepo=remi install  php-pecl-memcache php-eaccelerator

4.配置PHP

php配置文件默认为/etc/php.ini php-fpm.conf以及/etc/php.d和/etc/php-fpm.d目录下的所有文件

正常情况下无需更改默认配置

只更改/etc/php-fpm.d/www.conf内的以下字段即可

user = nginx

group = nginx

1.安装mysql

使用root登录服务器后,在命令行终端执行 yum -y install mysql mysql-server

在nginx网站root目录 默认为/usr/share/nginx/html

在该目录下使用命令行终端执行vi index.php

然后按键盘i键 输入以下内容:

<?php

session_start();

$_SESSION['time'] =date(“Y:m:d:H:s”,time());

echo “time”.”<font color=red>”.$_SESSION['time'].”</font>”.”<br>”;

echo “realserver ip address”.”<font color=red>”.$_SERVER['SERVER_ADDR'].”</font>”.”<br>”;

echo “local address”.”<font color=red>”.$_SERVER['SERVER_NAME'].”</font>”.”<br>”;

echo “SESSIONNAME”.”<font color=red>”.session_name().”</font>”.”<br>”;

echo “SESSIONID”.”<font color=red>”.session_id().”</font>”.”<br>”;

phpinfo();

?>

然后按键盘:wq保存退出

使用浏览器访问网站,如果可以正常显示当前session信息和phpinfo信息,说明正常。

如果提示404等无法访问页面或者访问页面不存在、以及直接显示源代码表示php异常,请确认nginx配置中的root参数目录路径是否正确。

声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。