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

Linux self-study Notes-- LNMP

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Traditionally, web services based on process or thread model handle concurrent requests per process or thread, which is bound to cause blocking during network and Ibank O operations, and another inevitable result is low utilization of memory or CPU. Generating a new process / thread requires preparing its runtime environment in advance, which includes allocating heap memory and stack memory, and creating a new execution context for it. These operations take up CPU, and too many processes / threads can lead to thread jitter or frequent context switching, resulting in further degradation of system performance.

In the initial stage of design, the main focus of nginx is its high performance and high-density utilization of physical computing resources, so it adopts different architectural models. Inspired by the advanced processing mechanism based on "event" in the design of a variety of operating systems, nginx adopts modular, event-driven, asynchronous, single-threaded and non-blocking architecture, as well as a large number of multiplexing and event notification mechanisms. In nginx, connection requests are processed by a few single-thread process worker with an efficient loopback mechanism, while each worker can process thousands of concurrent connection requests in parallel.

If the load is dominated by cpu-intensive applications, such as ssl or compressed applications, the number of worker should be the same as the number of cpu; if the load is mainly IO-intensive, such as responding to a large amount of content to the client, the number of worker should be 1.5 or 2 times the number of cpu.

Nginx runs multiple processes as needed: a main process (master) and several worker processes (worker). When caching is configured, there are cache loader processes (cache loader) and cache manager processes (cache manager). All processes contain only one thread, and inter-process communication is mainly achieved through the mechanism of "shared memory". The main process runs as root, and erworker, cache loader, and cache manager should all run as unprivileged users.

The main work of the main process is as follows:

1. Read and verify configuration information

two。 Create, bind, and close sockets

3. Number of worker processes started, terminated, and maintained

4. Reconfigure work characteristics without suspending the service

5. Control non-interrupted program upgrades, enable new binaries and roll back to the old version if needed

6. Reopen the log file to achieve log scrolling

7. Compile embedded perl script

The main tasks accomplished by the Worker process are:

1. Receive, incoming, and process connections from the client

two。 Provide reverse proxy and filtering functions

3. Any other task that Nginx can accomplish

The main tasks accomplished by the Cache loader process are:

1. Check the cache objects in the cache store

two。 Using cache metadata to build an in-memory database

The main tasks of the Cache manager process:

1. Cache invalidation and expiration check

The configuration of Nginx has several different contexts: main, http, server, upstream, and location (and mail that implements the reverse proxy of the mail service). The format and definition of the configuration syntax follow the so-called c style, so it supports nesting, has the advantages of clear logic and easy to create, read, and maintain.

The Nginx code consists of a core and a series of modules, which are mainly used to provide the basic functions of web server, as well as the functions of web and mail reverse proxies: they are also used to enable network protocols, create unwanted runtime environments, and ensure smooth interaction between different modules. However, most of the functions related to the protocol and the specific functions of an application are implemented by the module of nginx. These functional modules can be divided into event module, phase processor, output filter, variable processor, protocol, upstream and load balancing, which constitute the http function of nginx. The event module is mainly used to provide OS independent (different operating systems have different time mechanisms) event notification mechanisms such as kqueue or epoll. The protocol module is responsible for realizing that nginx establishes a session with the corresponding client through http, tls/ssl, smtp, pop3 and imap.

Within nginx, inter-process communication is achieved through the module's pipeline or chain: in other words, each function or operation is implemented by a module. For example, compressing, communicating with a upstream server over fastcgi or uwcgi protocols, and establishing a session with mencached, etc.

The following manual compilation installs and deploys the LNMP environment:

First, install nginx

1. Resolve dependencies

Compiling and installing nginx requires the implementation of installing the development package groups "Development Tools" and "Server Platform Development". At the same time, you also need to install the pcre-devel,zlib-devel,openssl-devel dependency package, but generally you only need to install the pcre-devel package, and the other two packages are already installed

Install / var/tmp/nginx this directory, otherwise there will be an error in starting nginx

two。 Installation

1) add users and groups to run nginx processes

# groupadd-r nginx

# useradd-r-g nginx nginx

2) decompress the source code package and enter the directory

3) use the command. / configure-- help can view installation options

4) compile and install

#. / configure\

-- prefix=/usr/local/nginx\

-- sbin-path=/usr/local/nginx/sbin/nginx\

-- conf-path=/etc/nginx/nginx.conf\

-- error-log-path=/var/log/nginx/error.log\

-- http-log-path=/var/log/nginx/access.log\

-- pid-path=/var/run/nginx/nginx.pid\

-- lock-path=/var/lock/nginx.lock\

-- user=nginx\

-- group=nginx\

-- with-http_ssl_module\

-- with-http_flv_module\

-- with-http_stub_status_module\

-- with-http_gzip_static_module\

-- http-client-body-temp-path=/var/tmp/nginx/client/\

-- http-proxy-temp-path=/var/tmp/nginx/proxy/\

-- http-fastcgi-temp-path=/var/tmp/nginx/fcgi/\

-- http-uwsgi-temp-path=/var/tmp/nginx/uwsgi\

-- http-scgi-temp-path=/var/tmp/nginx/scgi\

-- with-pcre

# make & & make install

Note: if you want to use nginx's perl module, you can add the-with-http_perl_module option at compile time to implement it

5) provide Sysv init script

Create a new file / etc/rc.d/init.d/nginx with the following contents:

#! / bin/sh

#

# nginx-this script starts and stops the nginx daemon

#

# chkconfig:-85 15

# description: Nginx is an HTTP (S) server, HTTP (S) reverse\

# proxy and IMAP/POP3 proxy server

# processname: nginx

# config: / etc/nginx/nginx.conf

# config: / etc/sysconfig/nginx

# pidfile: / var/run/nginx/nginx.pid

# Source function library.

. / etc/rc.d/init.d/functions

# Source networking configuration.

. / etc/sysconfig/network

# Check that networking is up.

["$NETWORKING" = "no"] & & exit 0

Nginx= "/ usr/local/nginx/sbin/nginx"

Prog=$ (basename $nginx)

NGINX_CONF_FILE= "/ etc/nginx/nginx.conf"

[- f / etc/sysconfig/nginx] & &. / etc/sysconfig/nginx

Lockfile=/var/lock/nginx.lock

Make_dirs () {

# make required directories

User= `nginx-V 2 > & 1 | grep "configure arguments:" | sed's / [^ *] *-- user=\ ([^] *\). * /\ 1Uniple g'- `

Options= `$ nginx-V 2 > & 1 | grep 'configure arguments:' `

For opt in $options; do

If [`echo $opt | grep'. *-temp-path' `]; then

Value= `echo $opt | cut-d "="-f 2`

If [!-d "$value"]; then

# echo "creating" $value

Mkdir-p $value & & chown-R $user $value

Fi

Fi

Done

}

Start () {

[- x $nginx] | | exit 5

[- f $NGINX_CONF_FILE] | | exit 6

Make_dirs

Echo-n $"Starting $prog:"

Daemon $nginx-c $NGINX_CONF_FILE

Retval=$?

Echo

[$retval-eq 0] & & touch $lockfile

Return $retval

}

Stop () {

Echo-n $"Stopping $prog:"

Killproc $prog-QUIT

Retval=$?

Echo

[$retval-eq 0] & & rm-f $lockfile

Return $retval

}

Restart () {

Configtest | | return $?

Stop

Sleep 1

Start

}

Reload () {

Configtest | | return $?

Echo-n $"Reloading $prog:"

Killproc $nginx-HUP

RETVAL=$?

Echo

}

Force_reload () {

Restart

}

Configtest () {

$nginx-t-c $NGINX_CONF_FILE

}

Rh_status () {

Status $prog

}

Rh_status_q () {

Rh_status > / dev/null 2 > & 1

}

Case "$1" in

Start)

Rh_status_q & & exit 0

, 1

Stop)

Rh_status_q | | exit 0

, 1

Restart | configtest)

, 1

Reload)

Rh_status_q | | exit 7

, 1

Force-reload)

Force_reload

Status)

Rh_status

Condrestart | try-restart)

Rh_status_q | | exit 0

*)

Echo $"Usage: $0 {start | stop | status | restart | condrestart | try-restart | reload | force-reload | configtest}"

Exit 2

Esac

6) add execution permissions for this script

7) add the service management list and enable it to start automatically

8) start the service and test

Second, install mariadb-5.5.46 (in binary mode)

1. Prepare the data storage directory

two。 Create users and groups to run the process

3. Compilation and installation

1) decompress the source code package to the specified directory / usr/local, create a soft connection mysql and enter the mysql directory

2) modify the files in the directory to belong to the master group

3) run the script to generate the Metabase; indicate the location of the user and the data path

4. Provide the main configuration file for mysql

1) copy files

2) Edit the configuration file / etc/my.cnf to add or modify the content

5. Provide Sysv init service scripts for mysql, which include adding execution permissions and adding a list of services and testing the use of

In order to install mysql in accordance with the system usage specifications and export its development components to the system for use, you also need to perform the following steps

6. Output the man manual of mysql to the man command lookup path

Edit / etc/man.config, add the following line

7. Output mysql header file to system header file path / usr/include; can be achieved by simply creating a link

8. Output the library file of mysql to the system to find the path

9. Modify the PATH environment variable so that the system can use mysql-related commands directly.

Test:

At this point, all mariadb installations are complete.

Install php-5.4.26 (in fpm mode)

1. Resolve dependencies

First configure the yum source (system installation and epel source) and execute the following command

# yum-y groupinstall "Desktop Platform Development"

# yum-y install bzip2-devel libmcrypt-devel libxml2-devel

two。 Compile and install php-5.4.26

1) decompress the compressed source file and enter the decompressed directory

2) compile and install

# / configure-- prefix=/usr/local/php-fpm-- with-mysql=/usr/local/mysql-- with-openssl-- enable-fpm-- enable-sockets-- enable-sysvshm-- with-mysqli=/usr/local/mysql/bin/mysql_config-- enable-mbstring-- with-freetype-dir-- with-jpeg-dir-- with-png-dir-- with-zlib-dir-- with-libxml-dir=/usr-- enable-xml-- with-mhash-- with- Mcrypt-with-config-file-path=/etc-with-config-file-scan-dir=/etc/php.d-with-bz2-with-curl

# make & & make install

3. Provide configuration files for php

4. Configure php-fpm

1) provide Sysv init service scripts for php-fpm and add them to the list of services

2) provide configuration files for php-fpm

3) Edit this profile

Configure the values of the relevant options for fpm and enable the pid file

Enable the pid file and remove the semicolon to enable:

4) start the service and test

Fourth, integrate nginx and php-fpm

1. Edit / etc/nginx/nginx.conf and enable the following options

Note: some need to replace / scripts with root absolute path; here / scripts can be replaced with / usr/local/nginx/html

two。 Add a home page in php format to the supported main page format

3. Create a new index.php test page to test whether php is working properly

4. Test

Install xcache to accelerate php

For specific steps, please see my blog, compile and install lamp.

VI. Supplementary explanation

If you want to use php in SSL, you need to add this item to the location of php:

Fastcgi_param HTTPS on

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

Database

Wechat

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

12
Report