LNMP architecture and Application in CentOS

Source: Internet
Author: User
Tags check character epoll gmp mcrypt php software website server nginx load balancing

LNMP architecture and Application in CentOS

LNMP stands for the website server architecture of Nginx + MySQL + PHP in Linux.

Linux is a general term for a type of Unix computer operating system and is currently the most popular free operating system.

Nginx is a high-performance HTTP and reverse proxy server and an IMAP/POP3/SMTP proxy server.

It has the following advantages:

As a web server, nginx is highly efficient in processing static files and index files;

As a proxy server, nginx can achieve cache-free reverse proxy acceleration to speed up website operation;

As a Server Load balancer, nginx supports both Rails and PHP internally, HTTP proxy servers for external services, and simple error tolerance and load balancing using algorithms;

In terms of performance, it occupies a small amount of resources and supports more concurrent connections to achieve higher access efficiency;

Nginx is an excellent proxy server and Server Load balancer server;

Nginx is easy to install and flexible to configure;

In terms of performance, nginx is specially developed for performance optimization and focuses on efficiency. It uses the Poll model to support more concurrent connections. It supports a maximum of 50000 concurrent connections and only occupies low memory resources;

High Availability, nginx supports hot deployment, and the startup speed is extremely fast. You can upgrade the software version or configuration without restarting the service for several months, it can run almost times without interruption.

Mysql is a small relational database management system. PHP is a script language for embedding HTML documents on the server.

These four types of software are free and open-source software combined to become a free, efficient, and scalable website service system.

1. Nginx installation and basic configuration

Nginx.org Official Website

Tarzxf nginx-1.4.2.tar.gznginx-1.4.2/src/corevingw.h # define NGINX_VER "nginx/" NGINX_VERSION // NGINX_VERSION display version, can be removed to prevent malicious hacker attacks

# Nginx-1.4.2/auto/cc # vi gcc # debug # CFLAGS = "$ CFLAGS-g" // Disable debug

#. /Configure -- prefix =/usr/local/lnmp/nginx \ -- with-http_ssl_module \ -- withhttp_stub_status_module // compile, enable the NginxStatus function of https encryption and nginx, // monitors the current status of Nginx.

# Make & make install

# Cd/usr/local/lnmp/nginx/sbin # ln-s/usr/local/lnmp/nginx/sbin/nginx/usr/local/sbin // run the nginx command add environment variable # useradd-M-s/sbin/nologin nginx

# Vim conf/nginx. confuser nginx; worker_processes 2; events {use epoll; // optimized nginxworker_connections 1024 ;} http {// open the log_format main '$ remote_addr-$ remote_user [$ time_local] "$ request" ''$ status $ body_bytes_sent" $ http_referer "'' "$ http_user_agent "" $ http_x_forwarded_for "'; server {location/status {stub_status on; access_log off ;}}}

Nginx supports the following connection handling methods (I/O multiplexing methods), which can be specified through the use command.

  • Select-standard method. If the current platform does not have a more effective method, it is the default method during compilation. You can use configuration parameters -- with-select_module and -- without-select_module to enable or disable this module.

  • Poll-standard method. If the current platform does not have a more effective method, it is the default method during compilation. You can use configuration parameters -- with-poll_module and -- without-poll_module to enable or disable this module.

  • Kqueue-An Efficient Method Used in FreeBSD 4.1 +, OpenBSD 2.9 +, NetBSD 2.0, and MacOS X. Using a dual-processor MacOSX System Using kqueue may cause kernel crashes.

  • Epoll-An Efficient Method Used in Linux kernel version 2.6 and later systems. In some release versions, such as SuSE8.2, there is a patch that allows the kernel of version 2.4 to support epoll.

Nginx-t // check syntax

Nginx // start nginx

Nginx-s reload // reload nginx

Nginx-s stop // disable nginx

Nginx Virtual Host

Vingdeletion. confhttp {server {listen 80; server_name www.tjf.com; location/{root/html/tjf; index index.html index.htm ;}} server {listen 80; server_name www.ty.com; location/{root/html/ty; index index.html index.htm ;}}}

[Root @ node5 conf] # mkdir/html [root @ node5 conf] # mkdir/html/tjf [root @ node5 conf] # mkdir/html/ty [root @ node5 conf] # echo "tjf">/html/tjf/index.html [root @ node5 conf] # echo "ty">/html/ty/index.html [root @ node5 conf] # nginx-s reload

Add resolution in/etc/hosts to enable the web browser to access the domain name.

Nginx load balancing:

# Vi nginx. confupstream ty {// ty is just a name. You can set 192.168.0.25: 8080 weight = 3; // weight to set the number of times the server is accessed each time. server 192.168.0.26: 8080 ;} // Add it to http {}

If you want the server to achieve load balancing, modify the statements in the server {}.

Server {listen 80; server_name www.ty.org; # access_log logs/ty.org. access. log main; location/{proxy_pass http: // ty ;}}

Nginx session Persistence:

Use the nginx sticky module to implement cookie-Based Load Balancing

That is to say, an ip address always accesses a tomcat server at a time (when a user registers an account, the first registration page is completed, and the next step is to switch the page without switching to another tomcat server ), access the next tomcat server with another ip Address

# Nginx-s stop // Add module to nginx Xu re-compile, first stop # tar zxf nginx-sticky-module-1.1.tar.gz # cd nginx-1.4.2 # make clean#./Configure -- prefix =/usr/local/lnmp/nginx -- with-http_ssl_module -- with-http_stub_status_module -- add-module =/root/nginx-sticky-module-1.1 # make & make install

# Vim/usr/local/lnmp/nginx/conf/nginx. conf // Add the sticky module upstream ty {sticky; server 192.168.1.25: 8080; server 192.168.1.26: 8080;} to the configuration file ;}

# Nginx // start nginx, you can find that an ip address always accesses a tomcat server within the effective time

Https encrypted web Access

Cd/etc/pki/tls/certsmakecert. pem // key and ciphertext are both in this file cpcert. pem/usr/local/lnmp/nginx/conf

Vim nginx. conf // open the HTTPSserver segment, that is, remove # ssl_certificate cert. pem; ssl_certificate_key cert. pem;

Nginx-t

Nginx-s reload

Access the browser


Get Certificate


2. Install Mysql source code

Mysql-5.5.12.tar.gz

# Yum install cmake make gcc-c ++

# Tar zxf mysql-5.5.12.tar.gz

# Cd mysql-5.5.

1234567 # cmake-DCMAKE_INSTALL_PREFIX =/usr/local/lnmp/mysql \ # installation directory>-DMYSQL_DATADIR =/usr/local/lnmp/mysql/data \ # database storage directory>-DMYSQL_UNIX_ADDR =/ usr/local/lnmp/mysql/data/mysql. sock \ # Unixsocket file path>-plugin = 1 \ # Install myisam storage engine>-DDEFAULT_CHARSET = utf8 \ # Use utf8 character>-DDEFAULT_COLLATION = utf8_general_ci \ # Check character>-DEXTRA_CHARSETS = all # Install all extended character sets

If this problem occurs, follow the prompts to install yuminstall ncurses-devel,after installation, delete and save the cmakecache.txt file, and re-execute the compilation.

Make & makeinstalluseradd-M-s/sbin/nologinmysqlcd/usr/local/lnmp/mysqlcdscripts/

. /Mysql_install_db -- user = mysql -- basedir =/usr/local/lnmp/mysql/-- datadir =/usr/local/lnmp/mysql/data // available. /mysql_install_db -- help to view parameter information

Chown-R root. mysql/usr/local/lnmp/mysql/* chown-R mysql data/Cpsupport-files/my-medium.cnf/etc/my. cnf // configuration file cpmysql. server/etc/init. d/mysqld // start script service mysqld start

# Vi/root/. bash_profile

PATH = $ PATH: $ HOME/bin:/usr/local/lnmp/mysql/bin

//// Currently, commands such as mysql can be directly used and cannot be executed. You need to configure environment variables and find the mysql command path after source code compilation as/usr/local/lnmp/mysql/bin, add it to the file PATH, separated by semicolons, that is

# Source. bash_profile // make it take effect

3. install php source code

Gd is a library used to generate images.
Libiconv is used to convert one character encoding to another.
Mhash is a hash function library that supports multiple hash algorithms, such as MD5 and SHA1.
Libmcrypt enables php to support more encryption algorithms

Tarzxf libiconv-1.13.1.tar.gz # enhanced system cdlibiconv-1.13.1 for features supporting character encoding conversion./configure -- prefix =/usr/local/lnmp/modules/libiconvmake & makeinstall

Tarjxf libmcrypt-2.5.8.tar.bz2 # mcrypt mhash is php encryption algorithm extended library cdlibmcrypt-2.5.8./configure -- prefix =/usr/local/lnmp/modules/libmcryptmake & makeinstall

Cdlibltdl/./configure -- prefix =/usr/local/lnmp/modules/libmcrypt/libltdl -- enable-ltdl-installmake & makeinstall

Tarjxf mhash-0.9.9.9.tar.bz2cdmhash-0.9.9.9./configure -- prefix =/usr/local/lnmp/modules/mhashmake & makeinstall

Tarzxf mcrypt-2.6.8.tar.gzcdmcrypt-2.6.8./configure -- prefix =/user/local/lnmp/modules/mcrypt -- with-libmcrypt-prefix =/user/local/lnmp/modules/libmcrypt

The following error is reported:

1234 # vi/etc/ld. so. confinclude ld. so. conf. d/*. conf/usr/local/lnmp/modules/libmcrypt/lib # ldconfig

Re-compilation reports an mhash error, as shown below:

Operation required

Vi/etc/ld. so. add conf as follows:/usr/local/lnmp/modules/mhash/libln-s/usr/local/lnmp/modules/mhash/lib/*/usr/local/libln-s /usr/local/lnmp/modules/mhash/include/*/usr/loca/includeldconfig

Then re-compile the code.

# Make & make install

Php software package dependency:

The yum install net-snmp-devel curl-devellibxml2-devel libpng-devel libjpeg-devel freetype-

Devel gmp-devel

Tarjxf php-5.4.12.tar.bz2cdphp-5.4.12. /configure -- prefix =/usr/local/lnmp/php -- with-config-file-path =/usr/local/lnmp/php/etc \ -- with-mysql =/usr /local/lnmp/mysql -- with-openssl -- with-snmp -- with-gd \ -- with-zlib -- with-curl -- with-libxml-dir -- with-png-dir -- with-jpeg-dir \ -- with-freetype-dir -- with-pear -- with-gettext -- with-gmp -- enable-inline-optimization \ -- enable-soap -- enable-ftp \ -- enable-sockets -- enable-mbstring -- with-mysqli =/usr/local/lnmp/mysql/bin/mysql_config \ -- enable-fpm -- with-fpm-user = nginx -- -fpm-group = nginx \ -- with-mcrypt =/usr/local/lnmp/modules/libmcrypt -- with-mhash

# Make ZEND_EXTRA_LIBS = '-liconv' # ZEND accelerates php Execution and manually adds iconv to the additional php library. # In general, php can be automatically added to these libraries, however, iconv must be manually added.

# Vi/etc/ld. so. confusr/local/lnmp/modules/libiconv/lib # ln-s/usr/local/lnmp/modules/libiconv/lib/*/usr/local/lib # ldconfig

# Make install

# Cd/usr/local/lnmp/php/etc

# Cp php-fpm.conf.default php-fpm.conf # vi php-fpm.conf; pid = run/php-fpm.pid // remove comments from the front

# Cd php-5.4.12 # cp php. ini-production/usr/local/lnmp/php/etc/php. ini # configuration files suitable for php production environments # vi/usr/local/php/etc/php. inicgi. fix_pathinfo = 0 # prevent parsing vulnerability due to incorrect Nginx file type date. timezone = Asia/Shanghai # set the time zone

# Cd sapi/fpm/# cp init. d. php-fpm/etc/init. d/fpm // fpm STARTUP script. The default port is 9000 # chmod + x/etc/init. d/fpm #/etc/init. d/fpm start

Now you can configure nginx to access the php test page as follows:

Vi/usr/local/lnmp/nginx/conf/nginx. confserver {location/{root html; index index.phpindex.html index.htm; // remember to add index. php} location ~ \. Php $ {root html; fastcgi_pass 127.0.0.1: 9000; fastcgi_index index. php; # fastcgi_param SCRIPT_FILENAME/scripts $ fastcgi_script_name; include fastcgi. conf ;}}

# Cat html/index. php

<? Php

Phpinfo ()

?>

# Nginx-s reload // access the ip address/index. php In the browser. The php test page is displayed successfully.


Now that the LNMP architecture has been set up, we will build a bbs website on the lnmp architecture:

Software

Http://download.comsenz.com/DiscuzX/

1. upload files in the upload directory to the server

2. Set Directory properties (ignore this step on windows servers)

The following directories require Read and Write Permissions

./Config

./Data sub-directory

3. Execute the installation script/install/

Run the install program in the browser to access http: // your domain name/Forum directory/install/

  1. Follow the prompts on the page to install it until the installation is complete.

After installation

Tomcat dynamic page:

The Tomcat server is a free and open-source Dynamic Web application server, which is a lightweight application server. It is widely used in small and medium systems and concurrent users, is the first choice for JSP program development and debugging.

Tomcat is equivalent to a jsp interpreter and mainly used for dynamic pages.

Jdk must be installed to install tomcat.

Install jdk-6u32-linux-x64.bin

Sh jdk-6u32-linux-x64.bin

Mv jdk1.6.0 _ 32 // usr/local/jdk // move to the specified directory

Vim/etc/profile // set the java global variable exportJAVA_HOME =/usr/local/jdkexportCLASSPATH =.: $ JAVA_HOME/lib: $ JAVA_HOME/jre/lib // specify the java library directory exportPATH = $ PATH: $ JAVA_HOME/bin

Source/etc/profile // make it effective immediately

Tar zxf apache-tomcat-7.0.42.tar.gz-C/usr/local/lnmp/

Cd, usr, local, lnmp, apache, tomcat, 7.0.42, and bin

./Startup. sh // tomcat opens port 8080 by default

Visit 192.168.1.25: 8080 in the browser to view the tomcat webpage.

8080 is required for each access. If you do not want to add it, you can modify nginx. conf.

Location ~ \. Jsp $ {

Proxy_pass http: // 127.0.0.1: 8080;

}

In this way, you do not need to add port 8080 to access 192.168.1.25/index. jsp. Now, jsp dynamic web pages are accessed through nginx

Because nginx does not currently support the image function, the image cannot be displayed normally during access. In this case, add the following field to ngix. conf:

Location ~ \. (Png | gif | css | jsp | js) $ {

Root/usr/local/lnmp/tomcat/webapps/ROOT;

}

You can access the image again.

Test page content Thissystem's time is <% = new java. util. Date () %>

Use nginx to achieve load balancing of tomcat dynamic pages

Vi nginx. conf

Http {

Upstream ty {

Server 192.168.1.25: 8080; // tomcat is installed on host 25 and host 26

Server 192.168.1.26: 8080;

}

}

Server {

Location ~ \. Jsp $ {

Proxy_pass http: // ty;

}

}

Configure jsp Dynamic Webpage files for each server

Nginx-s reload

Now you can access the server Load balancer instance.

-----------------------------------------------------------

Memcached

Is a high-performance distributed memory object cache system. By maintaining a unified and huge hash table in the memory, it can be used to store data in various formats, including image, video, file, and database retrieval results. Simply put, the data is called to the memory and then read from the memory, which greatly improves the reading speed.

Memcache is a php module that enables php to use memory storage and load to memory to increase access speed.

Memcached is provided in the Package of enterprise6.

RPM package installation

Yum install memcached

/Etc/init. d/memcached start


Telnet localhost 11211 // test whether memcached is successfully enabled

Stats view status

Set settings

Add

Get view delete

Session Control for tomcat server Load balancer

(The accessed server goes down and switches directly to another server. The last access record is still available)

Session:

Cross-store memcache

Tomcat-1 (T1) Stores sessions on memcached-2 (T2. Session is saved only when M2 is unavailable.

Stored on the memcached-1 (M1 is T1 failoverNode ). The benefit of using this configuration is that when T1 and M1 crash at the same time

Session will not be lost during the crash to avoid single point of failure.

Official http://code.google.com/p/memcached-session-manager

First, Server Load balancer A and Server Load balancer B has been used for tomcat:

A:

1,

Asm-3.2.jar minlog-1.2.jar

Kryo-1.04.jar msm-kryo-serializer-1.6.5.jar

Kryo-serializers-0.10.jar reflectasm-1.01.jar

Memcached-session-manager-1.6.3.jar spymemcached-2.7.3.jar

Memcached-session-manager-tc7-1.6.3.jar // tc7 corresponds to tomcat version 7

// Put the. jar package to the tomcat/lib directory. Pay attention to the version of the package.

2,

Vim/tomcat/conf/context. xml <ManagerclassName = "de. javakaffee. web. msm. memcachedBackupSessionManager "memcachedNodes =" n1: 192.168.1.25: 11211, n2: 192.168.1.26: 11211 "failoverNodes =" n1 "requestUriIgnorePattern = ". *\. (ico | png | gif | jpg | css | js) $ "transcoderFactoryClass =" de. javakaffee. web. msm. serializer. kryo. kryoTranscoderFactory "/>

3,

Cd/tomcat/webapps/ROOTvitest. jsp // create test page <% @ page contentType = "text/html; charset = GBK" %> <% @ pageimport = "java. util. * "%> 4,

/Etc/init. d/memcachedstart/tomcat/bin/./shutdown. sh/tomcat/bin/./startup. sh // restart tomcat

Tail-f/tomcat/logs/catalina. out // view logs. If finished is initialized, INFO: MemcachedSessionServicefinished initialization ......

B: Steps 1, 3, and 4 are the same. The configuration content of step 2 is slightly changed.

<ManagerclassName = "de. javakaffee. web. msm. memcachedBackupSessionManager "memcachedNodes =" n1: 192.168.1.25: 11211, n2: 192.168.1.26: 11211 "failoverNodes =" n2 "requestUriIgnorePattern = ". *\. (ico | png | gif | jpg | css | js) $ "transcoderFactoryClass =" de. javakaffee. web. msm. serializer. kryo. kryoTranscoderFactory "/>

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.