Aliyun 服务器apache切换nginx

我的服务器内存是512M的,用的apache,内存常常爆满。呵呵,看着心急啊,各位都懂得,于是听说nginx是轻量级的服务器,遂把apache换成了nginx。

 

第一步,停止apache:

sudo service httpd stop

 

第二步,安装nginx:

sudo yum install nginx

Then start nginx:

sudo service nginx start

现在访问你的ip应该可以看到nginx的欢迎界面了。
nginx的默认web地址是:

/usr/share/nginx/html

 

第三步,你需要对你的nginx服务器做些修改,以便更好的服务于你:(如果你的服务器是多核的)

sudo vi /etc/nignx/nginx.conf
修改如下内容
worker_processes  1; change to worker_processes  2;

 

第四步,使nginx支持php

1、你首先要安装php-fpm:

sudo yum install php-fpm

Then:

sudo php-fpm start

2、然后配置nginx:

sudo vi /etc/nginx/conf.d/default.conf

配置如下(我的web目录是/var/www/html):

#
# The default server
#
server {
    listen       80;
    server_name  _;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   /var/www/html;
        index  index.php index.html index.htm;
	if (!-e $request_filename)
	{
	    rewrite ^/(.+)$ /index.php last;
	}

    }

    error_page  404              /404.html;
    location = /404.html {
        root   /var/www/html;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /var/www/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #	root html;
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /var/www/html;
        try_files $uri =404;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny  all;
    }
}

第五步,配置网站:
1、建立目录

sudo mkdir /etc/ngnix/vhosts

2、建立你的网站的配置文件

sudo vi /etc/nginx/vhosts/your_domain.com.conf

3.添加如下配置:(我的web目录是/var/www/html)

server {
        listen  80;
        server_name your_domain.com www.your_domain.com;

        access_log  /var/log/nginx/your_domain.access.log;

        location / {
            root   /var/www/html/your_domain.com;
            index  index.php index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }

       # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/www/html/your_domain/$fastcgi_script_name;
            include        fastcgi_params;
        }

        location ~ /\.ht {
            deny  all;
        }
}

4、将这个配置文件添加在/etc/nginx/nginx.conf

include /etc/nginx/vhosts/*;

广告


JackSun

JackSun

I'm a coder.

You may also like...

No Responses

  1. 夏初舞 says:

    这么强,支持博主,佩服

  2. jerry Wang says:

    不错

Leave a Reply

Your email address will not be published.