The server architecture involved in this article is mainly for Small and Medium-sized websites of about 3000 people/15 minutes. It is mostly seen in discuz! Communities built on forums, etc.
The environment is composed of Linux (kernel 2.6 +), nginx, PHP (FastCGI), MySQL, and other highly efficient open-source software, which is the first choice for ordinary webmasters,
But for some enterprises, this solution is also a very good choice!
As for the Performance Comparison Between nginx, Apache, and other Web servers, we will not mention it here.
This document describes the installation and configuration of instances for your reference.
1. hardware environment
One Web server and one DB server without raid
Web Server Xeon dual-core 1.60 GHz, 2g, 73gscsi * 2
DB Server Xeon dual-core 1.60 GHz, 2g, 73gscsi * 2
2. System Environment
Centos 5.x (kernel 2.6 +)
To put it simply, let's talk about the preliminary work: install the system as much as possible, check the system time, dmesg to see if there are any errors, disable SELinux, IPv6, and upgrade the system to the latest Yum-y update
Install necessary software packages for the following web environment:
# Yum install-y NTP vim-enhanced GCC gcc-C ++ gcc-g77 flex bison Autoconf automake bzip2-devel ncurses-devel libjpeg-devel libpng-devel LibTIFF-devel FreeType-devel Pam-devel Kernel
3. Prepare Software
Nginx-0.6.31.tar.gz
Pcre-7.3.tar.gz
Php-5.2.6.tar.bz2
Eaccelerator-0.9.5.3.tar.bz2
ZendOptimizer-3.3.0-linux-glibc21-i386.tar.gz
Lighttpd-1.4.19.tar.bz2
Libxml2-2.6.30.tar.gz
Libmcrypt-2.5.8.tar.bz2
Gd-2.0.34.tar.gz
Mysql-5.0.51a-linux-i686-glibc23.tar.gz
The above software is the latest and most stable version. Search for and download the above software or the latest version.
Enter the full software name intitle: Index at www.google.com.
Generally, the first few of them can be downloaded. You can find the fastest one.
4. Start the installation environment:
(1) install the GD library
# Tar zxvf gd-2.0.34.tar.gz
# Cd gd-2.0.34
#./Configure -- prefix =/usr/local/GD2
# Make
# Make install
(2) install libxml2
# Tar zxvf libxml2-2.6.30.tar.gz
# Cd libxml2-2.6.30
#./Configure -- prefix =/usr/local/libxml2
# Make
# Make install
(3) install libmcrypt
# Cd/usr/local/src
# Tar jxvf libmcrypt-2.5.8.tar.bz2
# Cd libmcrypt-2.5.8
#./Configure -- prefix =/usr/local/libmcrypt
# Make
# Make install
(4)decompress mysql-5.0.51a-linux-i686-glibc23.tar.gz, and then transfer the lib and include directories in the unlocked package to any directory, such as/usr/local/MySQL
Because PHP 5 compilation requires MySQL support (-- With-MySQL)
# Tar zxvf mysql-5.0.51a-linux-i686-glibc23.tar.gz
# Cd mysql-5.0.51a-linux-i686-glibc23
# Mkdir-P/user/local/MySQL
# Mv./lib./include/user/local/MySQL/
(5) Compile PHP (fastcfi Mode)
#. /Configure -- prefix =/usr/local/PHP-fcgi -- enable-FastCGI -- enable-discard-path -- enable-force-CGI-redirect -- With-config-file-Path = /usr/local/PHP-fcgi/etc -- enable-Zend-multibyte -- With-mysql =/usr/local/MySQL -- With-libxml-Dir =/usr/local/libxml2 -- With-Gd =/usr/local/GD2 -- With-JPEG-dir -- With-PNG-dir -- with-bz2 -- With-FreeType-dir -- With-iconv-dir --- zlib-dir -- With-mcrypt =/usr/local/libmcrypt -- enable-sysvsem -- enable-inline-optimization -- enable-soap -- enable-Gd-native-TTF -- enable-FTP -- enable-mbstring -- enable-EXIF -- disable-Debug -- disable-ipv6
# Make
# Make install
# Cp PHP. ini-Dist/usr/local/PHP-fcgi/etc/PHP. ini
(6) install zendoptimizer
# Tar zxvf ZendOptimizer-3.3.0-linux-glibc21-i386.tar.gz
# Cd ZendOptimizer-3.3.0-linux-glibc21-i386
#./Install
Basically, the carriage return is always performed. It is worth noting that:
When you see that you want to select the PHP path, write:
/Usr/local/PHP-fcgi/etc
Are you using Apache Web server?
Select No here
(7) install eaccelerator
# Tar jxvf eaccelerator-0.9.5.3.tar.bz2
# Cd eaccelerator-0.9.5.3
#./Configure -- enable-eaccelerator = shared -- With-PHP-Config =/usr/local/PHP-fcgi/bin/PHP-config
# Make
# Make install
# Mkdir/tmp/eaccelerator & chmod 777/tmp/eaccelerator & Touch/var/log/eaccelerator_log
Edit PHP. ini
# Vi/usr/local/PHP-fcgi/etc/PHP. ini
Add:
Extension =/usr/local/PHP-fcgi/lib/PHP/extensions/no-debug-non-zts-20060613/eaccelerator. So
Eaccelerator. shm_size = "16"
Eaccelerator. cache_dir = "/tmp/eaccelerator"
Eaccelerator. Enable = "1"
Eaccelerator. optimizer = "1"
Eaccelerator. check_mtime = "1"
Eaccelerator. DEBUG = "0"
Eaccelerator. LOG_FILE = "/var/log/eaccelerator_log"
Eaccelerator. Filter = ""
Eaccelerator. shm_max = "0"
Eaccelerator. shm_ttl = "0"
Eaccelerator. shm_prune_period = "0"
Eaccelerator. shm_only = "0"
Eaccelerator. Compress = "1"
Eaccelerator. compress_level = "9"
(8) The PCRE Library is required for installing the nginx rewrite module.
# Tar zxvf pcre-7.3.tar.gz
# Cd pcre-7.3
#./Configure
# Make & make install
(9) install nginx
#./Configure -- prefix =/usr/local/nginx/-- with-http_stub_status_module
# Make & make install
(10) Compile the spawn-fcgi included in Lighttpd, which is used to start PHP-CGI.
# Tar jxvf lighttpd-1.4.19.tar.bz2
# Cd lighttpd-1.4.19
#./Configure
# Make
# Cp src/spawn-fcgi/usr/local/PHP-fcgi/bin/
Now, the installation process is complete.
5. Start to configure the environment:
# Cd/usr/local/nginx/Conf
# Mkdir vhosts
Back up the file to be modified
# Cp nginx. conf nginx. conf. Bak
# Cp fastcgi_params fastcgi_params.bak
# Mkdir-P/usr/local/nginx/temp/client_body
# Mkdir/usr/local/nginx/var
(1) EDIT nginx main configuration file nginx. conf
Edit nginx. conf and change its content to the last few lines.
Daemon on;
Worker_processes 4;
Worker_cpu_affinity 0001 0010 0100 1000;
Error_log/dev/NULL;
PID var/nginx. PID;
Lock_file var/nginx. lock;
Events {
Worker_connections 2048;
Use epoll;
Multi_accept on;
}
HTTP {
Include mime. types;
Default_type application/octet-stream;
Server_names_hash_max_size 512;
Server_names_hash_bucket_size 128;
Sendfile on;
Tcp_nopush on;
Tcp_nodelay on;
Keepalive_timeout 1;
Client_header_timeout 10;
Client_body_timeout 10;
Client_max_body_size 10 m;
Client_body_buffer_size 256 K;
Send_timeout 30;
Client_header_buffer_size 1 K;
Large_client_header_buffers 4 4 K;
Client_body_temp_path/usr/local/nginx/temp/client_body 1 2;
Gzip on;
Gzip_min_length 1000;
Gzip_buffers 4 8 K;
Gzip_http_version 1.1;
Gzip_comp_level 1;
Gzip_types text/plain text/CSS application/X-JavaScript text/XML application/XML + RSS text/JavaScript;
Log_format main '$ remote_addr-$ remote_user [$ time_local] $ request'
'"$ Status" $ body_bytes_sent "$ http_referer "'
'"$ Http_user_agent" "$ http_x_forwarded_for "';
Access_log/dev/NULL;
# Reference the virtual host configuration file you are about to place
Include vhosts/hao32.com;
Include vhosts/linuxsense.org;
}
(2) edit the configuration file of PHP in FastCGI mode:
# Vi fastcgi_params
Modify the content:
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_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 gateway_interface cgi/1.1;
Fastcgi_param server_software nginx/$ nginx_version;
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;
(2) edit the VM configuration file
# Cd vhosts/
# Vi hao32.com
Add the following content:
Server {
Listen 80;
SERVER_NAME hao32.com www.hao32.com;
# Enable log
# Access_log/var/log/hao32.com/access.log combined buffer = 32 K;
# Error_log/var/log/hao32.com/error.log warn;
Location /{
Index index.html index.htm index. php;
Root/www/wwwroot/hao32.com;
Location ~ /. Php $ {
Include fastcgi_params;
Fastcgi_index index. php;
Fastcgi_pass 127.0.0.1: 8407;
Fastcgi_param script_filename/www/wwwroot/hao32.com $ fastcgi_script_name;
}
Location/hao32-status {
Stub_status on;
Access_log off;
}
}
# Vi linuxsense.org
The added content is as follows. This is a discuz! Host example of the default rewrite rule
Server {
Listen 80;
SERVER_NAME linuxsense.org www.linuxsense.org;
# Enable log
# Access_log/var/log/linuxsense.org/access.log combined buffer = 32 K;
# Error_log/var/log/linuxsense.org/error.log warn;
Location /{
Index index.html index.htm index. php;
Root/www/wwwroot/linuxsense.org;
Rewrite ^ (. *)/archiver/(FID | tid)-[/W/-] +/. html) $1/archiver/index. php? $2 last;
Rewrite ^ (. *)/Forum-([0-9] +)-([0-9] +)/. html $1/Forumdisplay. php? FID = $2 & page = $3 last;
Rewrite ^ (. *)/thread-([0-9] +)-([0-9] +)-([0-9] + )/. HTML $1/viewthread. PHP? Tid = $2 & extra = page/% 3d $4 & page = $3 last;
Rewrite ^ (. *)/profile-(username | UID)-(. +)/. html $1/viewpro. php? $2 = $3 last;
Rewrite ^ (. *)/space-(username | UID)-(. +)/. html $1/space. php? $2 = $3 last;
Rewrite ^ (. *)/Tag-(. +)/. html $1/Tag. php? Name = $2 last;
Location ~ /. Php $ {
Include fastcgi_params;
Fastcgi_index index. php;
Fastcgi_pass 127.0.0.1: 8408;
Fastcgi_param script_filename/www/wwwroot/linuxsense.org $ fastcgi_script_name;
}
Location/linuxsense-status {
Stub_status on;
Access_log off;
}
}
(3) add website users
Useradd hao32-D/www/wwwroot/hao32.com-S/sbin/nologin
Useradd linuxsense-D/www/wwwroot/linuxsense.org-S/sbin/nologin
If the path after-D exists, a prompt is displayed.
Useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
This does not affect,
Chown-r hao32: hao32/www/wwwroot/hao32.com
Chown-r linuxsense: linuxsense/www/wwwroot/linuxsense
(5) create a user list to be used.
# Vi/usr/local/nginx/CONF/vhosts/user_list
The format is as follows:
Remove-a-p-c-u-F and other parameters. The table sharding is the IP address, port, and user used by PHP on the local machine.
Note: if there are two users, there will be two rows instead of blank lines.
-A 127.0.0.1-P 8407-C 20-u hao32-F
-A 127.0.0.1-P 8408-C 200-u linuxsense-F
(6) Create startup scripts such as PHP-CGI and nginx and add them to system services.
# Vi/etc/init. d/PHP-CGI
# Chmod 755/etc/init. d/PHP-CGI
Edit the PHP-cgi startup script and add the following code:
#! /Bin/bash
#
# PHP-FastCGI control script for Red Hat based Linux.
# Written by hao32
# Chkconfig: 3 89 89
# Description: PhP fast-CGI
# PHP users settings
User_info = "/usr/local/nginx/CONF/vhosts/user_list"
# Source function library
./Etc/init. d/functions
Php_spawn = "/usr/local/PHP-fcgi/bin/spawn-fcgi"
Php_sbin = "/usr/local/PHP-fcgi/bin/PHP-cgi"
Php_pid = "/usr/local/nginx/var/php-fcgi.pid"
Retval = 0
Prog = "php FastCGI"
Mkdir-P/var/run/PHP-fcgi 2>/dev/null
Start (){
Echo-N $ "Starting $ prog :"
Cat $ user_info | while read list
Do
Daemon $ php_spawn $ list $ php_sbin
Done
Retval =$?
If [$ retval-EQ 0]
Then
Touch/var/lock/subsys/PHP-fcgi
Fi
Echo
Return $ retval
}
Stop (){
Echo-N $ "Stopping $ prog :"
Killproc PHP-CGI
Retval =$?
If [$ retval-EQ 0]
Then
Rm-F/var/lock/subsys/PHP-fcgi
Fi
Echo
Return $ retval
}
Restart (){
Stop
Echo-ne "restart.../N"
Sleep 3
Start
}
Case "$1" in
Start)
Start
;;
Stop)
Stop
;;
Restart)
Restart
;;
*)
Echo $ "Usage: $0 {START | stop | restart }"
Retval = 1
Esac
Exit $ retval
# Vi/etc/init. d/nginx
# Chmod 755/etc/init. d/nginx
Edit the nginx STARTUP script and add the following code:
#! /Bin/bash
#
# Nginx control script for Red Hat based Linux.
# Chkconfig: 3 90
# Description: nginx Web Server Control
# Source function library
./Etc/init. d/functions
# Nginx settings
Nginx_sbin = "/usr/local/nginx/sbin/nginx"
Nginx_conf = "/usr/local/nginx/CONF/nginx. conf"
Nginx_pid = "/usr/local/nginx/var/nginx. PID"
Retval = 0
Prog = "nginx"
Mkdir-P/var/run/nginx 2>/dev/null
Start (){
Echo-N $ "Starting $ prog :"
Daemon $ nginx_sbin-C $ nginx_conf
Retval =$?
If [$ retval-EQ 0]
Then
Touch/var/lock/subsys/nginx
Fi
Echo
Return $ retval
}
Stop (){
Echo-N $ "Stopping $ prog :"
Killproc-p $ nginx_pid $ nginx_sbin-term
Retval =$?
If [$ retval-EQ 0]
Then
Rm-F/var/lock/subsys/nginx
Fi
Echo
Return $ retval
}
Reload (){
Echo-N $ "reloading $ prog :"
Killproc-p $ nginx_pid $ nginx_sbin-Hup
Retval =$?
Echo
Return $ retval
}
Restart (){
Stop
Start
}
Configtest (){
$ Nginx_sbin-C $ nginx_conf-T
Exit
}
Case "$1" in
Start)
Start
;;
Stop)
Stop
;;
Reload)
Reload
;;
Restart)
Restart
;;
Configtest)
Configtest
;;
*)
Echo $ "Usage: $0 {START | stop | reload | restart | configtest }"
Retval = 1
Esac
Exit $ retval
Add to system server
Chkconfig -- add nginx
Chkconfig -- add PHP-CGI
Now you can start PHP-CGI and nginx.
Service nginx start
Service PHP-CGI start
Restart
Start installing and configuring MySQL
# Tar zxvf mysql-5.0.51a-linux-i686-glibc23.tar.gz
# Music mysql-5.0.51a-linux-i686-glibc23/usr/local/
# Ln-S/usr/local/mysql-5.0.51a-linux-i686-glibc23/usr/local/MySQL
# Useradd MySQL
# Chown-r MYSQL: Root/usr/local/MySQL/
# Cd/usr/local/MySQL
#./Scripts/mysql_install_db -- user = MySQL
# Cp./support-files/MySQL. Server/etc/rc. d/init. d/mysqld
# Chmod 755/etc/rc. d/init. d/mysqld
# Chkconfig -- add mysqld
# Chkconfig -- level 3 mysqld on
# Cp./support-files/my-huge.cnf/etc/My. CNF
# Mv/usr/local/MySQL/data/var/lib/MySQL
# Chown-r MYSQL: MySQL/var/lib/MySQL
# Cd/usr/local/MySQL/bin
# For I in *; do ln-S/usr/local/MySQL/bin/$ I/usr/bin/$ I; done
Edit/etc/My. CNF
Content:
[Client]
Port = 3306
Socket =/tmp/MySQL. Sock
[Mysqld]
Port = 3306
Socket =/tmp/MySQL. Sock
Max_connections = 1024
# Max_user_connections = 40
Skip-InnoDB
# Skip-network
# Skip-bdb
Datadir =/var/lib/MySQL
Default-character-set = utf8
Long_query_time = 5
Log-Slow-queries =/var/lib/MySQL/slow_queries
Wait_timeout = 5
Thread_stack = 128 K
Interactive_timeout = 20
Skip-locking
Key_buffer = 256 m
Max_allowed_packet = 2 m
Table_cache = 512
Sort_buffer_size = 2 m
Read_buffer_size = 2 m
Read_rnd_buffer_size = 8 m
Myisam_sort_buffer_size = 64 m
Thread_cache_size = 8
Query_cache_size = 32 m
# Try Number of CPU's * 2 for thread_concurrency
Thread_concurrency = 8
Log-bin = mysql-bin
Server-id = 1
[MySQL]
Default-character-set = utf8
Log on to MySQL on the local machine
# Mysql-uroot
Mysql>
Enter (copy and paste) and add a remote connection user. In this example, the user is hao32 and the password is hao32.com.
Then, use software such as phpMyAdmin on the Web server to connect to the database server, and delete and add corresponding users.
Insert into 'mysql'. 'user '(
'Host ',
'User ',
'Password ',
'Select _ priv ',
'Insert _ priv ',
'Update _ priv ',
'Delete _ priv ',
'Create _ priv ',
'Drop _ priv ',
'Reload _ priv ',
'Shutdown _ priv ',
'Process _ priv ',
'File _ priv ',
'Grant _ priv ',
'References _ priv ',
'Index _ priv ',
'Alter _ priv ',
'Show _ db_priv ',
'Super _ priv ',
'Create _ tmp_table_priv ',
'Lock _ tables_priv ',
'Execute _ priv ',
'Repl _ slave_priv ',
'Repl _ client_priv ',
'Create _ view_priv ',
'Show _ view_priv ',
'Create _ routine_priv ',
'Alter _ routine_priv ',
'Create _ user_priv ',
'Ssl _ type ',
'Max _ questions ',
'Max _ updates ',
'Max _ connections ',
'Max _ user_connections'
)
Values (
'%', 'Hao32', password ('hao32. com '), 'y ', 'y ', 'y', ', '0', '0 ', '0', '0'
);
Finished!