Debian Compile installation NGINX+MYSQL+PHP-FPM environment detailed

Source: Internet
Author: User
Tags chmod curl fpm ftp mcrypt openssl mysql database zend


Linux environment: Debian 6 Stable

Program Version selection:

Nginx:tengine 1.2.4
Taobao produced, based on Nginx and introduced a number of new features

mysql:5.1.66
The latest version of the 5.1 series. The reason for not 5.5 is that 5.5 started compiling using CMake, and the data is relatively small. (In fact, Wangyan's script uses 5.5, but I haven't seen it yet ...). )

php:5.4.9
A few days ago just out of the current stable, with FPM, and nginx with good.

Resources:

The main reference to the Lnmp script and Wangyan lanmp script. The script used by the military is very wide and has a good reputation, but the software version is older. Wangyan's LANMP script insists on using the latest version of the software (PHP directly grabs the current stable version number from the Web page and downloads it), and the code structure is awesome, and it's very clear to read.

Nginx (Tengine)

1. Pre-Installation Preparation

The first is to install the required packages:

Apt-get install build-essential OpenSSL libssl0.9 Libssl-dev

In fact, because it is a retrospective so the specific needs of the package I also remember not clear ... That's basically it. In fact, do not worry about what will be, do configure when there will be a hint, the lack of what to install what can be.

Then prepare users and directories:


Groupadd www
useradd-m-s/usr/sbin/nologin-g www www

Mkdir-p/home/wwwroot
chmod +w/home/wwwroot
Mkdir-p/home/wwwlogs
chmod 777/home/wwwlogs

2. Compile and install Pcre


wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.tar.gz
Tar zxf pcre-8.21.tar.gz
CD pcre-8.21
./configure
Make && Make install

3. Compile and install Tengine


wget http://tengine.taobao.org/download/tengine-1.4.2.tar.gz
Tar zxf tengine-1.4.2.tar.gz
CD tengine-1.4.2
./configure--user=www--group=www--prefix=/usr/local/nginx--with-http_stub_status_module--with-http_ssl_module- -with-http_gzip_static_module
Make && Make install

The compilation parameters are copied by the military brother's script. There are many more parameters to be used in Wangyan scripts. Just for now, I think that's enough.

4. Configure Tengine

Configuration files are mainly referred to the nginx.conf written by the military, the fcgi.conf for fastcgi configuration is completely copied. The specific parameters I have not studied the role, and so on when free time to see.

My nginx.conf (main configuration file) is as follows, there are some of the parameters are Tengine unique, part of the configuration is my environment to add changes, are annotated.


User www www.
Worker_processes Auto; #auto这个值是tengine添加的, the original nginx does not exist. can automatically determine the number of worker per CPU core
Error_log/home/wwwlogs/nginx_error.log Crit;
Pid/usr/local/nginx/logs/nginx.pid;

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

Events
{
Use Epoll;
Worker_connections 51200;
}

http
{
Include Mime.types;
Default_type Application/octet-stream;

Server_names_hash_bucket_size 128;
Client_header_buffer_size 32k;
Large_client_header_buffers 4 32k;
Client_max_body_size 50m;

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 64k;
Fastcgi_buffers 4 64k;
Fastcgi_busy_buffers_size 128k;
Fastcgi_temp_file_write_size 256k;

gzip on;
Gzip_min_length 1k;
Gzip_buffers 4 16k;
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 crawler $binary _remote_addr 10m;

#log format
Log_format access ' $remote _addr-$remote _user [$time _local] ' $request '
' $status $body _bytes_sent ' $http _referer '
' $http _user_agent ' $http _x_forwarded_for ';

Server #此段server是禁止通过ip/Not configured domain name access, quite useful
{
server_name _;
return 403;
}
Server #默认段
{
Listen 80;
server_name site domain name;
Index index.html index.htm index.php;
Root/home/wwwroot;

Location ~. *\. (PHP|PHP5)? $
{
Try_files $uri = 404;
Fastcgi_pass Unix:/tmp/php-fpm.sock; #我的php-fpm configured to work with UNIX sockets, if it is a listening port remember to modify the configuration
Fastcgi_index index.php;
Include fcgi.conf; #此处fcgi. conf configuration fastcgi related parameters, I use the military version of Nginx, the default is fastcgi.conf
}

Location/status {
Stub_status on;
Access_log off;
}

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

Location ~. *\. (JS|CSS)? $
{
Expires 12h;
}

Access_log/home/wwwlogs/access.log access;
}
Include vhost/*.conf; #将其它虚拟主机的配置文件置于vhost子文件夹下即可通过此语句自动生效
}

5. Configure Startup scripts

Nginx startup scripts are very versatile, and I'll just borrow the script provided by the military:


Wget-c Http://soft.vpser.net/lnmp/ext/init.d.nginx
CP Init.d.nginx/etc/init.d/nginx
chmod +x/etc/init.d/nginx
Update-rc.d-f Nginx Defaults
Configuration complete. At this time casually to wwwroot under the cat >> index.html point content can access the test. Direct access to http://domain/status can also be able to see the Tengine state module.

Mysql

1. Pre-installation configuration

The same is the first installation of the use of the package, and then configure the user ... Then again I also put the specific dependencies on which to forget =. = still need nothing to fill what on it.


Apt-get Install LIBNCURSES5 Libncurses5-dev
Groupadd MySQL
Useradd-m-s/usr/sbin/nologin-g MySQL MySQL

2. Compile and install MySQL

Let's spit out a groove ... MySQL Source package download address is really hard to find, I was finally released from the news on the 5.1.66 found, the official website I turned over a half-day Leng did not turn to the direct address.


wget ftp://mirror.switch.ch/mirror/mysql/Downloads/MySQL-5.1/mysql-5.1.66.tar.gz
Tar zxf mysql-5.1.66.tar.gz
CD mysql-5.1.66
./configure--prefix=/usr/local/mysql--with-extra-charsets=complex--enable-thread-safe-client--enable-assembler --with-mysqld-ldflags=-all-static--with-charset=utf8--enable-thread-safe-client--with-big-tables-- With-readline--with-ssl--with-embedded-server--enable-local-infile
Make && Make install
MySQL compiled for a long time, it is recommended to open screen compilation and then go for a cup of coffee and so on ... You can see from the configuration parameters that I did not install InnoDB. You need to add-with-plugins=innobase if you want to pretend. The early MySQL default is to compile the version with debug information, since 5.1.16 the default version of the release without debug information (if you need to add-with-debug). The performance gap between the two is said to be 5%-10%. In addition,-with-mysqld-ldflags=-all-static is also an important parameter for performance improvement.

3. Configure MySQL


Cp/usr/local/mysql/share/mysql/my-medium.cnf/etc/my.cnf
Sed-i ' s/skip-locking/skip-external-locking/g '/etc/my.cnf

The primary configuration file. The second sentence of the role is to change the skip-locking to skip-external-locking. MySQL5.5 will discard the skip-locking option, and there will be an error. And now the 5.1 series will suggest replacing with skip-external-locking.

Next, install the MySQL database:

/usr/local/mysql/bin/mysql_install_db--user=mysql--basedir=/usr/local/mysql--datadir=/usr/local/mysql/var

To configure startup scripts:

Cp/usr/local/mysql/share/mysql/mysql.server/etc/init.d/mysql
chmod 755/etc/init.d/mysql

Perform security installation cleanup:

/usr/local/mysql/bin/mysql_secure_installation

There is no password for the mysql,root just after the installation. Executing this script will help you set the password for root and delete the anonymous account and test database in the database.

Then clean up the permissions and symbolic links, this part from the military Lnmp script, the specific role I am not very clear:


Chown-r Mysql/usr/local/mysql/var
Chgrp-r mysql/usr/local/mysql/.

Cat > /etc/ld.so.conf.d/mysql.conf<< Eof
/usr/local/mysql/lib/mysql
/usr/local/lib
Eof
Ldconfig

Ln-s/usr/local/mysql/share/mysql/usr/share/
Ln-s/usr/local/mysql/lib/mysql/usr/lib/mysql
Ln-s/usr/local/mysql/include/mysql/usr/include/mysql

Ln-s/usr/local/mysql/bin/mysql/usr/bin/mysql
Ln-s/usr/local/mysql/bin/mysqldump/usr/bin/mysqldump
Ln-s/usr/local/mysql/bin/myisamchk/usr/bin/myisamchk
MySQL installation completed, you can use/etc/init.d/mysql start to test it.

php-fpm

All right, here's the big play. This part of me is basically a line that executes according to Wangyan's script, not much of what I try to do. And as before, the specific need to install which packages I also forget ...

1. Installation Preparation


Apt-get-y Install ZLIBC zlib1g Zlib1g-dev
Apt-get-y Install Libfreetype6 Libfreetype6-dev
Apt-get-y Install LIBXML2 Libxml2-dev
Apt-get-y Install LIBMHASH2 Libmhash-dev
Apt-get-y Install Curl Libcurl3 Libcurl4-openssl-dev
Apt-get-y Install LIBXMLRPC-CORE-C3 Libxmlrpc-core-c3-dev

2. Compile dependent

Need to compile the dependence mainly includes libpng, Libjpeg, Libiconv (these three GD to use), Libmcrypt, Mhash, MCrypt. The compilation process is very consistent:


Wget Source Package Address
Tar zxf Source Pack
CD Source Directory
./configure--prefix=/usr/local
Make && Make install
Where the address is:
Http://src-mirror.googlecode.com/files/libpng-1.5.13.tar.gz http://src-mirror.googlecode.com/files/ jpegsrc.v8d.tar.gz http://src-mirror.googlecode.com/files/libiconv-1.14.tar.gz http://src-mirror.googlecode.com/ files/libmcrypt-2.5.8.tar.gz http://src-mirror.googlecode.com/files/mhash-0.9.9.9.tar.gz http:// Src-mirror.googlecode.com/files/mcrypt-2.6.8.tar.gz

Mhash a ldconfig must be executed once the compilation is complete.

3. Compiling PHP

Because the selected version is 5.4.9, so php-fpm support is self-contained, do not need to be like the version before 5.3 to download Php-fpm-diff manual patch source code.

The installation process is as follows:


wget http://www.php.net/distributions/php-5.4.9.tar.gz
Tar zxf php-5.4.9
CD php-5.4.9
./configure--prefix=/usr/local/php--with-curl--with-curlwrappers--with-freetype-dir--with-gettext- With-iconv-dir--with-jpeg-dir--with-libxml-dir--with-mcrypt--with-mhash--with-mysql=/usr/local/mysql-- With-mysqli=/usr/local/mysql/bin/mysql_config--with-openssl--with-pear--with-png-dir--with-xmlrpc--with-zlib-- Enable-bcmath--enable-calendar--enable-exif--enable-fpm--enable-ftp--enable-gd-native-ttf-- Enable-inline-optimization--enable-mbregex--enable-mbstring--enable-pcntl--enable-shmop--enable-soap-- Enable-sockets--enable-sysvsem--enable-sysvshm--enable-xml--enable-zip
Make zend_extra_libs= '-liconv '
Make install

The long configure command has wood. Wangyan original Script The parameters are divided into one line with the continuation line, a screen shows only this command ...
The specific parameter meaning I do not speak, basically can see from the name. The more critical-enable-fpm don't leak.

4. Install part of PHP extension

Includes PDO MySQL and memcache extensions.

PHP extensions are installed in a very uniform manner, all in the specified directory to perform the phpize, and then the general compile installation can be.
The following command starts before the working directory is in the PHP source directory.


CD ext/pdo_mysql/
/usr/local/php/bin/phpize
./configure--with-php-config=/usr/local/php/bin/php-config--with-pdo-mysql=/usr/local/mysql
Make && Make install

Cd.. /.. /..
wget http://src-mirror.googlecode.com/files/memcache-2.2.6.tgz
Tar zxf memcache-2.2.6.tgz
CD memcache*
/usr/local/php/bin/phpize
./configure--with-php-config=/usr/local/php/bin/php-config--with-zlib-dir--enable-memcache
Make && Make install

5.PHP Configuration

Continue to copy the Wangyan script. Configuration consists of two aspects, one is php.ini configuration, the other is PHP-FPM configuration


For i in ' ls/usr/local/php/bin '; Do ln-s/usr/local/php/bin/$i/usr/bin/$i; Done #为php的执行文件做软连接

CP Php-*/php.ini-production/usr/local/php/lib/php.ini #此处的php-* refers to the PHP source directory, adjusted according to the situation
Sed-i ' s#; Extension_dir = "./" #extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/" \nextension = " memcache.so "\nextension =" pdo_mysql.so "\n#g '/usr/local/php/lib/php.ini #修改扩展的目录, enabling Memcache and Pdo_mysql

Sed-i ' S/short_open_tag = Off/short_open_tag = On/g '/usr/local/php/lib/php.ini
Sed-i ' s/disable_functions =/disable_functions = system,passthru,exec,shell_exec,popen,symlink,dl/g '/usr/local/php /lib/php.ini
Sed-i ' s/max_execution_time = 30/max_execution_time = 300/g '/usr/local/php/lib/php.ini
Sed-i ' s/post_max_size = 8m/post_max_size = 80m/g '/usr/local/php/lib/php.ini
Sed-i ' S/MAGIC_QUOTES_GPC = OFF/MAGIC_QUOTES_GPC = On/g '/usr/local/php/lib/php.ini
Sed-i ' s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g '/usr/local/php/lib/php.ini
Sed-i ' s/upload_max_filesize = 2m/upload_max_filesize = 20m/g '/usr/local/php/lib/php.ini
#sed-i ' s#;d ate.timezone = #date. TimeZone = Asia/shanghai#g '/usr/local/php/lib/php.ini #这句被我去掉是因为我并不想使用上海时区
Sed-i ' S#;sendmail_path = #sendmail_path =/usr/sbin/sendmail-t-i#g '/usr/local/php/lib/php.ini
The next step is to configure the PHP-FPM. PHP-FPM's profile I use the Wangyan version, and his php-fpm.conf content is here, copied to the/usr/local/php/etc/directory. The configuration of the listen parameter, which he uses to provide the FASTCGI service in the form of a 9000-port listener, I modified it to/tmp/php-fpm.sock UNIX socket form to be consistent with my nginx configuration. In addition, his default running users and groups is also www and www, if there are different needs to be modified according to the environment.
Then configure the PHP-FPM startup script (source code)

CP php-5.4.*/sapi/fpm/init.d.php-fpm/etc/init.d/php-fpm #此处的php-* refers to the PHP source directory, which needs to be adjusted according to the situation
chmod 755/etc/init.d/php-fpm
Update-rc.d-f PHP-FPM Defaults
/ETC/INIT.D/PHP-FPM start
At this point the installation is basically complete, you can write a php file under Wwwroot to fill in phpinfo () to test whether the work is normal.

6. Installation of Zend Guard Loader

Although I write this, but in fact PHP5.4 is not Zend Guard Loader (before Zend Optimizer), officially provided only until 5.3. Wangyan's script is copied and configured, but it can't actually be loaded. After consulting some information, we find that the new version of the ZGL will wait a long time to Zend.

Because of its dependence on PHP is not very small, so other XCache, APC and other accelerators and I did not install, this is the end of the

So far, a complete NGINX+MYSQL+PHP-FPM environment has been compiled and installed. I hung up on some of the monitoring scripts to test, the functions are very normal.

Wangyan's Lanmp script is very, very beautiful, much more readable than the Lnmp script of the army. Suggested that interested children's shoes all go to have a look.

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.