A, environmental

Server version: CentOS 7.2

To make sure you don’t encounter strange things during your study period, make sure there are four things you can do:

  1. Confirming the System Network
  2. Confirm yum is available
  3. Confirm that iptables is disabled
  4. Confirm that selinux is disabled
# Check the iptables state
systemctl status firewalld.service
# disable firewall (temporary shutdown)
systemctl stop firewalld.service
Check SELinux status
getenforce
# Temporarily close SELinux
setenforce 0
Copy the code

Install some basic system tools, which normally come with the system.

yum -y install gcc gcc-c++ autoconf pcre pcre-devel make automake
yum -y install wget httpd-tools vim
Copy the code

What is Nginx?

Nginx is an open source, high-performance, reliable HTTP middleware, proxy services, other HTTP services:

  1. HTTPD – Apache foundation
  2. Microsoft IIS –
  3. Gws-google (not open to the public)

In recent years, Nginx’s market share is getting higher and higher, once soaring, why? The next thing we know!

Why do we choose Nginx?

1. IO multiplexing epoll

How do you understand that? Take an example! There were three teachers, A, B and C, who all had A problem. They had to help A class of students with their homework. Teacher A took turns to answer the questions from the first row. Teacher A wasted A lot of time, and some students had not finished their homework, so the teacher came, and the efficiency was very slow. Teacher B is A ninja, he found that teacher A’s method is not possible, so he used the shadow of several self to help several students at the same time to answer the question, the final answer is not finished, teacher B exhausted energy collapsed. Teacher C was smart. He told the students that if they raised their hands after finishing their homework, he would direct the questions. He asked the students to take the initiative to speak, which separated the “concurrency”. This teacher C is Nginx.

2. Lightweight

  • Few functional modules – Nginx retains only HTTP required modules, the rest of the way with plug-ins, added after the day
  • Code modular – more suitable for secondary development, such as Alibaba Tengine

3. CPU affinity

Bind CPU core to Nginx worker process, fix each worker process to execute on one CPU, reduce cache miss of switching CPU, and improve performance.

Installation and Directory

I use bird brother LNMP integration package lnmp.org, simple and convenient – recommended!

Nginx PHP mysql will be installed according to the instructions. Please visit LNMP website for more details
The default installation directory is /usr/localWget -c http://soft.vpser.net/lnmp/lnmp1.4.tar.gz && tar ZXF lnmp1.4. Tar. Gz &&cdLnmp1.4 &&. / install. Sh LNMPThe default installation directory
/usr/local
Copy the code

4. Basic configuration

Open the main configuration file if you are using LNMP
vim /usr/local/nginx/conf/nginx.conf

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

user                    # set system user for nginx service
worker_processes        The number of working processes is generally consistent with the number of CPU cores
error_log               #nginx error log
pid                     #nginx pid at startup

events {
    worker_connections    # Maximum number of connections per process
    use                   # kernel model used by nginx
}

Copy the code

We use the HTTP service of Nginx. In the HTTP area of the configuration file nginx.conf, we configure countless servers, each server corresponds to this virtual host or domain name

http { ... .More on the HTTP configuration project later
    
    server {
        listen 80                          # monitor port;
        server_name localhost              # address
        
        location / {                       # Access the home page path
            root /xxx/xxx/index.html       # default directory
            index index.html index.htm     # default file
        }        
        
        error_page  500 504   /50x.html    # Redefine to 50x.html when the above status code appears
        location = /50x.html {             # when accessing 50x.html
            root /xxx/xxx/html             #50x.html location of the page} } server { ... . }}Copy the code

A server can have multiple locations. Let’s configure different access paths for different situations. Let’s look at HTTP configuration details

http {
    sendfile  on                  The efficient file transfer mode must be enabled
    keepalive_timeout   65        Client server request timeout
    log_format  main   XXX        Define log format codename main
    access_log  /usr/local/access.log  main     # Log save address format code main
}
Copy the code

Four, modules,

Nginx has been enabled and linked to the module, too many modules, not this long article, there is a need for their own hundred degree ~

# uppercase V to view all modules, lowercase V to view versions
nginx -V
Check for syntax errors in this configuration file
nginx -tc /usr/local/nginx/conf/nginx.conf
Copy the code