CentOS + Nginx + PHP + Mysql (2 ))

Source: Internet
Author: User
Tags sendfile stop script unzip extract zts
CentOS + Nginx + PHP + Mysql (2) (conversion) [use the yum command to configure and upgrade the required Library]

# Sudo-s
# LANG = C
# Yum-y install gcc-c ++ autoconf libjpeg-devel libpng-devel freetype-devel libxml2 libxml2-devel zlib-devel glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses -devel curl-devel
↑ Install and upgrade these libraries

[Download the files required by the environment to the specified directory]

# Mkdir-p/software
Idea create a software folder in the root directory
# Cd/software
Enter the software folder
# Wget http://sysoev.ru/nginx/nginx-0.7.19.tar.gz
# Wget http://www.php.net/get/php-5.2.6.tar.gz/from/this/mirror
# Wget http://php-fpm.anight.org/downloads/head/php-5.2.6-fpm-0.5.9.diff.gz
# Wget http://blog.s135.com/soft/linux/mysql/mysql-5.1.26-rc.tar.gz
# Wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.12.tar.gz
# Wget http://mirror.optus.net/sourceforge/m/mc/mcrypt/libmcrypt-2.5.8.tar.gz
# Wget http://mirror.optus.net/sourceforge/m/mc/mcrypt/mcrypt-2.6.7.tar.gz
# Wget http://pecl.php.net/get/memcache-2.2.3.tgz
# Wget http://mirror.optus.net/sourceforge/m/mh/mhash/mhash-0.9.9.tar.gz
# Wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.7.tar.gz
# Wget http://bart.eaccelerator.net/source/
↑ Download these files to the software Directory

[Compile and install the support libraries required for PHP 5.2.6]

# Tar zxvf libiconv-1.12.tar.gz
Unzip extract (tar) parameter (zxvf) file name (libiconv-1.12.tar.gz)
# Cd libiconv-1.12/
Folder (libiconv-1.12)
#./Configure -- prefix =/usr/local
Configure the installation information and specify the installation directory as/usr/local.
# Make
Unzip make installation file
# Make install
Begin installation
# Cd ../
The parent directory (the software directory) is returned)

# Tar zxvf libmcrypt-2.5.8.tar.gz
# Cd libmcrypt-2.5.8/
# Make
# Make install
# Cd libltdl/
#./Configure -- enable-ltdl-install
# Make
# Make install
# Cd http://www.cnblogs.com/

# Tar zxvf mhash-0.9.9.tar.gz
# Cd mhash-0.9.9/
# Make
# Make install
# Cd ../

# Cp/usr/local/lib/libmcrypt. */usr/lib
# Ln-s/usr/local/lib/libmhash. so.2/usr/lib/libmhash. so.2

# Tar zxvf mcrypt-2.6.7.tar.gz
# Cd mcrypt-2.6.7/
# Make
# Make install
# Cd ../

[Compile and install MySQL 5.1.26-rc]

#/Usr/sbin/groupadd mysql
↑ Create a mysql User Group
#/Usr/sbin/useradd-g mysql
↑ Create a mysql user to the mysql User Group
# Tar zxvf mysql-5.1.26-rc.tar.gz
# Cd mysql-5.1.26-rc/
#. /Configure -- prefix =/usr/local/webserver/mysql/-- enable-starter er -- with-extra-charsets = complex -- enable-thread-safe-client -- with-big-tables -- with-readline -- with-ssl -- with-embedded-server -- enable-local-infile
# Make & make install
# Chmod + w/usr/local/webserver/mysql
# Chown-R mysql: mysql/usr/local/webserver/mysql
# Cp support-files/my-medium.cnf/usr/local/webserver/mysql/my. cnf
# Cd ../

#/Usr/local/webserver/mysql/bin/mysql_install_db -- defaults-file =/usr/local/webserver/mysql/my. cnf -- basedir =/usr/local/webserver/mysql -- datadir =/usr/local/webserver/mysql/data -- user = mysql -- pid-file =/usr/local/webserver /mysql. pid -- skip-locking -- port = 3306 -- socket =/tmp/mysql. sock
↑ Create a data table as a mysql User account

#/Bin/sh/usr/local/webserver/mysql/bin/mysqld_safe -- defaults-file =/usr/local/webserver/mysql/my. cnf &
↑ Start MySQL (last & indicates running in the background)

[Compile and install PHP (FastCGI mode)]

# Tar zxvf php-5.2.6.tar.gz
# Gzip-cd php-5.2.6-fpm-0.5.9.diff.gz | patch-d php-5.2.6-p1
# Cd php-5.2.6/
#. /Configure -- prefix =/usr/local/webserver/php -- with-config-file-path =/usr/local/webserver/php/etc -- with-mysql =/usr/ local/webserver/mysql -- with-mysqli =/usr/local/webserver/mysql/bin/mysql_config -- with-iconv-dir =/usr/local -- with-freetype-dir -- with-jpeg-dir -- with-png-dir -- with-zlib -- with-libxml-dir =/usr -- 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
# Sed-I's #-lz-lm-lxml2-lz-lm-lxml2-lz-lm-lcrypt # &-liconv # 'makefile
# Make
# Make install
# Cp php. ini-dist/usr/local/webserver/php/etc/php. ini
# Cd ../

[Compile and install the PHP5 extension module]

# Tar zxvf memcache-2.2.3.tgz
# Cd memcache-2.2.3/
#./Configure -- with-php-config =/usr/local/webserver/php/bin/php-config
# Make
# Make install
# Cd ../

# Tar jxvf eaccelerator-
# Cd eaccelerator-
#./Configure -- enable-eaccelerator = shared -- with-php-config =/usr/local/webserver/php/bin/php-config
# Make
# Make install
# Cd ../

[Modify the php. ini file]

Manual modification:
# Vi/usr/local/webserver/php/etc/php. ini
Set "extension_dir = "./""
Change to "extension_dir ="/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613 /""

Set "output_buffering = Off"
Change to "output_buffering = On"

Extension = "memcache. so"
Add this line at the end of the hosts file

Automatic modification (skipped when manual modification is used ):
# Sed-I's # extension_dir = ". /"# extension_dir ="/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/"\ nextension =" memcache. so "\ n # '/usr/local/webserver/php/etc/php. ini
# Sed-I's # output_buffering = Off # output_buffering = On # '/usr/local/webserver/php/etc/php. ini

[Configure eAccelerator to accelerate PHP]

# Mkdir-p/usr/local/webserver/eaccelerator_cache
# Vi/usr/local/webserver/php/etc/php. ini
Press shift + g to jump to the end of the configuration file and add the following configuration information:

Zend_extension = "/usr/local/webserver/php/lib/php/extension/ no-debug-non-zts-20060613/eaccelerator. so"
Eaccelerator. shm_size = "128"
Eaccelerator. cache_dir = "/usr/local/webserver/eaccelerator_cache"
Eaccelerator. enable = "1"
Eaccelerator. optimizer = "1"
Eaccelerator. check_mtime = "1"
Eaccelerator. debug = "0"
Eaccelerator. filter = ""
Eaccelerator. shm_max = "0"
Eaccelerator. shm_ttl = "300"
Eaccelerator. shm_prune_period = "120"
Eaccelerator. shm_only = "0"
Eaccelerator. compress = "1"
Eaccelerator. compress_level = "9"

# Vi/etc/sysctl. conf
Modify configuration file

Set "kernel. shmmax = **********"
Change to "kernel. shmmax = 134217728"

↑ Execute this command to make the configuration take effect

[Create www users and groups]

#/Usr/sbin/groupadd www-g 48
↑ Create a www user group and specify the group ID as 48
#/Usr/sbin/useradd-u 48-g www
↑ Create a www user to the www user group
# Mkdir-p/wwwroot
Create the wwwroot website directory in the root directory
# Chmod + w/wwwroot
Supervisor adds writable permissions to the wwwroot directory.
# Chown-R www: www/wwwroot
Users: Set the user group of the wwwroot directory to www and the user to www.

[Create a php-fpm configuration file]

Php-fpm is a FastCGI management patch for PHP. it can smoothly change the php. ini configuration without restarting php-cgi.
# Rm-f/usr/local/webserver/php/etc/php-fpm.conf
Deleting original php-fpm.conf files
# Vi/usr/local/webserver/php/etc/php-fpm.conf
Create a new php-fpm.conf file and start the vi editor to edit it
Enter the following content (note that the content after "marker" cannot appear in the actual file ):

All relative paths in this config are relative to php's install prefix

Pid file

Error log file

Log level

When this amount of php processes exited with SIGSEGV or SIGBUS...

... In a less than this interval of time, a graceful restart will be initiated.
Useful to work around und accidental curruptions in accelerator's shared memory.
1 m

Time limit on waiting child's reaction on signals from master

Set to 'no' to debug fpm

Name of pool. Used in logs and stats.

Address to accept fastcgi requests on.
Valid syntax is 'IP. ad. re. ss: Port' or just 'port' or '/path/to/unix/socket' 9000

Set listen (2) backlog

Set permissions for unix socket, if one used.
In Linux read/write permissions must be set in order to allow connections from web server.
Extends BSD-derrived systems allow connections regardless of permissions.


Additional php. ini defines, specific to this pool of workers.

If Nginx + PHP is installed for program debugging, set it to "1" to display the PHP error message. if it is set to "0", Nginx will report a blank error page in the status of 500.

Unix user of processes

Unix group of processes

Process manager settings

Sets style of controling worker process count.
Valid values are 'static 'and 'Apache-like'

Sets the limit on the number of simultaneous requests that will be served.
Equivalent to Apache MaxClients directive.
Equivalent to PHP_FCGI_CHILDREN environment in original php. fcgi
Used with any pm_style.
The number of worker processes is 64. if the server memory is greater than 3 GB, you can enable only-processes.

Settings group for 'Apache-like' pm style

Sets the number of server processes created on startup.
Used only when 'Apache-like 'pm_style is selected

Sets the desired minimum number of idle server processes.
Used only when 'Apache-like 'pm_style is selected

Sets the desired maximum number of idle server processes.
Used only when 'Apache-like 'pm_style is selected

The timeout (in seconds) for serving a single request after which the worker process will be terminated
Shocould be used when 'max _ execution_time 'ini option does not stop script execution for some reason
'0s' means 'off'
0 s

The timeout (in seconds) for serving of single request after which a php backtrace will be dumped to slow. log file
'0s' means 'off'
0 s

The log file for slow requests
Logs/slow. log

Set open file desc rlimit

Set max core size rlimit

Chroot to this directory at the start, absolute path

Chdir to this directory at the start, absolute path

Redirect workers 'stdout and stderr into main error log.
If not set, they will be redirected to/dev/null, according to FastCGI specs

How much requests each process shocould execute before respawn.
Useful to work around memory leaks in 3rd party libraries.
For endless request processing please specify 0

Comma separated list of ipv4 addresses of FastCGI clients that allowed to connect.
Equivalent to FCGI_WEB_SERVER_ADDRS environment in original php. fcgi (5.2.2 +)
Makes sense only with AF_INET listening socket.

Pass environment variables like LD_LIBRARY_PATH
All $ VARIABLEs are taken from current environment


[Start the php-cgi process and listen to Port 9000 of]

# Ulimit-shn51200
#/Usr/local/webserver/php/sbin/php-fpm start

[Install the pcre library required by Nginx]

# Tar zxvf pcre-7.7.tar.gz
# Cd pcre-7.7/
# Make & make install
# Cd ../

[Install Nginx 0.7.19]

# Tar zxvf nginx-0.7.19.tar.gz
# Cd nginx-0.7.19/
#./Configure -- user = www -- group = www -- prefix =/usr/local/webserver/nginx -- with-http_stub_status_module -- with-http_ssl_module
# Make
# Make install
# Cd ../

[Create Nginx log Directory]

# Mkdir-p/logs
# Chmod + w/logs
# Chown-R www: www/logs

[Create an nginx. conf configuration file]

# Rm-f/usr/local/webserver/nginx/conf/nginx. conf
# Vi/usr/local/webserver/nginx/conf/nginx. conf
Enter the following content (note that the content after "marker" cannot appear in the actual file ):

User www;

Worker_processes 8;
Every process of ingress Nginx consumes 10 MB ~ 12 MB memory

Error_log/logs/nginx_error.log warn;

Pid/usr/local/webserver/nginx. pid;

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

Use epoll;
Worker_connections 51200;

Include mime. types;
Default_type application/octet-stream;

# Charset gb2312;

Server_names_hash_bucket_size 128;
Client_header_buffer_size 32 k;
Large_client_header_buffers 4 32 k;

Sendfile on;
Tcp_nopush on;

Keepalive_timeout 60;

Tcp_nodelay on;

Fastcgi_connect_timeout 300;
Fastcgi_send_timeout 300;
Fastcgi_read_timeout 300;
Fastcgi_buffer_size 64 k;
Fastcgi_buffers 4 64 k;
Fastcgi_busy_buffers_size 128 k;
Fastcgi_temp_file_write_size 128 k;

Gzip on;
Gzip_min_length 1 k;
Gzip_buffers 4 16 k;
Gzip_http_version 1.0;
Gzip_comp_level 2;
Gzip_types text/plain application/x-javascript text/css application/xml;
Gzip_vary on;

Limit_zone crash $ binary_remote_addr 10 m;
Explain defines a record area called "crash" with a total capacity of 10 MB. the variable $ binary_remote_addr is used as the benchmark for session judgment (that is, one address and one session ), when the partition size is 1 MB, about 32000 sessions can be recorded (one session occupies 32 bytes)

Listen 80;
Index index.html index.htm index. php;

# Limit_conn crash 5;
Comment * has been commented out by #. that is, it does not work. * defines the restrictions of the entire website. In the "crash" record area, the variable $ binary_remote_addr is used as the benchmark for session determination (that is, one address and one session) to restrict the Global Directory of the website, one session can only have five connections (that is, one IP address can only initiate five connections, more than five connections, always with 503 errors)

Location ~ . * \. (Php | php5 )? $
# Fastcgi_pass unix:/tmp/php-cgi.sock;
Fastcgi_pass 9000;
Fastcgi_index index. php;
Fcinclude GI. conf;

Location ~ . * \. (Gif | jpg | jpeg | png | bmp | swf) $
Expires 30d;

Location ~ . * \. (Js | css )? $
Expires 1 h;

Location/resource /{
Limit_conn crash 2;
Limit defines the limits of the resource directory. In the "crash" record area, the variable $ binary_remote_addr is used as the benchmark for session judgment (that is, one address and one session), which limits the resource directory, one session can only have two connections (that is, one IP address can only initiate two connections, and there are more than two connections, all of which are 503 errors)

Log_format access' $ 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 access;
Sendfile on;
Tcp_nopush on;
Client_max_body_size 50 m;
The maximum size allowed for uploads in The Ghost website program. it is set to 50 MB here, which is only the nginx limit. PHP itself is limited to 2 MB.

[Create fcgi. conf configuration file]

# Vi/usr/local/webserver/nginx/conf/fcgi. conf
Enter the following content:

Fastcgi_param GATEWAY_INTERFACE CGI/1.1;
Fastcgi_param SERVER_SOFTWARE nginx;

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_FILENAME $ document_root $ fastcgi_script_name;
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 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;

[Start Nginx]

# Ulimit-shn51200

[Smoothly change the Nginx configuration without stopping the Nginx service]

After modifying the/usr/local/webserver/nginx/conf/nginx. conf configuration file, run the following command to check whether the configuration file is correct:
If you test OK successfully, run the following command to restart Nginx:
(1st types) # pkill nginx
#/Usr/local/webserver/nginx/conf/nginx. conf
(2nd types) # kill-HUP 'cat/usr/local/webserver/nginx. Pi'
#/Usr/local/webserver/nginx/conf/nginx. conf
(3rd types) # ps-ef | grep "nginx: master process" | grep-v "grep" | awk-F ''' {print $2 }'
# Kill-HUP number
This number is from the number output on the screen after the previous command is executed, that is, the pid process Number of Nginx
#/Usr/local/webserver/nginx/conf/nginx. conf

If the following two lines of information are displayed, the configuration file is correct:
The configuration file/usr/local/webserver/nginx/conf/nginx. conf syntax is OK
The configuration file/usr/local/webserver/nginx/conf/nginx. conf was tested successfully

[Configure to automatically start Nginx + PHP + MySQL at startup]

# Vi/etc/rc. local
Add the following content at the end:
/Bin/sh/usr/local/webserver/mysql/bin/mysqld_safe -- defaults-file =/usr/local/webserver/mysql/my. cnf &
Ulimit-SHn 51200
/Usr/local/webserver/php/sbin/php-fpm start

[Optimize Linux kernel parameters]

# Vi/etc/sysctl. conf
Add the following content at the end of the file:

Net. ipv4.tcp _ fin_timeout = 30
Net. ipv4.tcp _ keepalive_time = 300
Net. ipv4.tcp _ syncookies = 1
Net. ipv4.tcp _ tw_reuse = 1
Net. ipv4.tcp _ tw_recycle = 1
Net. ipv4.ip _ local_port_range = 5000 65000

Configure to take effect immediately

[Compile a daily script to regularly cut Nginx logs]

# Vi/usr/local/webserver/nginx/sbin/cut_nginx_log.sh
↑ Create a cut script

Enter the following content:
#! /Bin/bash
# This script run at 00:00

# The Nginx logs path
Logs_path = "/logs /"

Mkdir-p $ {logs_path} $ (date-d "yesterday" + "% Y")/$ (date-d "yesterday" + "% m ")/
Mv $ {logs_path} access. log $ {logs_path} $ (date-d "yesterday" + "% Y")/$ (date-d "yesterday" + "% m ") /access _ $ (date-d "yesterday" + "% Y % m % d "). log
Mv $ {logs_path} nginx_error.log $ {logs_path} $ (date-d "yesterday" + "% Y")/$ (date-d "yesterday" + "% m ") /nginx_error _ $ (date-d "yesterday" + "% Y % m % d "). log
Kill-USR1 'cat/usr/local/webserver/nginx. pid'

[Set a scheduled task for log splitting]

# Crontab-e
↑ Edit the scheduler task list

Enter the following content:
00 00 ***/bin/bash/usr/local/webserver/nginx/sbin/cut_nginx_log.sh
↑ Cut nginx access logs at every day

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.