Nginx website service
Experimental thinking
The first step is installation and operation control.
Step 2 profile adjustment
The third step is status statistics and virtual host
The fourth step is LNMP construction
Step 5 LNMP platform deployment (launch) web application (website project)
two。 Experimental environment:
Overview of host OS IP address software description
Ctos6-1 Centos6.5 192.168.200.254 ftp and yum sources provide the underlying environment
Ctos6-2 Centos6.5 192.168.200.202 Nginx, mysql, php Nginx website
3. Key content:
Key content 1: what is nginx:
Lightweight HTTP service software, a web service software specially used to deal with static pages with stability, high efficiency, low consumption and high concurrent connection processing ability.
Key content 2: installation and operation control:
1. Download software-> install dependency packages (support software) to create users and groups-> install
two。 Operation control:
Nginx-t # # check the configuration
Nginx # # launch
Killall-s signal nginx # # signal has HUP overload configuration, QUIT exits, KILL kills
Key content 3: configuration file: / usr/local/nginx/conf/nginx.conf
1. Basic format:
Vi / usr/local/nginx/conf/nginx.conf
:% g / ^ $/ d # # Delete blank lines
:% g _
Configuration item format: key valu
Configuration includes: global configuration, events O event configuration (events {configuration item}), HTTP configuration (http {server {location {}}) http configuration can have multiple server configurations, server configuration can have multiple location configurations.
: wq
two。 Important configuration items:
Domain name of Server_name website
Location / {
Root html; # # the root directory of the website
Index index.html; # # specify the default home page
}
Key content 4: access status statistics and virtual hosts:
1. Access status: nginx built-in HTTP_STUB_STATUS module
Enabling this feature requires two steps: one is to compile and install is to specify-- with-http_stub_status_module, and the other is to modify the configuration file.
two。 Virtual hosts: nginx supports virtual hosts (port, ip, domain name)
1) nginx implements virtual host: one virtual host, one server {different listening address, domain name, port, log location, web page root directory}
2) commonly used virtual hosts are domain name-based virtual hosts. When configuring multiple virtual machines, you only need to add multiple domain name resolution and set multiple server {} configurations.
Key content 5: LNMP architecture and application deployment: nginx handles static pages, php-fpm deals with dynamic pages
1. Install MySQL database: compile, install, optimize, adjust, initialize the database to start the mysql service
two。 Install php: compile and install (note-- php-fpm enables php support) adjust to add zendguardloader after installation
3. Configure php-fpm (nginx supports php environment): first, create a php-fpm listening TCP/9000 port; second, add nginx to forward php requests to port 9000.
1) create a php-fpm configuration file:
Vi / usr/local/php5/etc/php-fpm.conf # # fpm configuration file
[global] # # Global configuration
Pid = run/php-fpm.pid
[www] # # website configuration
Listen = 127.0.0.1 9000 # # listening ip: Port
User = nginx # # user must be a user of the nginx process
Group = nginx
Pm = dynamic
Pm.max_children = 50 # # number of processes started at startup
Pm.start_servers= 20 # # minimum number of idle processes
Pm.min_spare_servers = 5 # # minimum number of idle processes
Pm.max_spare_servers = 35 # # maximum number of processes
: wq
2) configure nginx to support PHP parsing: choose one of the two ways
Method 1: proxy mode (forwarding php requests to other hosts that can resolve php) (used in the cluster)
Server {
Location ~ .php ${proxy_pass http://php parsing host ip: Port}
}
Method 2: call the php-fpm process (for stand-alone use)
Server {
Location ~ .php ${# # matches the request of php in URL
Root html; # # Web page root directory
Fastcgi_pass 127.0.0.1 9000; # # specify fpm snooping address and port
Fastcgi_index index.php; # # specify the php home page file
Include fastcgi.conf; # # introducing fastcgi.conf configuration
}
}
4. Publish php application (code online): download the program code (web page project) and extract and copy it to the root directory of the web page to create a database and authorize web page initialization and access.
three。 Project experiment steps (operation screenshot or operation command)
1. Installation and operation control:
1) install nginx:192.168.200.202
[root@localhost ~] # lftp 192.168.200.254 # download
Lftp 192.168.200.254VR ~ > cd tools/
Lftp 192.168.200.254:/tools > get nginx-1.6.0.tar.gz
802956 bytes transferred
Lftp 192.168.200.254:/tools > bye
[root@localhost ~] # yum-y install pcre-devel zlib-devel & > / dev/null # # install dependency package
[root@localhost ~] # useradd-M-s / sbin/nologin nginx # # create a user
[root@localhost ~] # tar zxf nginx-1.6.0.tar.gz-C / usr/src/ & > / dev/null # # decompress
[root@localhost ~] # cd / usr/src/nginx-1.6.0/
[root@localhost nginx-1.6.0] # / configure-- prefix=/usr/local/nginx-- user=nginx-- group=nginx-- with-http_stub_status_module & & make & & make install # # installation
[root@localhost nginx-1.6.0] # ln-s / usr/local/nginx/sbin/nginx / usr/local/sbin/ # create soft links and optimize command search paths
[root@localhost nginx-1.6.0] # ls-l / usr/local/sbin/nginx
Lrwxrwxrwx 1 root root 27 August 31 17:02 / usr/local/sbin/nginx-> / usr/local/nginx/sbin/nginx
[root@localhost nginx-1.6.0] # cd
2) Operation Control: 192.168.200.202
[root@localhost ~] # nginx-t
Nginx: the configuration file / usr/local/nginx/conf/nginx.conf syntax is ok
Nginx: configuration file / usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~] #
[root@localhost ~] # nginx # # start the service
[root@localhost ~] # netstat-utpln | grep 80 # # Verification
Tcp 0 0 0.0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0 of the LISTEN 3250/nginx.
[root@localhost ~] #
3) use nginx service scripts:
[root@localhost] # killall-s H
UP nginx # # reload the configuration file, which is equivalent to reload
[root@localhost ~] # killall-s QUIT nginx # # exit and end normally
[root@localhost] # killall-s KILL nginx # # forced killing
[root@localhost ~] # vi / etc/init.d/nginx
#! / bin/bash
Chkconfig: 35 99 20description: Nginx Server Control Script
NP= "/ usr/local/nginx/sbin/nginx"
NPF= "/ usr/local/nginx/logs/nginx.pid"
Case "$1" in # # $1 represents the first location variable, and $0 represents the script itself
Start)
$NP
If [$?-eq 0]
Then
Echo "nginx is startling!"
Fi
Stop)
Kill-s QUIT $(cat $NPF)
If [$?-eq 0]
Then
Echo "nginx is stoppings!"
Fi
Restart)
$0 stop
$0 start
Reload)
Kill-s HUP $(cat $NPF)
If [$?-eq 0]
Then
Echo "nginx config file is reload!"
Fi
)
Echo "Usage: $0 {start | stop | restart | reload}"
Exit 1
Esac
Exit 0
: wq
[root@localhost ~] # chkconfig-- add nginx # # add system services
[root@localhost] # chmod + x / etc/init.d/nginx # # Authorization
[root@localhost ~] # / etc/init.d/nginx restart # # restart verification
Nginx is stopping!!
Nginx is starting!!
[root@localhost ~] # netstat-utpln | grep nginx # # View nginx listening port
Tcp 0 0 0.0.0.0:80 0.0.0.0: LISTEN 3277/nginx
[root@localhost ~] #
4) access verification:
Real machine access verification: 192.168.200.202
Linux client access authentication: 192.168.200.254
[root@localhost ~] # yum-y install elinks
[root@localhost ~] # elinks-- dump http://192.168.200.202 # # visit
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
Working. Further configuration is required.
For online documentation and support please refer to [1] nginx.org.
Commercial support is available at [2] nginx.com.
Thank you for using nginx.
References
Visible links
Http://nginx.org/http://nginx.com/
[root@localhost ~] #
Profile adjustment (profile determines the security of the service, function, stability and other profile adjustments are very important)
Basic optimization:
[root@localhost ~] # vi / usr/local/nginx/conf/nginx.conf
:% g / ^ $/ d
:% gUniverse Universe d
: set nu
Adjust the number of concurrent links:
Concurrent connections = worker_process (number of worker processes) X worker_connections=2x4096=8192
1 worker_processes 2
2 events {
3 worker_connections 4096
4}
10 charset utf-8; # # supports Chinese character set and utf- 80000 code
16 index index.html index.htm index.php; # # support php home page
: wq
[root@localhost ~] # nginx-t # Test
Nginx: the configuration file / usr/local/nginx/conf/nginx.conf syntax is ok
Nginx: configuration file / usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~] #
[root@localhost ~] # / etc/init.d/nginx restart
Nginx is stopping!!
Nginx is starting!!
[root@localhost ~] #
[root@localhost ~] # ps aux | grep nginx | grep worker | number of wc-l # # verification worker processes
Status statistics and virtual host
1) status Statistics: 192.168.200.202
[root@localhost ~] # nginx-V # # verify whether the installation loads the statistics function
Nginx version: nginx/1.6.0
Built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
Configure arguments:-prefix=/usr/local/nginx-- user=nginx-- group=nginx-- with-http_stub_status_module
[root@localhost ~] # vi / usr/local/nginx/conf/nginx.conf # # modify configuration
18 location / status {
19 stub_status on; # # enable status statistics
20 access_log off; # turn off logging, only http://ip/status 's log is closed
21}
: wq
[root@localhost ~] # / etc/init.d/nginx restart
Nginx is stopping!!
Nginx is starting!!
[root@localhost ~] # elinks-- dump http://192.168.200.202/status # # 192.168.200.254 access authentication
Active connections: 1 (active connection)
Server accepts handled requests (connection information processed) 1 (number of connections) 1 (successful TCP handshake) 1 (number of requests processed)
Reading: 0 Writing: 1 Waiting: 0
[root@localhost ~] #
2) Virtual host configuration:
a. Set up dns parsing: 192.168.200.254
[root@ns ~] # cd / var/named/chroot/var/named
[root@ns named] # vi. /.. / etc/named.conf # # finally add
Zone "linuxren.cn." IN {
Type master
File "linuxren.cn.zone"
}
: wq
[root@ns named] # cp linuxfan.cn.zone linuxren.cn.zone
[root@ns named] # sed-I's Universe Ren linuxren.cn.zone
[root@ns named] # / etc/init.d/named restart
Stop named:. [OK]
Start named: [OK]
[root@localhost ~] #
[root@ns named] # vi / etc/resolv.conf
Nameserver 192.168.200.254
Nameserver 10.0.0.2
: wq
[root@ns named] # nslookup www.linuxfan.cn
Server: 192.168.200.254
Address: 192.168.200.254#53
Name: www.linuxfan.cn
Address: 192.168.200.202
[root@ns named] # nslookup www.linuxren.cn
Server: 192.168.200.254
Address: 192.168.200.254#53
Name: www.linuxren.cn
Address: 192.168.200.202
[root@ns named] #
b. Modify the configuration file and prepare the test directory: 192.168.200.202
[root@localhost ~] # mkdir / var/www/ # # create a test directory
[root@localhost ~] # echo www.linuxfan.cn > / usr/local/nginx/html/index.html # # create the test home page
[root@localhost ~] # echo www.linuxren.cn > / var/www/index.html
[root@localhost ~] # vi / usr/local/nginx/conf/nginx.conf # # add the following, and note the line number
13 server_name www.linuxfan.cn; # # modify the website domain name
27 server {
28 listen 80
29 server_name www.linuxren.cn; # # modify the website domain name
30 location / {
31 root / var/www/; # # specify the root of the web page
32 index index.html index.htm index.php
33}
34 error_page 500 502 503 504 / 50x.html
35 location = / 50x.html {
36 root html
37}
38}
: wq
[root@localhost ~] # nginx-t # # check syntax
Nginx: the configuration file / usr/local/nginx/conf/nginx.conf syntax is ok
Nginx: configuration file / usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~] #
[root@localhost] # / etc/init.d/nginx restart # # restart the service
Nginx is stopping!!
Nginx is starting!!
[root@localhost ~] #
C: access Test: 192.168.200.254
[root@localhost ~] # vi / etc/resolv.conf
; generated by / sbin/dhclient-script
Nameserver 192.168.200.254 # # modify the original nameserver
: wq
[root@localhost ~] # elinks-- dump http://www.linuxfan.cn # # access test
Www.linuxfan.cn
[root@localhost ~] # elinks-- dump http://www.linuxren.cn
Www.linuxren.cn
[root@localhost ~] #
LNMP construction
1) install mysql:192.168.200.202
[root@localhost ~] # lftp 192.168.200.254
Lftp 192.168.200.254VR ~ > cd tools/
Lftp 192.168.200.254:/tools > get lamp_install_publis-app-2015-07-16.tar.xz
95811756 bytes transferred in 3 seconds (26.83M/s)
Lftp 192.168.200.254:/tools > bye
[root@localhost] # tar Jxf lamp_install_publis-app-2015-07-16.tar.xz
[root@localhost ~] # vi bin/mysql_install.sh
: q
[root@localhost ~] # vi bin/mysql_config.sh
: q
[root@localhost] # mysql_install.sh & & mysql_config.sh
. . . A lot of tips are omitted here.
Starting MySQL.. SUCCESS!
Mysql root password is 123123
[root@localhost ~] # source / etc/profile # # execute configuration file
[root@localhost ~] # mysql-uroot-p123123 # # login MySQL verification
Mysql > quit
Bye
[root@localhost ~] #
2) install php:192.168.200.202
[root@localhost ~] # yum-y install gd libxml2-devel libjpeg-devel libpng-devel & > / dev/null # # install dependency package
[root@localhost ~] # ls php-5.3.28.tar.gz
Php-5.3.28.tar.gz
[root@localhost] # tar zxf php-5.3.28.tar.gz-C / usr/src/
[root@localhost ~] #
[root@localhost ~] # cd / usr/src/php-5.3.28/
[root@localhost php-5.3.28] # / configure-- prefix=/usr/local/php5-- with-gd-- with-zlib-- with-mysql=/usr/local/mysql/-- with-config-file-path=/usr/local/php5-- enable-mbstring-- enable-fpm-- with-jpeg-dir=/usr/lib & & make & make install # # installation uses the-- enable-fpm option to enable fastCGI process management to parse php pages
. . . Omit a lot of prompts.
You may want to add: / usr/local/php5/lib/php to your php.ini include_path
[PEAR] Structures_Graph- installed: 1.0.4
[PEAR] XML_Util-installed: 1.2.1
/ usr/src/php-5.3.28/build/shtool install-c ext/phar/phar.phar / usr/local/php5/bin
Ln-s-f / usr/local/php5/bin/phar.phar / usr/local/php5/bin/phar
Installing PDO headers: / usr/local/php5/include/php/ext/pdo/
[root@localhost php-5.3.28] # cp php.ini-development / usr/local/php5/php.ini # # copy configuration file
[root@localhost php-5.3.28] # ln-s / usr/local/php5/bin/ / usr/local/bin/ # # optimized path
[root@localhost php-5.3.28] # ln-s / usr/local/php5/sbin/ / usr/local/sbin/
[root@localhost php-5.3.28] # cd
3) install zendguardloader:192.168.200.202
[root@localhost ~] # tar zxf ZendGuardLoader-php-5.3-linux-glibc23-x86_64.tar.gz
[root@localhost ~] #
[root@localhost ~] # cp / root/ZendGuardLoader-php-5.3-linux-glibc23-x86_64/php-5.3.x/ZendGuardLoader.so / usr/local/php5/lib/php/ # # Note: copy the module file on one line
[root@localhost ~] # vi / usr/local/php5/php.ini # # add at the end
Zend_extension=/usr/local/php5/lib/php/ZendGuardLoader.so # # specify the location of module files
Zend_loader.enable=1 # # enable zend module
: wq
4) configure php environment: enable php-fpm:192.168.200.202
[root@localhost ~] # vi / usr/local/php5/etc/php-fpm.conf
[global]
Pid = run/php-fpm.pid
[www]
User = nginx
Group = nginx
Listen = 127.0.0.1 9000
Pm = dynamic
Pm.max_children = 5
Pm.start_servers = 2
Pm.min_spare_servers = 1
Pm.max_spare_servers = 3
: wq
[root@localhost ~] #
[root@localhost ~] # / usr/local/sbin/php-fpm # # start the service
[root@localhost ~] # netstat-utpln | grep php
Tcp 00 127.0.0.1 9000 0.0.0.0 * LISTEN 118437/php-fpm
[root@localhost ~] #
5) nginx supports php-fpm:
[root@localhost ~] # vi / usr/local/nginx/conf/nginx.conf
22 location. Php$ {
23 root html
24 fastcgi_pass 127.0.0.1:9000
25 fastcgi_index index.php
26 include fastcgi.conf
27}
: wq
6) write a script to start LNMP:
[root@localhost ~] # vi / etc/init.d/lnmp
#! / bin/bashchkconfig: 35 95 30description: This script is for LNMP Management!
NGF=/usr/local/nginx/sbin/nginx
NGP=/usr/local/nginx/logs/nginx.pid
FPMF=/usr/local/php5/sbin/php-fpm
FPMP=/usr/local/php5/var/run/php-fpm.pid
Case $1 in
Start)
$NGF & & echo "nginx is starting!"
$FPMF & & echo "php-fpm is starting!"
Stop)
Kill-QUIT $(cat $NGP) & & echo "nginx is stoped!"
Kill-QUIT $(cat $FPMP) & & echo "php-fpm is stoped!"
Restart)
$0 stop
$0 start
Reload)
Kill-HUP $(cat $NGP)
Kill-HUP $(cat $FPMP)
Status)
Netstat-utpln | grep nginx & > / dev/null
If [$?-eq 0]
Then
Echo "nginx is running!"
Else
Echo "nginx is not running!"
Fi
Netstat-upltn | grep php-fpm & > / dev/null
If [$?-eq 0]
Then
Echo "php-fpm is runing!"
Else
Echo "php-fpm is not running!"
Fi
*)
Echo "Usage $0 {start | stop | status | restart}"
Exit 1
Esac
: wq
[root@localhost ~] # chmod + x / etc/init.d/lnmp
[root@localhost] # chkconfig-- add lnmp
[root@localhost ~] # / etc/init.d/lnmp status
Nginx is running!
Php-fpm is runing!
[root@localhost ~] #
[root@localhost ~] # vi / usr/local/nginx/html/index.php
: wq
[root@localhost ~] #
Access verification: 192.168.200.11 (real machine) Note: set DNS to 192.168.200.254
5.LNMP platform deployment (launch) web application (website project): 192.168.200.202
[root@localhost] # yum-y install unzip # # install the decompression software
[root@localhost ~] # lftp ftp.linuxfan.cn
Lftp ftp.linuxfan.cn:~ > cd tools/
Lftp ftp.linuxfan.cn:/tools > get SKYUC.v3.4.2.SOURCE.zip # # download the website project
8249271 bytes transferred
Lftp ftp.linuxfan.cn:/tools > bye
[root@localhost ~] #
[root@localhost ~] # unzip SKYUC.v3.4.2.SOURCE.zip # # decompress
[root@localhost ~] # cd SKYUC.v3.4.2.SOURCE/
[root@localhost ~] # cp-rf wwwroot / usr/local/nginx/html/skyuc # # copy the project, or use the ln command to link
[root@localhost ~] # cd / usr/local/nginx/html/skyuc # # enter the directory
[root@localhost] # chown-R nginx:nginx admincp/ data/ templates/ upload/ # # Authorization
[root@localhost ~] # mysql-uroot-p123123-s # # Log in to mysql
Create database skyucdb; # # create a database
Grant all on skyucdb.* to runskyuc@'localhost' identified by '123123 authorization; # # authorize local access
Quit
[root@localhost ~] #
Browser access: http://192.168.200.202/skyuc
The operation of the web page is relatively simple for everyone to complete by themselves.
Delete installation files on [root@localhost ~] # cd / usr/local/nginx/html/skyuc # # 192.168.200.202
[root@localhost skyuc] # rm-rf install/
[root@localhost skyuc] #
Results: