最新消息:

Nginx+PHP(FastCGI)+MySQL在小内存VPS(t1.micro)上的安装配置与优化

mysql admin 3025浏览 0评论

参考资料:
http://blog.s135.com/nginx_php_v6/
http://blog.s135.com/post/375/

背景介绍:
以前,因为AWS EC2的价格比较昂贵而租用了其它国外小厂商的VPS,在使用了3年多之后,发现AWS EC2的价格居然比现在正在使用的VPS要便宜很多。
全球线路与速度最理想的日本节点的t1.micro型号,选择3年长期合约的价格在2500元人民币左右,一年下来也不到900元,所以就打算将Blog迁移到AWS EC2上。
之前在部署VPS环境的时候,基本上是完整的参考了张宴的两篇文档,包括操作系统的选择也是相同的。

但这一次我想采用目前较新的CentOS 6.4 minimal x86_64
同时选择版本最新的Nginx,PHP以及MySQL,引入EPEL仓库来简化安装与部署过程中对额外的依赖软件的安装,优化一些原来的工作方式,比如创建服务管理脚本来启动/停止Nginx与MySQL,使用logrotate来切割日志等;
经过了一番折腾,在填了不少的坑之后,终于成功的完成了新环境的部署,感觉非常不错。

下面,就是此次的整个安装配置过程:
1. 安装常用工具
yum install -y vim wget unzip screen tree mlocate

2. 安装编译工具
yum install -y gcc gcc-c++ autoconf patch cmake automake

3. 安装EPEL仓库
yum install -y http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

4. 安装所有必需的软件包
yum install -y libjpeg-turbo libjpeg-turbo-devel libpng libpng-devel freetype freetype-devel
yum install -y libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel gmp gmp-devel
yum install -y bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel unixODBC unixODBC-devel
yum install -y krb5-libs krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel
yum install -y libgcrypt libgcrypt-devel libtool
yum install -y libmcrypt libmcrypt-devel mhash mhash-devel mcrypt pcre pcre-devel
yum install -y ImageMagick ImageMagick-devel

5. 下载所有必需的软件源码包
mkdir /root/packages
cd /root/packages
wget http://nginx.org/download/nginx-1.4.7.tar.gz
wget http://museum.php.net/php5/php-5.2.17.tar.gz
wget http://php-fpm.org/downloads/php-5.2.17-fpm-0.5.14.diff.gz
wget http://mirrors.sohu.com/mysql/MySQL-5.5/mysql-5.5.36.tar.gz
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
wget http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz
wget http://pecl.php.net/get/imagick-3.2.0RC1.tgz
wget http://pkgs.fedoraproject.org/repo/pkgs/php-eaccelerator/eaccelerator-0.9.6.1.tar.bz2/32ccd838e06ef5613c2610c1c65ed228/eaccelerator-0.9.6.1.tar.bz2

6. 编译安装iconv
cd /root/packages
tar xzvf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure
make
make install

7. 编译安装并配置MySQL
7.1 编译安装MySQL
cd /root/packages
tar xzvf mysql-5.5.36.tar.gz
cd mysql-5.5.36
cmake -DCMAKE_INSTALL_PREFIX=/webserver/mysql
-DSYSCONFDIR=/webserver/mysql/etc
-DMYSQL_DATADIR=/webserver/mysql/data
-DMYSQL_TCP_PORT=3306
-DMYSQL_UNIX_ADDR=/werbserver/mysql/run/mysqld.sock
-DMYSQL_USER=mysql
-DEXTRA_CHARSETS=all
-DWITH_READLINE=1
-DWITH_SSL=system
-DWITH_EMBEDDED_SERVER=1
-DENABLED_LOCAL_INFILE=1
-DWITH_INNOBASE_STORAGE_ENGINE=1
make
make install

7.2 创建mysql用户
useradd mysql -d /webserver/mysql/data

7.3 创建MySQL所需目录
mkdir -p /webserver/mysql/{etc,data,tmp,run,binlogs,log}
chown mysql:mysql -R /webserver/mysql/{etc,data,tmp,run,binlogs,log}

7.4 创建my.cnf配置文件
vim /webserver/mysql/etc/my.cnf

01 [mysqld]
02 # basic settings
03 datadir = /webserver/mysql/data
04 tmpdir = /webserver/mysql/tmp
05 socket = /webserver/mysql/run/mysqld.sock
06 port = 3306
07 pid-file = /webserver/mysql/run/mysqld.pid
08
09 # innodb settings
10 default-storage-engine = INNODB
11 innodb_file_per_table = 1
12 log-bin = /webserver/mysql/binlogs/bin-log-mysqld
13 log-bin-index = /webserver/mysql/binlogs/bin-log-mysqld.index
14 innodb_data_home_dir = /webserver/mysql/data
15 innodb_data_file_path = ibdata1:10M:autoextend
16 innodb_log_group_home_dir = /webserver/mysql/data
17
18 # increase the max connections
19 max_connections = 200
20
21 # the expiration time for logs, including binlogs
22 expire_logs_days = 7
23
24 # set the character as utf8
25 character-set-server = utf8
26 collation-server = utf8_unicode_ci
27
28 # server id
29 server-id=33061
30
31 # other settings
32 [mysqld_safe]
33 log-error = /webserver/mysql/log/mysqld.log
34 pid-file = /webserver/mysql/run/mysqld.pid
35 open-files-limit = 8192
36
37 [mysqlhotcopy]
38 interactive-timeout
39
40 [client]
41 port = 3306
42 socket = /webserver/mysql/run/mysqld.sock
43 default-character-set = utf8

chown mysql:mysql /webserver/mysql/etc/my.cnf

7.5 初始化MySQL系统数据库
chmod +x scripts/mysql_install_db
scripts/mysql_install_db –user=mysql –basedir=/webserver/mysql –datadir=/webserver/mysql/data/ –defaults-file=/webserver/mysql/etc/my.cnf

7.6 创建MySQL服务管理脚本
mkdir /webserver/init.d/
cp support-files/mysql.server /webserver/init.d/mysqld
chmod +x /webserver/init.d/mysqld

7.7 启动MySQL服务
/webserver/init.d/mysqld start

7.8 登陆MySQL命令行终端
/webserver/mysql/bin/mysql -uroot -p

8. 编译安装并配置PHP
8.1 编译安装PHP
cd /root/packages
tar xzf php-5.2.17.tar.gz
gzip -cd php-5.2.17-fpm-0.5.14.diff.gz | patch -d php-5.2.17 -p1

注:创建软链接以便PHP在编译时能顺利找到所需的库文件
ln -s /webserver/mysql/lib /webserver/mysql/lib64
ln -s /usr/local/lib/libiconv.so /usr/local/lib64/libiconv.so
ln -s /usr/lib64/libmhash.so.2 /usr/lib64/libmhash.so

cd php-5.2.17
./configure –prefix=/webserver/php
–with-config-file-path=/webserver/php/etc
–with-mysql=/webserver/mysql –with-mysqli=/webserver/mysql/bin/mysql_config
–with-libdir=lib64
–with-iconv-dir=/usr/local
–with-freetype-dir –with-jpeg-dir –with-png-dir
–with-zlib –with-libxml-dir –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 –with-xmlrpc
–enable-zip –enable-soap

make ZEND_EXTRA_LIBS=’-liconv’

注:创建软链接以便PHP在安装时能顺利找到所需的库文件
ln -s /webserver/mysql/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18
ln -s /usr/local/lib/libiconv.so.2 /usr/lib64/libiconv.so.2

make install
cp php.ini-dist /webserver/php/etc/php.ini

8.2 编译安装eaccelerator
cd /root/packages
tar xjf eaccelerator-0.9.6.1.tar.bz2
cd eaccelerator-0.9.6.1/
/webserver/php/bin/phpize
./configure –enable-eaccelerator=shared –with-php-config=/webserver/php/bin/php-config
make
make install
cd ../

8.3 编译安装PDO_MYSQL
tar zxvf PDO_MYSQL-1.0.2.tgz
cd PDO_MYSQL-1.0.2/
/webserver/php/bin/phpize
./configure –with-php-config=/webserver/php/bin/php-config –with-pdo-mysql=/webserver/mysql
make
make install
cd ../

8.4 编译安装imagick
tar xzf imagick-3.2.0RC1.tgz
cd imagick-3.2.0RC1
/webserver/php/bin/phpize
./configure –with-php-config=/webserver/php/bin/php-config
make
make install

8.5 修改php.ini配置文件
vim /webserver/php/etc/php.ini

01 ; Directory in which the loadable extensions (modules) reside.
02 ;extension_dir = "./"
03 extension_dir = "/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/"
04 extension = "imagick.so"
05 extension = "pdo_mysql.so"
06
07 ;output_buffering = Off
08 output_buffering = On
09
10 ; cgi.fix_pathinfo=0
11 cgi.fix_pathinfo=0

8.6 创建缓存目录
mkdir /webserver/eaccelerator_cache

8.7 在php.ini中添加eaccelerator支持
vim /webserver/php/etc/php.ini

01 [eaccelerator]
02 zend_extension="/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
03 eaccelerator.shm_size="1"
04 eaccelerator.cache_dir="/webserver/eaccelerator_cache"
05 eaccelerator.enable="1"
06 eaccelerator.optimizer="1"
07 eaccelerator.check_mtime="1"
08 eaccelerator.debug="0"
09 eaccelerator.filter=""
10 eaccelerator.shm_max="0"
11 eaccelerator.shm_ttl="3600"
12 eaccelerator.shm_prune_period="3600"
13 eaccelerator.shm_only="0"
14 eaccelerator.compress="1"
15 eaccelerator.compress_level="9"
16 eaccelerator.keys = "disk_only"
17 eaccelerator.sessions = "disk_only"
18 eaccelerator.content = "disk_only"

8.8 创建www用户
useradd -r www

8.9 创建所需目录
mkdir -p /webserver/blog/{rainbow,eric,keshui,logs}
chown www:www /webserver/blog/{rainbow,eric,keshui,logs}
chmod +w /webserver/blog/{rainbow,eric,keshui,logs}
mkdir /webserver/php/run

8.10 创建php-fpm.con配置文件
rm -f /webserver/php/etc/php-fpm.conf
vim /webserver/php/etc/php-fpm.conf

001 <?xml version="1.0" ?>
002 <configuration>
003
004   All relative paths in this config are relative to php's install prefix
005
006   <section name="global_options">
007
008     Pid file
009     <value name="pid_file">/webserver/php/logs/php-fpm.pid</value>
010
011     Error log file
012     <value name="error_log">/webserver/php/logs/php-fpm.log</value>
013
014     Log level
015     <value name="log_level">notice</value>
016
017     When this amount of php processes exited with SIGSEGV or SIGBUS ...
018     <value name="emergency_restart_threshold">10</value>
019
020     ... in a less than this interval of time, a graceful restart will be initiated.
021     Useful to work around accidental curruptions in accelerator's shared memory.
022     <value name="emergency_restart_interval">1m</value>
023
024     Time limit on waiting child's reaction on signals from master
025     <value name="process_control_timeout">5s</value>
026
027     Set to 'no' to debug fpm
028     <value name="daemonize">yes</value>
029
030   </section>
031
032   <workers>
033
034     <section name="pool">
035
036       Name of pool. Used in logs and stats.
037       <value name="name">default</value>
038
039       Address to accept fastcgi requests on.
040       Valid syntax is 'ip.ad.re.ss:port' or just 'port' or '/path/to/unix/socket'
041       <value name="listen_address">/webserver/php/run/php-cgi.sock</value>
042
043       <value name="listen_options">
044
045         Set listen(2) backlog
046         <value name="backlog">-1</value>
047
048         Set permissions for unix socket, if one used.
049         In Linux read/write permissions must be set in order to allow connections from web server.
050         Many BSD-derrived systems allow connections regardless of permissions.
051         <value name="owner"></value>
052         <value name="group"></value>
053         <value name="mode">0666</value>
054       </value>
055
056       Additional php.ini defines, specific to this pool of workers.
057       <value name="php_defines">
058         <value name="sendmail_path">/usr/sbin/sendmail -t -i</value>
059         <value name="display_errors">0</value>
060       </value>
061
062       Unix user of processes
063       <value name="user">www</value>
064
065       Unix group of processes
066       <value name="group">www</value>
067
068       Process manager settings
069       <value name="pm">
070
071         Sets style of controling worker process count.
072         Valid values are 'static' and 'apache-like'
073         <value name="style">static</value>
074
075         Sets the limit on the number of simultaneous requests that will be served.
076         Equivalent to Apache MaxClients directive.
077         Equivalent to PHP_FCGI_CHILDREN environment in original php.fcgi
078         Used with any pm_style.
079         <value name="max_children">5</value>
080
081         Settings group for 'apache-like' pm style
082         <value name="apache_like">
083
084           Sets the number of server processes created on startup.
085           Used only when 'apache-like' pm_style is selected
086           <value name="StartServers">20</value>
087
088           Sets the desired minimum number of idle server processes.
089           Used only when 'apache-like' pm_style is selected
090           <value name="MinSpareServers">5</value>
091
092           Sets the desired maximum number of idle server processes.
093           Used only when 'apache-like' pm_style is selected
094           <value name="MaxSpareServers">35</value>
095
096         </value>
097
098       </value>
099
100       The timeout (in seconds) for serving a single request after which the worker process will be terminated
101       Should be used when 'max_execution_time' ini option does not stop script execution for some reason
102       '0s' means 'off'
103       <value name="request_terminate_timeout">0s</value>
104
105       The timeout (in seconds) for serving of single request after which a php backtrace will be dumped to slow.log file
106       '0s' means 'off'
107       <value name="request_slowlog_timeout">0s</value>
108
109       The log file for slow requests
110       <value name="slowlog">logs/slow.log</value>
111
112       Set open file desc rlimit
113       <value name="rlimit_files">65535</value>
114
115       Set max core size rlimit
116       <value name="rlimit_core">0</value>
117
118       Chroot to this directory at the start, absolute path
119       <value name="chroot"></value>
120
121       Chdir to this directory at the start, absolute path
122       <value name="chdir"></value>
123
124       Redirect workers' stdout and stderr into main error log.
125       If not set, they will be redirected to /dev/null, according to FastCGI specs
126       <value name="catch_workers_output">yes</value>
127
128       How much requests each process should execute before respawn.
129       Useful to work around memory leaks in 3rd party libraries.
130       For endless request processing please specify 0
131       Equivalent to PHP_FCGI_MAX_REQUESTS
132       <value name="max_requests">1024</value>
133
134       Comma separated list of ipv4 addresses of FastCGI clients that allowed to connect.
135       Equivalent to FCGI_WEB_SERVER_ADDRS environment in original php.fcgi (5.2.2+)
136       Makes sense only with AF_INET listening socket.
137       <value name="allowed_clients">127.0.0.1</value>
138
139       Pass environment variables like LD_LIBRARY_PATH
140       All $VARIABLEs are taken from current environment
141       <value name="environment">
142         <value name="HOSTNAME">$HOSTNAME</value>
143         <value name="PATH">/usr/local/bin:/usr/bin:/bin</value>
144         <value name="TMP">/tmp</value>
145         <value name="TMPDIR">/tmp</value>
146         <value name="TEMP">/tmp</value>
147         <value name="OSTYPE">$OSTYPE</value>
148         <value name="MACHTYPE">$MACHTYPE</value>
149         <value name="MALLOC_CHECK_">2</value>
150       </value>
151
152     </section>
153
154   </workers>
155
156 </configuration>

8.11 创建php-fpm服务管理脚本
ln -s /webserver/php/sbin/php-fpm /webserver/init.d/php-fpm

8.12 启动php-fpm服务
/webserver/init.d/php-fpm start

9. 编译安装并配置Nginx
9.1 编译安装Nginx
cd /root/packages
tar xzf nginx-1.4.7.tar.gz
cd nginx-1.4.7/
./configure –user=www –group=www
–prefix=/usr/local/webserver/nginx
–with-http_stub_status_module –with-http_ssl_module
make
make install

9.2 创建Nginx所需目录
mkdir /webserver/nginx/conf/conf.d
mkdir /webserver/nginx/run

9.3 创建fastcgi_params配置文件
vim fastcgi_params

01 fastcgi_param  QUERY_STRING       $query_string;
02 fastcgi_param  REQUEST_METHOD     $request_method;
03 fastcgi_param  CONTENT_TYPE       $content_type;
04 fastcgi_param  CONTENT_LENGTH     $content_length;
05
06 fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
07 fastcgi_param  REQUEST_URI        $request_uri;
08 fastcgi_param  DOCUMENT_URI       $document_uri;
09 fastcgi_param  DOCUMENT_ROOT      $document_root;
10 fastcgi_param  SERVER_PROTOCOL    $server_protocol;
11
12 fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
13 fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;
14
15 fastcgi_param  REMOTE_ADDR        $remote_addr;
16 fastcgi_param  REMOTE_PORT        $remote_port;
17 fastcgi_param  SERVER_ADDR        $server_addr;
18 fastcgi_param  SERVER_PORT        $server_port;
19 fastcgi_param  SERVER_NAME        $server_name;
20
21 # PHP only, required if PHP was built with --enable-force-cgi-redirect
22 fastcgi_param  REDIRECT_STATUS    200;

9.4 创建Nginx.conf主配置文件
vim /webserver/nginx/conf/nginx.conf

01 user        www www;
02 worker_processes  1;
03
04 error_log  /webserver/nginx/logs/error.log;
05 pid        /webserver/nginx/run/nginx.pid;
06
07 worker_rlimit_nofile   32768;
08
09 events {
10     use epoll;
11     worker_connections 32768;
12 }
13
14
15 http {
16     include       /webserver/nginx/conf/mime.types;
17     default_type  application/octet-stream;
18
19     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
20                       '$status $body_bytes_sent "$http_referer" '
21                       '"$http_user_agent" "$http_x_forwarded_for"';
22
23     access_log    /webserver/nginx/logs/access.log  main;
24
25     server_names_hash_bucket_size 128;
26     client_header_buffer_size 32k;
27     large_client_header_buffers 4 32k;
28
29     sendfile        on;
30     tcp_nopush      on;
31
32     keepalive_timeout  60;
33
34     tcp_nodelay     on;
35
36     fastcgi_connect_timeout 300;
37     fastcgi_send_timeout 300;
38     fastcgi_read_timeout 300;
39     fastcgi_buffer_size 64k;
40     fastcgi_buffers 4 64k;
41     fastcgi_busy_buffers_size 128k;
42     fastcgi_temp_file_write_size 128k;
43
44     gzip  on;
45     gzip_min_length 1k;
46     gzip_buffers 4 16k;
47     gzip_http_version 1.0;
48     gzip_comp_level 2;
49     gzip_types text/plain application/x-javascript text/css application/xml;
50     gzip_vary on;
51     
52     include /webserver/nginx/conf/conf.d/*.conf;
53 }

9.5 创建附属配置文件
9.5.1 创建status.conf配置文件管理status页面
vim /webserver/nginx/conf/conf.d/status.conf

1 server
2 {
3   listen 80;
4   server_name status.nginxs.com status.heylinux.com;
5   location / {
6     stub_status on;
7     access_log off;
8   }
9 }

9.5.2 创建rainbow.conf配置文件管理heylinux.com
vim /webserver/nginx/conf/conf.d/rainbow.conf

01 server
02 {
03   listen       80;
04   server_name  heylinux.com *.heylinux.com;
05   index index.html index.htm index.php;
06   root  /webserver/blog/rainbow;
07
08   location / {
09       if (-f $request_filename/index.html){
10               rewrite (.*) $1/index.html break;
11       }
12       if (-f $request_filename/index.php){
13               rewrite (.*) $1/index.php;
14       }
15       if (!-f $request_filename){
16               rewrite (.*) /index.php;
17       }
18   }
19
20   location ~ .*.(php|php5)?$
21   {
22     fastcgi_pass  unix:/webserver/php/run/php-cgi.sock;
23     fastcgi_index index.php;
24     fastcgi_param SCRIPT_FILENAME /webserver/blog/rainbow$fastcgi_script_name;
25     include /webserver/nginx/conf/fastcgi_params;
26   }
27
28   location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
29   {
30     expires   15d;
31   }
32
33   location ~ .*.(js|css)?$
34   {
35     expires   1d;
36   }
37
38   log_format  rainbow_access  '$remote_addr - $remote_user [$time_local] "$request" '
39               '$status $body_bytes_sent "$http_referer" '
40               '"$http_user_agent" $http_x_forwarded_for';
41   access_log  /webserver/blog/logs/rainbow_access.log rainbow_access;
42 }

9.5.3 创建eric.conf配置文件管理nginxs.com
vim /webserver/nginx/conf/conf.d/eric.conf

01 server
02 {
03   listen       80;
04   server_name  nginxs.com *.nginxs.com;
05   index index.html index.htm index.php;
06   root  /webserver/blog/eric;
07
08   location / {
09       if (-f $request_filename/index.html){
10               rewrite (.*) $1/index.html break;
11       }
12       if (-f $request_filename/index.php){
13               rewrite (.*) $1/index.php;
14       }
15       if (!-f $request_filename){
16               rewrite (.*) /index.php;
17       }
18   }
19
20   location ~ .*.(php|php5)?$
21   {
22     fastcgi_pass  unix:/webserver/php/run/php-cgi.sock;
23     fastcgi_index index.php;
24     fastcgi_param SCRIPT_FILENAME /webserver/blog/eric$fastcgi_script_name;
25     include /webserver/nginx/conf/fastcgi_params;
26   }
27
28   location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
29   {
30     expires   15d;
31   }
32
33   location ~ .*.(js|css)?$
34   {
35     expires   1d;
36   }
37
38   log_format  eric_access  '$remote_addr - $remote_user [$time_local] "$request" '
39               '$status $body_bytes_sent "$http_referer" '
40               '"$http_user_agent" $http_x_forwarded_for';
41   access_log  /webserver/blog/logs/eric_access.log eric_access;
42 }

9.6 创建Nginx服务管理脚本
touch /webserver/init.d/nginx
chmod +x /webserver/init.d/nginx
vim /webserver/init.d/nginx

001 #!/bin/sh
002 #
003 # nginx - this script starts and stops the nginx daemon
004 #
005 # chkconfig:   - 85 15
006 # description:  Nginx is an HTTP(S) server, HTTP(S) reverse
007 #               proxy and IMAP/POP3 proxy server
008 # processname: nginx
009 # config:      /etc/nginx/nginx.conf
010 # config:      /etc/sysconfig/nginx
011 # pidfile:     /var/run/nginx.pid
012  
013 # Source function library.
014 . /etc/rc.d/init.d/functions
015  
016 # Source networking configuration.
017 . /etc/sysconfig/network
018  
019 # Check that networking is up.
020 "$NETWORKING" "no" ] && exit 0
021  
022 nginx="/webserver/nginx/sbin/nginx"
023 prog=$(basename $nginx)
024  
025 NGINX_CONF_FILE="/webserver/nginx/conf/nginx.conf"
026  
027 lockfile=/var/lock/subsys/nginx
028  
029 make_dirs() {
030    # make required directories
031    user=`$nginx -V 2>&1 | grep "configure arguments:" sed 's/[^*]*--user=([^ ]*).*/1/g' -`
032    if [ -z "`grep $user /etc/passwd`" ]; then
033        useradd -M -s /bin/nologin $user
034    fi
035    options=`$nginx -V 2>&1 | grep 'configure arguments:'`
036    for opt in $options; do
037        if [ `echo $opt | grep '.*-temp-path'` ]; then
038            value=`echo $opt | cut -d "=" -f 2`
039            if [ ! -d "$value" ]; then
040                # echo "creating" $value
041                mkdir -p $value && chown -R $user $value
042            fi
043        fi
044    done
045 }
046  
047 start() {
048     [ -x $nginx ] || exit 5
049     [ -f $NGINX_CONF_FILE ] || exit 6
050     make_dirs
051     echo -n $"Starting $prog: "
052     daemon $nginx -c $NGINX_CONF_FILE
053     retval=$?
054     echo
055     [ $retval -eq 0 ] && touch $lockfile
056     return $retval
057 }
058  
059 stop() {
060     echo -n $"Stopping $prog: "
061     killproc $prog -QUIT
062     retval=$?
063     echo
064     [ $retval -eq 0 ] && rm -f $lockfile
065     return $retval
066 }
067  
068 restart() {
069     configtest || return $?
070     stop
071     sleep 1
072     start
073 }
074  
075 reload() {
076     configtest || return $?
077     echo -n $"Reloading $prog: "
078     killproc $nginx -HUP
079     RETVAL=$?
080     echo
081 }
082  
083 force_reload() {
084     restart
085 }
086  
087 configtest() {
088   $nginx -t -c $NGINX_CONF_FILE
089 }
090  
091 rh_status() {
092     status $prog
093 }
094  
095 rh_status_q() {
096     rh_status >/dev/null 2>&1
097 }
098  
099 case "$1" in
100     start)
101         rh_status_q && exit 0
102         $1
103         ;;
104     stop)
105         rh_status_q || exit 0
106         $1
107         ;;
108     restart|configtest)
109         $1
110         ;;
111     reload)
112         rh_status_q || exit 7
113         $1
114         ;;
115     force-reload)
116         force_reload
117         ;;
118     status)
119         rh_status
120         ;;
121     condrestart|try-restart)
122         rh_status_q || exit 0
123             ;;
124     *)
125         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
126         exit 2
127 esac

9.7 启动Nginx服务
/webserver/init.d/nginx start

10. 优化系统内核参数
vim /etc/sysctl.conf

01 # For Nginx
02 net.ipv4.tcp_max_syn_backlog = 65536
03 net.core.netdev_max_backlog = 32768
04 net.core.somaxconn = 32768
05 net.ipv4.tcp_timestamps = 0
06 net.ipv4.tcp_synack_retries = 2
07 net.ipv4.tcp_syn_retries = 2
08 net.ipv4.tcp_tw_recycle = 1
09 net.ipv4.tcp_tw_reuse = 1
10 net.ipv4.ip_local_port_range = 1024 65535

sysctl -p

vim /etc/security/limits.conf

1 *                -       nofile          32768
2 *                -       nproc           32768

11. 创建交换分区
dd if=/dev/zero of=/opt/swapfile bs=1M count=256
mkswap /opt/wapfile
swapon /opt/swapfile

vi /etc/fstab

1 /opt/swapfile           swap    swap    defaults        0 0

12. 配置logrotate切割日志
yum install -y logrotate
vim /etc/logrotate.d/blog

01 /webserver/blog/logs/rainbow_access.log
02 /webserver/blog/logs/eric_access.log
03 {
04   daily
05   rotate 7
06   missingok
07   notifempty
08   dateext
09   sharedscripts
10   postrotate
11     if [ -f /webserver/nginx/run/nginx.pid ]; then
12       /bin/kill -HUP `/bin/cat /webserver/nginx/run/nginx.pid`
13     fi
14   endscript
15 }

13. 结束

转载请注明:爱开源 » Nginx+PHP(FastCGI)+MySQL在小内存VPS(t1.micro)上的安装配置与优化

您必须 登录 才能发表评论!