Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of LNMP Server Environment configuration

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "LNMP server environment configuration case analysis". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. brief introduction

Nginx is a very lightweight http server written by Russians, nginx, pronounced "engine x", is a high-performance http and reverse proxy server, and is also an imap/pop3/smtp proxy server. Nginx was developed by Russian igor sysoev for the second most visited rambler.ru site in Russia, and it has been running on this site for more than three years. Igor sysoev uses a bsd-based license when building a project.

In the case of high concurrent connections, nginx is a good alternative to apache servers. Nginx can also be used as a 7-tier load balancing server. Nginx 0.8.46 + php 5.2.14 (fastcgi) can withstand more than 30, 000 concurrent connections, which is 10 times that of apache in the same environment.

Nginx surpasses apache's high performance and stability, making more and more domestic websites using nginx as web server, including Sina blog, Sina podcast, NetEase News, QQ.com, Sohu blog and other portal channels, six rooms, 56.com and other video sharing sites, discuz! Official forums, Shuimu Community and other well-known forums, Shanda online, Jinshan Xiaoyao Network and other online game sites, Douban, Renren, yupoo photo album, Kingsoft ICIBA, Xunlei online and other emerging web 2.0 websites.

Why is the performance of nginx much better than apache? This is due to the fact that nginx uses the latest epoll (linux 2.6 kernel) and kqueue (freebsd) network iUnix model, while apache uses the traditional select model. At present, squid and memcached, which can withstand high concurrent access under linux, adopt the epoll network iPlanco model.

To deal with the read and write of a large number of connections, the select network iUnio model adopted by apache is very inefficient. Here is a metaphor to analyze the difference between the select model adopted by apache and the epoll model adopted by nginx:

Suppose you are studying in a university and there are many rooms in the dormitory building where you live. Your friends will come to see you. The select version of the hostel aunt will take your friends from room to room until they find you. The epoll version of the hostel aunt will first write down the room number of each classmate. When your friend comes, just tell your friend which room you are staying in. You don't have to take your friend all over the building to find someone. If 10000 people come to find their own classmates living in this building, it is self-evident who is more efficient than the select version or the epoll version of the hostel aunt. By the same token, polling iUnip o is one of the most time-consuming operations in highly concurrent servers, and it is also clear which performance of select or epoll is higher.

II. System environment

System platform: rhel 5.4 (system requirements: linux 2.6 + kernel)

Nginx version: nginx/1.0.15

Mysql version: 5.1.35-log source distribution

Php version: php-5.2.10

Third, installation preparation

1. Obtain relevant open source programs and install them

Other linux distributions such as redhat can find the rpm package of these libraries on the installation CD. Redhat can be installed directly using centos's rpm package.

You can use rpm to install the following packages, such as related packages, along with the installation.

Gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers

If the above packages are installed, there is no need to install them.

2. Rpm package search website

3. Nginx package preparation

The packages required by nginx can be obtained from the following location, or you can remember the name to search in google.

Nginx-0.7.61.tar.gz

Php-5.2.10.tar.gz

Php-5.2.10-fpm-0.5.11.diff.gz

Mysql-5.1.35.tar.gz

Libiconv-1.13.tar.gz

Libmcrypt-2.5.8.tar.gz

Mcrypt-2.6.8.tar.gz

Memcache-2.2.5.tgz

Mhash-0.9.9.9.tar.gz

Pcre-7.9.tar.gz

Eaccelerator-0.9.5.3.tar.bz2

Pdo_mysql-1.0.2.tgz

Imagemagick.tar.gz

Imagick-2.2.2.tgz

Fourth, install php 5.2.10 (fastcgi mode)

Compile and install the support libraries required for php 5.2.10:

1. Install libiconv

The text is converted between codes, and it is used to deal with the conversion between various codes in Chinese.

# tar zxvf libiconv-1.13.tar.gz

# cd libiconv-1.13/

#. / configure-prefix=/usr/local

# make

# make install

Cd.. /

two。 Install libmcrypt libraries that implement encryption functions.

# tar zxvf libmcrypt-2.5.8.tar.gz

# cd libmcrypt-2.5.8/

#. / configure

# make

# make install

# / sbin/ldconfig

# Note: don't return it here.

# cd libltdl/

#. / configure-enable-ltdl-install

# make

# make install

# cd http://www.cnblogs.com/

3. Install mhash (hash function library)

# tar zxvf mhash-0.9.9.9.tar.gz

# cd mhash-0.9.9.9/

#. / configure

# make

# make install

# cd.. /

Ln-s / usr/local/lib/libmcrypt.la / usr/lib/libmcrypt.la

Ln-s / usr/local/lib/libmcrypt.so / usr/lib/libmcrypt.so

Ln-s / usr/local/lib/libmcrypt.so.4 / usr/lib/libmcrypt.so.4

Ln-s / usr/local/lib/libmcrypt.so.4.4.8 / usr/lib/libmcrypt.so.4.4.8

Ln-s / usr/local/lib/libmhash.a / usr/lib/libmhash.a

Ln-s / usr/local/lib/libmhash.la / usr/lib/libmhash.la

Ln-s / usr/local/lib/libmhash.so / usr/lib/libmhash.so

Ln-s / usr/local/lib/libmhash.so.2 / usr/lib/libmhash.so.2

Ln-s / usr/local/lib/libmhash.so.2.0.1 / usr/lib/libmhash.so.2.0.1

4. Install mcrypt

# tar zxvf mcrypt-2.6.8.tar.gz

# cd mcrypt-2.6.8/

# / sbin/ldconfig

#. / configure

# make

# make install

# cd.. /

Note: the installation package for dg library can be installed with rpm package to reduce the time, because php has integrated gd library, but the prerequisite support package should be installed beforehand, such as zlib,png,jpeg,freetype and so on.

Fifth, compile and install mysql

Establish mysql group, establish mysql user and join mysql group

# groupadd mysql

# useradd mysql-g mysql

[root@linux lnmp] # tar zxvf mysql-5.1.35.tar.gz

[root@linux lnmp] # cd mysql-5.1.35

#. / configure-prefix=/usr/local/mysql

-- without-debug

-- with-extra-charsets=gbk

-- with-extra-charsets=all

-- enable-assembler

-- with-pthread

-- enable-thread-safe-client

-- with-mysqld-ldflags=-all-static / * compiling mysqld*/ without shared libraries

-- with-client-ldflags=-all-static

-- with-big-tables

-- with-readline / * install ncurses or tar package in rpm mode * /

-- with-ssl / * install openssl*/ in rpm mode

-- with-embedded-server

-- enable-local-infile

-- with-plugins=innobase

# make & & make install

# / usr/local/mysql/bin/mysql_install_db-user=mysql

# initialize the database as mysql

# cp. / support-files/mysql.server / etc/init.d/mysql

# copy mysql to start the service to the system

# cp. / support-files/my-medium.cnf / etc/my.cnf

# chmod 755 / etc/init.d/mysql

# cd / usr/local/mysql/ # change to the cd / usr/local/mysql/ directory

# chown-r mysql. # change the owner under the current directory to mysql user

# chown-r mysql var # permissions to modify database directories

# chgrp-r mysql. # change the files of mysql users in the current directory to mysql group

# / usr/local/mysql/bin/mysqld_safe-user=mysql&

# / usr/local/mysql/bin/mysqladmin-u root password 'admin' # set the administrator password

[root@linux html] # / usr/local/mysql/bin/mysql-u root-p # Test password entry

Enter password:

Welcome to the mysql monitor. Commands end with; or\ g.

Your mysql connection id is 5

Server version: 5.1.35-log source distribution

Type 'help;' or'\ h' for help. Type'\ c'to clear the current input statement.

Mysql >\ Q / * exit mysql*/

# chkconfig-- add mysql # add mysqld services to the system

# chkconfig mysql on # Open myslqd service

# service mysql start # start mysql

# / usr/local/mysql/bin/mysqladmin shutdown # shut down the database

# check the opening of the mysql port

# netstat-tunlp

Active internet connections (only servers)

Proto recv-q send-q local address foreign address state pid/p name

Tcp 0 0 0.0.0. 0 3306 0.0.0. 0. 0. 0.

# check whether it is enabled:

# ps-ef | grep mysql

Note: in the configuration process, the overall configuration idea from top to bottom, some of these functions can be used in the actual configuration process, mainly used in the test link.

The optimization part of mysql is not given here, so you need to refer to other materials.

VI. Compile and install php

This place is the most important because by default there is no feeling between nginx and php. After building apache+php,apache+php before, module files are generated after compilation, while nginx+php needs php to generate executable files, so we need to use fastcgi technology to achieve the integration of nginx and php, as long as we enable fastcgi when we install it. This time we installed php not only using fastcgi, but also using php-fpm. Php-fpm is a manager for managing fastcgi, which exists as a plug-in for php. If you want to use php-fpm when installing php, you need to install php-fpm in php in the form of a patch, and php should be consistent with the php-fpm version, which is necessary, remember!

# tar zxvf php-5.2.10.tar.gz

# gzip-cd php-5.2.10-fpm-0.5.11.diff.gz | patch-d php-5.2.10- p1

# add php-5.2.10-fpm-0.5.11.diff.gz to php-5.2.10 as a patch

# cd php-5.2.10/

#. / configure-prefix=/usr/local/php

-- with-config-file-path=/usr/local/php/etc

-- with-mysql=/usr/local/mysql

-- with-mysqli=/usr/local/mysql/bin/mysql_config

-- with-iconv-dir=/usr/local

-- with-freetype-dir

-- with-jpeg-dir

-- with-png-dir

-- with-zlib

-- with-gd

-- enable-gd-native-ttf

-- 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-openssl

-- with-mhash

-- enable-pcntl

-- enable-sockets

-- with-ldap

-- with-ldap-sasl

-- with-xmlrpc

-- enable-zip

-- enable-soap

-- without-pear

Note: nginx+php integration must be enabled at installation time-- enable-fastcgi and-- enable-fpm, these two options are what is described above. After execution, you will be prompted that enable-fastcgi is an unknown option, and we don't care about it.

# Note: the following parameters must be added to make in order to be successful.

# make zend_extra_libs='-liconv'

# make install

# cp php.ini-dist / usr/local/php/etc/php.ini

# cd.. /

Note: the mixed installation of tar package and rpm is used in the installation process, so it is really troublesome to specify the library. If you use rpm installation, you do not need to specify the location of the support package. For tar installation, you need to specify the installation location.

7. Compile and install the php5 extension module

1. Install memcache

# tar zxvf memcache-2.2.5.tgz

# cd memcache-2.2.5/

# / usr/local/php/bin/phpize

#. / configure-with-php-config=/usr/local/php/bin/php-config

# make

# make install

# description: location of the memcache library

Installing shared extensions:

/ usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/

# cd..

two。 Install eaccelerator php acceleration

# tar jxvf eaccelerator-0.9.5.3.tar.bz2

# cd eaccelerator-0.9.5.3/

# / usr/local/php/bin/phpize

#. / configure-enable-eaccelerator=shared-with-php-config=/usr/local/php/bin/php-config

# make

# make install

Installing shared extensions:

/ usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/

# cd.. /

3. Install pdo_mysql (support for database connections)

# tar zxvf pdo_mysql-1.0.2.tgz

# cd pdo_mysql-1.0.2/

# / usr/local/php/bin/phpize

#. / configure-with-php-config=/usr/local/php/bin/php-config

-- with-pdo-mysql=/usr/local/mysql

# make

# make install

Installing shared extensions:

/ usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/

# cd.. /

4. Installing imagemagick is a very powerful image processing function under linux similar to gd.

# tar zxvf imagemagick.tar.gz

# cd imagemagick-6.5.1-2 /

#. / configure

# make

# make install

# cd.. /

5. Install imagick (the channel that connects php to imagemagick)

# tar zxvf imagick-2.2.2.tgz

# cd imagick-2.2.2/

# / usr/local/php/bin/phpize

#. / configure-with-php-config=/usr/local/php/bin/php-config

# make

# make install

Installing shared extensions:

/ usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/

# cd.. /

6. Modify the php.ini file to make php support extended functionality

Vi / usr/local/php/etc/php.ini

Find

Extension_dir = ". /"

Modify to

Extension_dir= "/ usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"

Add the following lines after this line, and then save:

Extension = "memcache.so"

Extension = "pdo_mysql.so"

Extension = "imagick.so"

Then look for output_buffering = off

Modified to output_buffering = on

7. Configure eaccelerator acceleration php:

Mkdir-p / usr/local/eaccelerator_cache

Vi / usr/local/php/etc/php.ini

At the end of the configuration file, paste the following:

[eaccelerator]

Zend_extension= "/ usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"

Eaccelerator.shm_size= "64"

Eaccelerator.cache_dir= "/ usr/local/eaccelerator_cache"

Eaccelerator.enable= "1"

Eaccelerator.optimizer= "1"

Eaccelerator.check_mtime= "1"

Eaccelerator.debug= "0"

Eaccelerator.filter= ""

Eaccelerator.shm_max= "0"

Eaccelerator.shm_ttl= "3600"

Eaccelerator.shm_prune_period= "3600"

Eaccelerator.shm_only= "0"

Eaccelerator.compress= "1"

Eaccelerator.compress_level= "9"

VIII. Php-fpm configuration

1. Create a php-fpm profile

Php-fpm is a fastcgi management patch for php that can smoothly change the php.ini configuration without restarting php-cgi:

Create the php-fpm.conf file in the / usr/local/php/etc/ directory, or you can modify it based on the original.

If you install nginx + php for program debugging

Please send the following

0 is changed to

1 in order to display the php error message, otherwise, nginx will report a blank error page with a status of 500.

Description: create www users and groups. You don't need to create the following.

# / usr/sbin/groupadd www-g 48

# / usr/sbin/useradd-u 48-g www www

Rm-f / usr/local/php/etc/php-fpm.conf

Vi / usr/local/php/etc/php-fpm.conf

# #

# enter or modify the following:

# #

/ usr/local/php/logs/php-fpm.pid

/ usr/local/php/logs/php-fpm.log

Notice

ten

1m

5s

Yes

Default

127.0.0.1:9000

-1

0666

/ usr/sbin/sendmail-t-I

one

Www

Www

Static

one hundred and twenty eight

twenty

five

thirty-five

0s

0s

Logs/slow.log

51200

0

Yes

five hundred

127.0.0.1

$hostname

/ usr/local/bin:/usr/bin:/bin

/ tmp

/ tmp

/ tmp

$ostype

$machtype

two

2.php-fpm startup and management

/ usr/local/php/sbin/php-fpm start

Note: / usr/local/php/sbin/php-fpm has other parameters, including:

Start | stop | quit | restart | reload | logrotate. If the php-cgi is not restarted after the php.ini is modified, and the configuration file is reloaded using reload, the php.ini is reloaded while the fastcgi process of php is running continuously.

IX. Nginx installation

1.nginx installation

Nginx is just a web server, which works with fastcgi implemented by php technology to improve performance.

1. Install the pcre library of the rewrite module support pack:

Pcre is the regular expression used by perl to make the installed software support regular expressions. By default, nginx only handles static web page requests, that is, html. If it comes from a dynamic web page request, such as * .php, then nginx queries the path according to the regular expression and then passes * .php to php to process.

# tar zxvf pcre-7.8.tar.gz

# cd pcre-7.8/

#. / configure

# make & & make install

Cd.. /

two。 Install nginx

Description: create www user groups and www users. If the php-fpm has not been created before, create it here.

# / usr/sbin/groupadd www

# / usr/sbin/useradd-g www www

# tar zxvf nginx-1.0.15.tar.gz

# cd nginx-1.0.15/

#. / configure-user=www-group=www-prefix=/usr/local/nginx-with-http_stub_status_module-with-http_ssl_module

Nginx path prefix: "/ usr/local/nginx"

Nginx binary file: "/ usr/local/nginx/sbin/nginx"

Nginx configuration prefix: "/ usr/local/nginx/conf"

Nginx configuration file: "/ usr/local/nginx/conf/nginx.conf"

Nginx pid file: "/ usr/local/nginx/logs/nginx.pid"

Nginx error log file: "/ usr/local/nginx/logs/error.log"

Nginx http access log file: "/ usr/local/nginx/logs/access.log"

Nginx http client request body temporary files: "client_body_temp"

Nginx http proxy temporary files: "proxy_temp"

Nginx http fastcgi temporary files: "fastcgi_temp"

# make & & make install

# cd.. /

After 3.nginx is installed, there is only one program file, and it does not provide various management programs. It uses parameters and system signal mechanisms to control the nginx process itself.

The parameters of nginx include the following:

-c: use the specified configuration file instead of nginx.conf in the conf directory.

-t: test whether the configuration file is correct. This command is very important when the configuration needs to be reloaded at run time to detect whether the modified configuration file has syntax errors.

-v: displays the nginx version number.

-v: displays the version number of the nginx as well as the compilation environment information and compile-time parameters.

For example, to test whether a configuration file is written correctly, we can use the following command

Sbin/nginx-t-c conf/nginx.conf

10. Nginx configuration

1. Create a nginx.conf file in the / usr/local/nginx/conf/ directory:

Rm-f / usr/local/nginx/conf/nginx.conf

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

= =

Nginx.conf is the configuration file of the nginx web server

= =

User www www; / * users and groups that start the nginx service * /

Worker_processes 1; / * start the working process of the nginx service * /

Error_log logs/nginx_error.log crit; / * error log, and level * /

Pid / usr/local/nginx/nginx.pid; / * nginx service process pid*/

Worker_rlimit_nofile 51200

Events

{

Use epoll; / * working mode * /

Worker_connections 51200; / * maximum number of simultaneous connections per process * /

}

Http

{

Include mime.types

Default_type application/octet-stream

# charset gb2312

Server_names_hash_bucket_size 128

Client_header_buffer_size 32k

Large_client_header_buffers 4 32k

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 128k

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

Server

{

Listen 80; / * listening port * /

Server_name localhost; / * Server name * /

Index index.html index.htm index.php; / * default home page name * /

Root / usr/local/nginx/html; / * the root directory of the website, you can also use the following content * /

# you can also use the relative path, as noted below * /

# location / {

# root html

# index index.html index.htm

#}

# limit_conn crawler 20

# support php,php pages to be processed by fastcgi proxy through fastcgi, which is also an application of reverse proxy, where scripts such as jsp/asp can be used.

# nginx forwards the php request to php through local port 9000. Php listens for data from local port 9000, and nginx and php complete the data request through local port 9000.

Location. *\. (php | php5)? $

{

# fastcgi_pass unix:/tmp/php-cgi.sock

Fastcgi_pass 127.0.0.1 9000; / * fastcgi listening port * /

Fastcgi_index index.php

Include fcgi.conf; / * fastcgi configuration file, modified to the following * /

}

# for a certain type of file, set the expiration time, and the static page is usually set to be longer.

# static files, handled by nginx itself

Location ~. *\. (gif | jpg | jpeg | png | bmp | swf | js | css) $

{

Expires 30d

}

# format of log

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

}

}

Note: the above configuration file is only a basic configuration file, in order to achieve other functions, it needs to be modified on this basis.

two。 Create a fcgi.conf file in the / usr/local/nginx/conf/ directory:

Description: you can paste the following directly.

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

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

11. Nginx startup and management

1. Start nginx

/ usr/local/nginx/sbin/nginx

2. Test the nginx configuration file

After modifying the / usr/local/nginx/conf/nginx.conf configuration file, execute the following command to check that the configuration file is correct:

# / usr/local/nginx/sbin/nginx-t

If the screen displays the following two lines of information, the configuration file is correct:

The configuration file / usr/local/nginx/conf/nginx.conf syntax is ok

The configuration file / usr/local/nginx/conf/nginx.conf was tested successfully

3. Check the nginx main process number

Ps-ef | grep "nginx: master process" | grep-v "grep" | awk-f'{print $2}'

What is displayed on the screen is the nginx main process number, for example:

6302

At this point, execute the following command to make the modified nginx configuration file effective:

Kill-hup 6302

Or, without so much trouble, find the pid file for nginx:

Kill-hup `cat / usr/local/nginx/logs/ nginx.pid`

4. Configure to boot nginx + php automatically.

Vi / etc/rc.local

Add the following:

Ulimit-shn 51200

/ usr/local/php/sbin/php-fpm start

/ usr/local/nginx/sbin/nginx

5. Test nginx

Vi / usr/local/nginx/html/phpinfo.php

6. Test result

Phpinfo ()

This is the end of the content of "LNMP server environment configuration example analysis". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report