nginx服务器安装记录

来源:互联网 发布:sql语句更改表名 编辑:程序博客网 时间:2024/06/05 03:01

安装文件为:

         Nginx: nginx-1.4.2  稳定版

         Php:php-5.5.4   其中已经内置支持fastcgi了,不用在打补丁

 

首先配置防火墙

不在叙述

 

安装nginx

安装必要补丁

# yum -y install pcre-devel opensslopenssl-devel gcc-c++

 

安装前优化

减小Nginx编译后的文件大小
在编译Nginx时,默认以debug模式进行,而在debug模式下会插入很多跟踪和ASSERT之类的信息,编译完成后,一个Nginx要有好几兆字节。在编译前取消Nginx的debug模式,编译完成后Nginx只有几百千字节,因此可以在编译之前,修改相关源码,取消debug模式,具体方法如下:
在Nginx源码文件被解压后,找到源码目录下的auto/cc/gcc文件,在其中找到如下几行:

1. # debug  

2. CFLAGS=”$CFLAGS -g”  

注释掉或删掉这两行,即可取消debug模式。

利用TCMalloc优化Nginx的性能
TCMalloc的全称为Thread-CachingMalloc,是谷歌开发的开源工具“google-perftools”中的一个成员。与标准的glibc库的malloc相比,TCMalloc库在内存分配效率和速度上要高很多,这在很大程度上提高了服务器在高并发情况下的性能,从而降低系统负载。下面简单介绍如何为Nginx添加TCMalloc库支持。
要安装TCMalloc库,需要安装libunwind(32位操作系统不需要安装)和google-perftools两个软件包,libunwind库为基于64位CPU和操作系统的程序提供了基本函数调用链和函数调用寄存器功能。下面介绍利用TCMalloc优化Nginx的具体操作过程:

1.安装libunwind
可以从http://download.savannah.gnu.org/releases/libunwind下载相应的libunwind版本,这里下载的是libunwind-0.99-alpha.tar.gz,安装过程如下:
 

1.  [root@localhost home]#tar zxvf libunwind-0.99-alpha.tar.gz  
2.  [root@localhost home]# cd libunwind-0.99-alpha/  
3.  [root@localhost libunwind-0.99-alpha]#CFLAGS=-fPIC ./configure  
4.  [root@localhost libunwind-0.99-alpha]#make CFLAGS=-fPIC  
5.  [root@localhost libunwind-0.99-alpha]#make CFLAGS=-fPIC install  

2.安装google-perftools
可以从http://google-perftools.googlecode.com下载相应的google-perftools版本,这里下载的是google-perftools-1.8.tar.gz,安装过程如下:
 

1.  [root@localhost home]#tar zxvf google-perftools-1.8.tar.gz  
2.  [root@localhost home]#cd google-perftools-1.8/  
3.  [root@localhost google-perftools-1.8]# ./configure  
4.  [root@localhost google-perftools-1.8]#make && make install  
5.  [root@localhost google-perftools-1.8]#echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf  
6.  [root@localhost google-perftools-1.8]# ldconfig  

至此,google-perftools安装完成。

3.重新编译Nginx
为了使Nginx支持google-perftools,需要在安装过程中添加“–with-google_perftools_module”选项重新编译Nginx,安装代码如下:
 

1.  [root@localhostnginx-0.7.65]#./configure \  
2.  >--with-google_perftools_module --with-http_stub_status_module   --with-http_mp4_module --with-http_flv_module --prefix=/usr/local/nginx
3.  [root@localhost nginx-0.7.65]#make  
4.  [root@localhost nginx-0.7.65]#make install  

到这里Nginx安装完成。

4.为google-perftools添加线程目录
创建一个线程目录,这里将文件放在/tmp/tcmalloc下,操作如下:
 

1.  [root@localhost home]#mkdir /tmp/tcmalloc  
2.  [root@localhost home]#chmod 0777 /tmp/tcmalloc  

5.修改Nginx主配置文件
修改nginx.conf文件,在pid这行的下面添加如下代码:
 

1.  #pid        logs/nginx.pid;  
2.  google_perftools_profiles /tmp/tcmalloc;  

接着,重启Nginx,完成google-perftools的加载。

6.验证运行状态
为了验证google-perftools已经正常加载,通过如下命令查看:
 

1.  [root@ localhost home]# lsof -n | grep tcmalloc  
2.  nginx      2395 nobody   9w  REG    8,8       0    1599440 /tmp/tcmalloc.2395  
3.  nginx      2396 nobody   11w REG   8,8       0    1599443 /tmp/tcmalloc.2396  
4.  nginx      2397 nobody   13w REG  8,8        0    1599441  /tmp/tcmalloc.2397  
5.  nginx     2398 nobody    15w REG  8,8     0    1599442 /tmp/tcmalloc.2398  

由于在Nginx配置文件中,设置worker_processes的值为4,因此开启了4个Nginx线程,每个线程会有一行记录。每个线程文件后面的数字值就是启动的Nginx的PID值。
至此,利用TCMalloc优化Nginx的操作完成。

 

内核参数的优化,主要是在Linux系统中针对Nginx应用而进行的系统内核参数优化,常见的优化参数值如下。
下面给出一个优化实例以供参考:

1.  net.ipv4.tcp_max_tw_buckets = 6000 
2.  net.ipv4.ip_local_port_range = 1024 65000  
3.  net.ipv4.tcp_tw_recycle = 1 
4.  net.ipv4.tcp_tw_reuse = 1 
5.  net.ipv4.tcp_syncookies = 1 
6.  net.core.somaxconn = 262144 
7.  net.core.netdev_max_backlog = 262144 
8.  net.ipv4.tcp_max_orphans = 262144 
9.  net.ipv4.tcp_max_syn_backlog = 262144 
10. net.ipv4.tcp_synack_retries = 1 
11. net.ipv4.tcp_syn_retries = 1 
12. net.ipv4.tcp_fin_timeout = 1 
13. net.ipv4.tcp_keepalive_time = 30 

将上面的内核参数值加入/etc/sysctl.conf文件中,然后执行如下命令使之生效:
[root@ localhost home]#/sbin/sysctl -p

 

详细解释参考高性能Linux 书

 

修改配置文件

Vi /usr/local/nginx/conf/nginx.conf

 

 

 

 

user nobody;

worker_processes  1;

 

#error_log logs/error.log;

error_log logs/error.log  notice;

#error_log logs/error.log  info;

 

pid       logs/nginx.pid;

google_perftools_profiles /tmp/tcmalloc;

 

worker_rlimit_nofile 65535;

events {

   use epoll;

   worker_connections  65536;

}

 

 

http {

        include       mime.types;

             default_type  application/octet-stream;

 

   log_format main '$remote_addr - $remote_user [$time_local] '

        '"$request" $status $bytes_sent'

        '"$http_referer""$http_user_agent" '

        '"$gzip_ratio"';

   log_format download '$remote_addr - $remote_user [$time_local] '

        '"$request" $status $bytes_sent'

        '"$http_referer""$http_user_agent" '

        '"$http_range""$sent_http_content_range"';

 

   access_log  logs/access.log  main;

   client_max_body_size  20m;

   client_header_buffer_size    32K;

   large_client_header_buffers  432k;

   sendfile        on;

   tcp_nopush     on;

 

    #keepalive_timeout  0;

   keepalive_timeout  60;

   client_header_timeout  10;

   client_body_timeout    10;

   send_timeout          10;

   gzip  on;

   gzip_min_length  1k;

   gzip_buffers     4  16k;

   gzip_http_version  1.1;

   gzip_comp_level  2;

   gzip_types  text/plainapplication/x-javascript text/css application/xml;

   gzip_vary  on;

   server {

       listen       80;

       server_name  localhost;

 

       #charset koi8-r;

 

       #access_log logs/host.access.log  main;

 

       location / {

           root   html;  #web路径

           index  index.html index.htmindex.php;

       }

 

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

       #

       error_page   500 502 503 504  /50x.html;

       location = /50x.html {

           root   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

       #

       location ~ \.php$ {

           root           html;

           fastcgi_pass   127.0.0.1:9000;

           fastcgi_index  index.php;

           fastcgi_param SCRIPT_FILENAME   /usr/local/nginx/html$fastcgi_script_name;    #/usr/local/nginx/htmlweb路径

           include        fastcgi_params;

       }

 

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

       # concurs with nginx's one

       #

       #location ~ /\.ht {

       #    deny  all;

       #}

    }

 

 

    #another virtual host using mix of IP-, name-, and port-based configuration

    #

   #server {

   #    listen       8000;

   #    listen       somename:8080;

   #    server_name  somename alias  another.alias;

 

   #    location / {

   #        root   html;

   #        index  index.html index.htm;

   #    }

   #}

 

 

    #HTTPS server

    #

   #server {

   #    listen       443;

   #    server_name  localhost;

 

   #    ssl                  on;

   #    ssl_certificate      cert.pem;

   #    ssl_certificate_key  cert.key;

 

   #    ssl_session_timeout  5m;

 

   #    ssl_protocols  SSLv2 SSLv3 TLSv1;

   #    ssl_ciphers  HIGH:!aNULL:!MD5;

   #   ssl_prefer_server_ciphers   on;

 

   #    location / {

   #        root   html;

   #        index  index.html index.htm;

   #    }

   #}

 

}

详细说明参考高性能linux 书

 

启动nginx

#/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

 

停止nginx                                        

查询nginx主进程号
#ps -ef | grep nginx

从容停止Nginx:
kill -QUIT 主进程号
快速停止Nginx:
kill -TERM 主进程号
强制停止Nginx:
pkill -9 nginx

 

 

测试页面,直接输入服务器ip

 

 

安装php

 

需要安装需要的模块,

yum -y install libjpeg libjpeg-devel libpnglibpng-devel freetype freetype-devel libxml2 libxml2-devel mysql pcre-devel curl-devel  libxslt-devel

 

 

tarzxvf php-5.5.4.tar.gz

cdphp-5.5.4

复杂配置

#./configure--prefix=/usr/local/php \
--with-curl \
--with-freetype-dir \
--with-gd \
--with-gettext \
--with-iconv-dir \
--with-jpeg-dir \
--with-kerberos \
--with-libdir=lib64 \
--with-libxml-dir \
--with-mysql \
--with-mysqli \
--with-openssl \
--with-pcre-regex \
--with-pdo-mysql \
--with-pdo-sqlite \
--with-pear \
--with-png-dir \
--with-xmlrpc \
--with-xsl \
--with-zlib \
--enable-fpm \
--enable-bcmath \
--enable-libxml \
--enable-inline-optimization \
--enable-gd-native-ttf \
--enable-mbregex \
--enable-mbstring \
--enable-opcache \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-xml \
--enable-zip

 

简易配置

./configure--prefix=/usr/local/php --with-mysql--enable-fpm--enable-opcache --with-freetype-dir --with-jpeg-dir--with-png-dir --with-zlib --with-libxml-dir --enable-xml --disable-debug--enable-safe-mode --enable-mbstring

如果需要GD库还要

./configure--prefix=/usr/local/php --with-mysql --enable-fpm--enable-opcache --with-freetype-dir--with-jpeg-dir --with-png-dir --with-gd --with-zlib --with-libxml-dir--enable-xml --disable-debug --enable-safe-mode --enable-mbstring

make&& make install

 

php-5.4.11已经支持fastcgi了,不用在打补丁了。
1.在编译安装时加上'--enable-fpm',注意修改php/etc/php-fpm.conf去掉pid前的注释;
2.复制/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm还可以做成服务

复制php-fpm文件到php安装目录

cp-R ./sapi/fpm/php-fpm.conf /usr/local/php/etc/php-fpm.conf

 

拷贝php.ini文件

安装目录有2个文件:php.ini-development和php.ini-production

注意php.ini-production拥有较高的安全性设定,则适合上线当产品使用

# cpphp.ini-development /usr/local/php/lib/php.ini

或者

# cp php.ini-production/usr/local/php/lib/php.ini

如果操作了这一步以后使用phpinfo()就会看到Loaded Configuration File:

 

php-fpm启动

拷贝启用文件
#  cp -R ./sapi/fpm/php-fpm/etc/init.d/php-fpm

启动
# /etc/init.d/php-fpm

 

重启

killallphp-fpm

# /etc/init.d/php-fpm

 

重启nginx

#/usr/local/nginx/sbin/nginx -s reload

php安装成功查看进程

 

配置php加速插件opcace

php.ini配置opcace

;opcache
zend_extension = /usr/local/php/lib/php/extensions
/no-debug-non-zts-20121212/opcache.so
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1
opcache.enable=1

 

/no-debug-non-zts-20121212   根据实际文件名称配置

128意思是给它分配128M内存,然后重启apache,用phpinfo查看是否生效,显示下面的信息就说明生效了

配置与优化PHP-FPM

标签max_children用于设置FastCGI的进程数。根据官方建议,小于2GB内存的服务器,可以只开启64个进程,4GB以上内存的服务器可以开启200个进程。
<valuename="max_children">5</value>

标签rlimit_files用于设置PHP-FPM对打开文件描述符的限制,默认值为1024。这个标签的值必须和Linux内核打开文件数关联起来,例如要将此值设置为65535,就必须在Linux命令行执行'ulimit -HSn 65536'。
<valuename="rlimit_files">1024</value>

 

在配置完成Nginx+FastCGI之后,为了保证Nginx下PHP环境的高速稳定运行,需要添加一些FastCGI优化指令。下面给出一个优化实例,将下面代码添加到Nginx主配置文件中的HTTP层级。

1.  fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10m inactive=5m;    
2.  fastcgi_connect_timeout 300;    
3.  fastcgi_send_timeout 300;    
4.  fastcgi_read_timeout 300;    
5.  fastcgi_buffer_size 64k;    
6.  fastcgi_buffers 4 64k;    
7.  fastcgi_busy_buffers_size 128k;    
8.  fastcgi_temp_file_write_size 128k;    
9.  fastcgi_cache TEST;    
10. fastcgi_cache_valid 200 302 1h;    
11. fastcgi_cache_valid 301 1d;    
12. fastcgi_cache_valid any 1m;     

 

 

启动nginx

ulimit –n 65535

ulimit –n 65536

# /usr/local/nginx/sbin/nginx -c/usr/local/nginx/conf/nginx.conf

重启php-fpm

# killall php-fpm

ulimit –HSn 65536

# /etc/init.d/php-fpm

 

重启nginx

# /usr/local/nginx/sbin/nginx -s reload

 

 

 

解决安装完成后Mysql  连接不了问题(Nosuch file or directory 提示)

 

首先确定是mysql_connect()和mysql_pconnect()的问题,故障现象就是函数返回空,而mysql_error()返回“No such file or directory”。

 

写个phpinfo页面,找到mysql.default_socket、mysqli.default_socket、pdo_mysql.default_socket。

 

启动mysql,执行命令 STATUS; 记下UNIX socket的值

 

如果2和3的值不一样,则打开php.ini(可以从phpinfo页面中找到php.ini的位置)

 

一共修改三处,分别为:mysql.default_socket、mysqli.default_socket、pdo_mysql.default_socket

将UNIX socket的值赋予上面三次

 

重启nginx 和 php-fpm

 

OK

0 0
原创粉丝点击