CentOS6.5 install and configure Tengine

Source: Internet
Author: User
Tags gz file openssl library

CentOS6.5 install and configure Tengine
1. Install pcre:
Copy code

Cd/usr/local/src
Wget http://downloads.sourceforge.net/project/pcre/pcre/8.34/pcre-8.34.tar.gz

Tar zxvf pcre-8.34.tar.gz
Cd pcre-8.34
./Configure -- prefix =/usr/local/pcre
Make
Make install

Copy code
Ii. Download The proxy_cache plug-in

Cd/usr/local/src

Wget http://labs.frickle.com/files/ngx_cache_purge-2.1.tar.gz

Tar zxvf ngx_cache_purge-2.1.tar.gz



3. Install tengine
Press Ctrl + C to copy the code
Press Ctrl + C to copy the code

/Usr/local/nginx/sbin/nginx # Start nginx
Chown nobody. nobody-R/usr/local/nginx/html
Chmod 700-R/usr/local/nginx/html

If there is a compilation problem, check whether the cause is as follows:

./Configure: error: the http ssl module requires OpenSSL library
Cause: openssl library is required to install the http_ssl_module.
Solution: yum install openssl-devel
./Configure: error: the HTTP rewrite module requires the PCRE library.
Cause: to install the http_rewrite_module, you must first install the PCRE development kit.
Solution: yum install pcre-devel



Note:

-- With-pcre =/usr/local/src/pcre-8.21 points to the source package unzipping path, rather than the installation path, otherwise an error is reported.

-- Add-module =/usr/local/src/ngx_cache_purge-2.1 is the plug-in module that loads the cache
4. Set Tengine to start

Vi/etc/rc. d/init. d/nginx # edit the Startup File and add the following content
Copy code

#! /Bin/bash
# Tengine Startup script # processname: nginx
# Chkconfig:-85 15
# Description: nginx is a World Wide Web server. It is used to serve
# Pidfile:/var/run/nginx. pid
# Config:/usr/local/nginx/conf/nginx. conf
Nginxd =/usr/local/nginx/sbin/nginx
Nginx_config =/usr/local/nginx/conf/nginx. conf
Nginx_pid =/usr/local/nginx/logs/nginx. pid
RETVAL = 0
Prog = "nginx"
# Source function library.
./Etc/rc. d/init. d/functions
# Source networking configuration.
./Etc/sysconfig/network
# Check that networking is up.
[$ {NETWORKING} = "no"] & exit 0
[-X $ nginxd] | exit 0
# Start nginx daemons functions.
Start (){
If [-e $ nginx_pid]; then
Echo "tengine already running ...."
Exit 1
Fi
Echo-n $ "Starting $ prog :"
Daemon $ nginxd-c $ {nginx_config}
RETVAL =$?
Echo
[$ RETVAL = 0] & touch/var/lock/subsys/nginx
Return $ RETVAL
}
# Stop nginx daemons functions.
Stop (){
Echo-n $ "Stopping $ prog :"
Killproc $ nginxd
RETVAL =$?
Echo
[$ RETVAL = 0] & rm-f/var/lock/subsys/nginx/usr/local/nginx/logs/nginx. pid
}
Reload (){
Echo-n $ "Reloading $ prog :"
# Kill-HUP 'cat $ {nginx_pid }'
Killproc $ nginxd-HUP
RETVAL =$?
Echo
}
# See how we were called.
Case "$1" in
Start)
Start
;;
Stop)
Stop
;;
Reload)
Reload
;;
Restart)
Stop
Start
;;

Status)
Status $ prog
RETVAL =$?
;;
*)
Echo $ "Usage: $ prog {start | stop | restart | reload | status | help }"
Exit 1
Esac
Exit $ RETVAL

Copy code

Save and exit

Chmod 775/etc/rc. d/init. d/nginx # grant the File Execution permission
Chkconfig -- level 012345 nginx on # Set startup
/Etc/rc. d/init. d/nginx restart

4. Configure Tengine
Back up the nginx initial configuration file. We need to recreate the configuration file.

Mv/usr/local/nginx/conf/nginx. conf/usr/local/nginx/conf/nginx. conf. bak

Create nginx user www

Groupadd www
Useradd-g www

Edit the main configuration file:

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

The content is as follows:
Copy code

User www;
Worker_processes 4; # Number of worker processes, which is the number of CPU cores or twice
Error_log logs/error. log crit; # debug | info | notice | warn | error | crit
Pid logs/nginx. pid;

# Specifies the value for maximum file descriptors that can be opened by this process.
Worker_rlimit_nofile 65535;

Events {
Use epoll; # The most common event triggering mechanism supporting high concurrency in Linux
Worker_connections 65535;
}

Http {
Include mime. types; # sets the mime type, which is defined by the mime. type file.
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 "';
Access_log logs/access. log main;

# Set Request Buffer
Server_names_hash_bucket_size 256; # increase, originally 128
Client_header_buffer_size 256 k; # increase, originally 32 k
Large_client_header_buffers 4 256 k; # added, originally 32 k

# Size limits
Client_max_body_size 50 m; # maximum number of bytes allowed for client requests per file
Client_header_timeout 3 m;
Client_body_timeout 3 m;
Send_timeout 3 m;

Sendfile on;
Tcp_nopush on;
Keepalive_timeout 60;
Tcp_nodelay on;
Server_tokens on; # Do Not Display nginx version information

Limit_conn_zone $ binary_remote_addr zone = perip: 10 m; # Add limit_zone to limit the concurrency of the same IP address
# Fastcgi_intercept_errors on; # enable error page Jump

Include gzip. conf; # compress the configuration file
Include proxy. conf; # proxy_cache parameter configuration file
Include vhost/*. conf; # nginx virtual host contains the file directory
Include mysvrhost. conf; # Back-end WEB server list file
}

Copy code

Edit the proxy configuration file:

Cd/usr/local/nginx/conf/
Mkdir vhost
Vi/usr/local/nginx/conf/proxy. conf

The content is as follows:
Copy code

# Note: The paths specified by proxy_temp_path and proxy_cache_path must be in the same partition.
Proxy_temp_path/tmp/proxy_temp;

# Set the name of the Web cache area to cache_one. The size of the memory cache space is 500 MB. The content not accessed within one day is automatically cleared. The size of the hard disk cache space is 30 GB.
Proxy_cache_path/tmp/proxy_cache levels = keys_zone = cache_one: 500 m inactive = 1d max_size = 30g;

Client_body_buffer_size 512 k; #512 KB
Proxy_connect_timeout 50; # proxy connection timeout
Proxy_read_timeout 600; # proxy sending timeout
Proxy_send_timeout 600; # proxy receiving timeout
Proxy_buffer_size 128 k; # proxy buffer size, originally 32 k
Proxy_buffers 16 256 k; # Proxy Buffer, originally 4 64 k
Proxy_busy_buffers_size 512 k; # buffer size under high load, originally 128 k
Proxy_temp_file_write_size 1024 m; # the size of the temporary file cached by proxy is 128 kb.
# Proxy_ignore_client_abort on; # The proxy is not allowed to close the connection.
Proxy_next_upstream error timeout invalid_header http_500 http_503 http_404 http_502 http_504;

Copy code

Edit the host configuration file:

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

The content is as follows:

Upstream cn100 {
Ip_hash; # session persistence
Server 127.0.0.1: 8080 max_fails = 1 fail_timeout = 60 s;
Server 127.0.0.1: 9080 max_fails = 1 fail_timeout = 60 s;
}

Edit the compressed configuration file:

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

The content is as follows:
Copy code

# GZIP compression settings
#2012.4.2
# You can use http://tool.chinaz.com/gzips/to check the compression condition.
#
# Start the pre-compression function, which is effective for all types of files
# Gzip_static on; # After nginx_static is enabled, the system first checks whether a corresponding gz file exists for any file.

# Pre-compressed files cannot be found for Dynamic Compression
Gzip on;
Gzip_min_length 1 k; # sets the minimum compression value. The unit is bytes. The value exceeding the set min_length will be compressed, and smaller than will not be compressed.
Gzip_comp_level 3; # Set the compression level. values 1-9 and 1 are the minimum compression speed and the fastest compression speed. 9 is the opposite. The maximum compression speed is the slowest, and many CPU resources are consumed.
Gzip_buffers 16 64 k; # Set the cache size of the system to store the data stream of GZIP compression results. This avoids the need for nginx to apply for compression space from the system.
Gzip_types text/plain application/x-javascript text/css text/javascript;

# For gzip_types, If You Want To enable gzip compression for images, use the following section:
# Gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php image/jpeg image/gif image/png;

# Gzip public Configuration
Gzip_http_version 1.1; # identify the http protocol version (1.0/1.1)
Gzip_proxied any; # Sets whether to perform compression when using a proxy. The default value is off.
Gzip_vary on; # It is related to the http header. Add a vary header and the proxy determines whether compression is required.
Gzip_disable "MSIE [1-6]."; # disable gzip compression for IE6

Copy code

Edit the configuration file:

Vi/usr/local/nginx/conf/vhost/cn100.conf

The content is as follows:
Copy code

Server {
Listen 80;
Server_name localhost;
# Default Startup File
Index index.html index.htm;

# Configure the publishing directory as/usr/local/tomcat1/webapps/ROOT
Root/usr/local/tomcat1/webapps/ROOT;

Location/
{
# If the backend server returns errors such as 502, 504, and execution timeout, the request is automatically forwarded to another server in the upstream Server Load balancer pool for failover.
Proxy_next_upstream http_502 http_504 error timeout invalid_header;
Proxy_cache cache_one;


# Set different cache times for different HTTP Status Codes
Proxy_cache_valid 200 304 12 h;
# Combine domain names, Uris, and parameters into the Key values of the Web cache. Nginx hashes the cached content to the second-level cache directory based on the Key values.
Proxy_cache_key $ host $ uri $ is_args $ args;

Proxy_set_header Host $ host;
Proxy_set_header X-Real-IP $ remote_addr;
Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
Proxy_pass http: // cn100;


Proxy_pass_header Set-Cookie;

# Transmit the Set-Cookie http header to the user. Otherwise, some applications that contain cookies, such as my typecho

# Expiration time 3 days
Expires 3d;
}

# It is used to clear the cache. If a URL is http: // 192.168.8.42/test.txt, you can clear the cache of the URL by accessing http: // 192.168.8.42/purge/test.txt.
Location ~ /Purge (/.*)
{
# Only the specified IP address or IP segment is allowed to clear the URL cache.
Allow 127.0.0.1;
Allow 192.168.0.0/16;
Deny all;
Proxy_cache_purge cache_one $ host $1 $ is_args $ args;
}



# View the nginx concurrent connection count Configuration
Location/NginxStatus
{
Stub_status on;
Access_log off;
Auth_basic "NginxStatus ";
}

# Define the path of Nginx output logs
# Access_log/data/logs/nginx_wugk/access. log main;
# Error_log/data/logs/nginx_wugk/error. log crit;
# Access_log off; # select whether to enable access logs based on your own needs. If you comment out this parameter, it indicates that access logs are enabled.
Error_page 404/404 .html;
Error_page 500 502 503 504/404 .html;
Location =/404.html {
Root html;
}
Limit_conn perip 50; # if the number of concurrent ip addresses exceeds 50, 503 is returned.
}



Copy code

Configure TCP settings for Tengine and optimize the settings:

Vi/etc/sysctl. conf

The content is as follows:
Copy code

Net. ipv4.ip _ forward = 0
Net. ipv4.conf. default. rp_filter = 1
Net. ipv4.conf. default. accept_source_route = 0
Kernel. sysrq = 0
Kernel. core_uses_pid = 1
Net. ipv4.tcp _ syncookies = 1
Kernel. msgmnb = 65536
Kernel. msgmax = 65536
Kernel. shmmax = 68719476736
Kernel. shmall = 4294967296
Net. ipv4.tcp _ max_tw_buckets = 6000
Net. ipv4.tcp _ sack = 1
Net. ipv4.tcp _ window_scaling = 1
Net. ipv4.tcp _ rmem = 4096 87380 4194304
Net. ipv4.tcp _ wmem = 4096 16384 4194304
Net. core. wmem_default = 8388608
Net. core. rmem_default = 8388608
Net. core. rmem_max = 16777216
Net. core. wmem_max = 16777216
Net. core. netdev_max_backlog = 262144
Net. core. somaxconn = 262144
Net. ipv4.tcp _ max_orphans = 3276800
Net. ipv4.tcp _ max_syn_backlog = 262144
Net. ipv4.tcp _ timestamps = 0
Net. ipv4.tcp _ synack_retries = 1
Net. ipv4.tcp _ syn_retries = 1
Net. ipv4.tcp _ tw_recycle = 1
Net. ipv4.tcp _ tw_reuse = 1
Net. ipv4.tcp _ mem = 94500000 915000000 927000000
Net. ipv4.tcp _ fin_timeout = 1
Net. ipv4.tcp _ keepalive_time = 30
Net. ipv4.ip _ local_port_range = 1024 65000

# Permitted port range

Copy code

Make the configuration take effect immediately

/Sbin/sysctl-p

Create a script to restart all

Vi/root/restartall
Copy code

#! /Bin/sh

#

# Restart the memcached Process

Service memcached restart

# Clearing logs

Rm-f/usr/local/tomcat1/logs /*

Rm-f/usr/local/tomcat2/logs /*

# Clear Cache

Rm-rf/tmp/proxy_cache

# Restart tomcat

/Usr/local/tomcat1/bin/shutdown. sh

/Usr/local/tomcat2/bin/shutdown. sh

/Usr/local/tomcat1/bin/startup. sh

/Usr/local/tomcat2/bin/startup. sh



# Restart nginx
Service nginx restart


Copy code

Grant operation permission

Chmod 777/root/restartall

To restart the service later, you only need:

/Root/restartall

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.