Small knowledge, big challenge! This article is participating in the “Essentials for Programmers” creative activity. This article has participated in the “Digitalstar Project” to win a creative gift package and challenge the creative incentive money.

The committee has previously written that Docker is tired of playing, so try using Vagrant and Vagrant with notes

Recently, Nginx is very popular (this thing for many years), the committee began to use 16 years, here is a summary of the preparation of one-click installation method, for everyone, learn not to spend money to buy more powerful server!

Step by step, there is a one-click installation technique at the end of the article (automatically help you compile the source code!)

What is Nginx

It is a lightweight, high-performance Web server/reverse proxy server, as well as an IMAP/POP3/SMTP proxy server.

Anyway, it is a good thing, “supercar in the server” worth the patience to learn! The main performance is strong, do a simple site do not spend too much money to buy higher configuration.

Small white not quite clear effect, can regard it as a very cow server software! We can put our own beautiful pages in this server software.

Here are the key points of installation, and then the committee will modify the page at last. Let’s have a look.

Install – Source compiler installs Nginx

Here is the script for the test (it was posted on Github a few years ago, and it works perfectly!)

Save the code as leixuewei.sh and run it in CentOS7.

systemctl stop firewalld.service

#use alibaba yum repo
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup`date '+%Y%m%d_%H%M%S'` 
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all
yum makecache

#get netstat
yum -y install net-tools
yum -y vim

#get deps to compile python modules
yum -y install gcc automake autoconf libtool make
yum -y install gcc
yum -y install gcc-c++

mkdir /toolings/xuewei
cp /tmp/*.tar.gz /toolings/xuewei
cd /toolings/xuewei

nginx=/toolings/xuewei/nginx/nginx
#for url rewrite
if [ ! -e pcre.tar.gz ] ; thenThe curl - https://jaist.dl.sourceforge.net/project/pcre/pcre/8.34/pcre-8.34.tar.gz - o k pcre. Tar. Gz.fi
tar -zxvf pcre.tar.gz
if [ ! -e $nginx];then cdPcre-8.34 &&./configure && make && make install;fi

cd /toolings/xuewei
if [ ! -e zlib.tar.gz ] ; thenThe curl - https://nchc.dl.sourceforge.net/project/libpng/zlib/1.2.11/zlib-1.2.11.tar.gz - o k zlib. Tar. Gz.fi
tar -zxvf zlib.tar.gz
if [ ! -e $nginx];then cdZlib-1.2.11 / &&./configure && make && make install;fi

cd /toolings/xuewei
if [ ! -e openssl.tar.gz ] ; then curl -k https://www.openssl.org/source/ openssl - t.t ar 1.0.1. Gz - o openssl. Tar. Gz;fi
tar -zxvf openssl.tar.gz
cd/./config shared zlib --prefix=/toolings/xuewei/opensslif [ ! -e $nginx];then make && make install ; fi

cd /toolings/xuewei
if [ ! -e nginx.tar.gz ] ; thenThe curl http://nginx.org/download/nginx-1.4.2.tar.gz - o nginx. Tar. Gz;fi
tar -zxvf nginx.tar.gz
cdNginx 1.4.2 /. / configure -- -- sbin - path = / toolings/xuewei/nginx/nginx \ conf - path = / toolings/xuewei/nginx/nginx. Conf \ - pid - path = / toolings/xuewei/nginx/nginx pid \ - with - http_ssl_module \ - with - pcre = / toolings/xuewei/pcre - 8.34 \ - with - zlib = / toolings/xuewei zlib - 1.2.11 \ - with openssl = / toolings/xuewei/openssl - t 1.0.1if [ ! -e $nginx];then make && make install ; fi

ln -s $nginx /usr/bin/nginx

cd /toolings/xuewei/nginx && ls -rtla

#netstat -ano|grep 80
netstat -tulnp|grep 80
cd / && nohup /toolings/xuewei/nginx/nginx &
netstat -tulnp|grep 80|egrep -v '80 [0-9] +'

echo "Nginx is started"
Copy the code

Code parsing:

The above code is divided into three main steps

  1. Check whether it is required. Otherwise, download dependent software
  2. Compile dependent software one by one
  3. Compile and install nginx and start it

This starts a Web server and the script runs like this:

A key to install

The code on the need to configure a Linux virtual machine or have a Linux server, more troublesome.

So create a directory to place Vagrantfile in, and then run vagrant up to start up.

The specific operation is as follows. It can be used repeatedly after completing one operation.

Create a directory to place the Vagrantfile in

So the academy prepared the following Vagrantfie.

Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.box_check_update = false
  config.vm.provider "virtualbox"

  $num_vms = 1
  (1..$num_vms).each do |id|
    config.vm.define "nginxbox#{id}" do |node|
      node.vm.hostname = "nginxbox#{id}"
      #node.vm.network :private_network, IP: "10.64.4.#{id}", auto_config: true
      config.vm.network "forwarded_port", guest: 80, host: 80, auto_correct: true # nginx  
      node.vm.provider :virtualbox do |vb, override|
        vb.name = Nginxbox#{id}
        vb.gui = false
        vb.memory = 2048
        vb.cpus = 1
      end
      
      config.vm.provision "shell", path: "leixuewei.sh"

    end
  end
end
Copy the code

Then open the terminal to run Vagrant Up

This allows you to install Nginx in one click and create a virtual machine that you can share with others.

Other operations (non-essential operations)

If you want to speed up the installation, you need to learn more about Vagrant -> Vagrant uses notes and then adds the following to the Vagrantfile. And download the package along with it.


      config.vm.provision "file".source: "Lib/nginx - 1.4.2. Tar. Gz." ", destination: "/tmp/nginx.tar.gz"
      config.vm.provision "file".source: "Lib/openssl - t.t ar 1.0.1. Gz." ", destination: "/tmp/openssl.tar.gz"
      config.vm.provision "file".source: "The lib/pcre - 8.34. Tar. Gz", destination: "/tmp/pcre.tar.gz"
      config.vm.provision "file".source: "Lib/zlib - 1.2.11. Tar. Gz." ", destination: "/tmp/zlib.tar.gz"
      config.vm.provision "file".source: "nginx.conf", destination: "/tmp/nginx.conf"
Copy the code

Readers can download the dependencies first

Nchc.dl.sourceforge.net/project/lib… https://www.openssl.org/source/openssl-1.0.1t.tar.gz jaist.dl.sourceforge.net/project/pcr… http://nginx.org/download/nginx-1.4.2.tar.gz

Optional, download the above package and place it under lib in the current directory.

Learn some principles and customize your page

How does default install Nginx work internally?

Nginx by reading/toolings/xuewei/nginx/nginx. Conf file, load internal server module. Server module to monitor the port 80, at the same time, alluding to the web site of the current directory to the HTML file (i.e., / toolings/xuewei/nginx/HTML directory), load the static files inside.

All right, that’s all you need to know.

We got inside the virtual machine (Linux host)/toolings/xuewei/nginx/HTML directory, find the following index. The HTML. Change the title and welcome content to Chinese as follows:



The main changes are:

  • Modify the title tag block, add the title ‘[Lei Xuewei]’
  • Modify the text of the H1 tag block inside the body to: Welcome to Nginx

Save and refresh the browser page, the changes will take effect immediately! Because Nginx loads static resources directly from HTML directories.

Here comes the key: how to turn the default site into a personal site

Easy to do, package your personal web page and unzip it into the HTML directory above, overwriting the files inside including index.html.

Nginx is very lightweight but also supports large concurrency (single ten thousand level concurrency), website load speed directly take off! Unless, of course, someone else is Nginx.

That’s all you have to share. Use it.

If you have a dynamic website, nginx can handle static content directly, and tomcat can handle dynamic content, which will at least double the performance. We’ll talk more about that later.

By the way, there is also this you can follow for a long time to read the thunder committee interesting programming story compilation or the thunder Committee NodeJS series

Continuous learning and continuous development, I am Lei Xuewei! Programming is fun. The key is to get the technology right. Welcome to pay attention, and feel free to like the collection!