What are we going to learn this time? It is to use Nginx to build the simplest web server displaying “Hello World” on a machine. then we will
Step by step let’s try~
1. Nginx package
The latest development version is 1.1.12:
We can download the stable version to try:
2. Download, decompress, and install Nginx
Here we take Linux/Unix: nginx-1.0.11.tar.gz as an example. Download and unzip:
wget http://nginx.org/download/nginx-1.0.11.tar.gz
tar -zxvf nginx-1.0.11.tar.gz
But do not rush to install after downloading and decompressing, because Nginx depends on a lot of software, we assume that your Linux environment is “clean”, so the following is mentioned
All packages that Nginx depends on. Please follow the steps below to install:
sudo apt-get install gcc
sudo apt-get install g++
sudo apt-get install make
sudo apt-get install libz-dev
sudo apt-get install libbz2-dev
sudo apt-get install libreadline-dev
This is some basic software, plus PCRE to install. PCRE stands for “Perl Compatible Regular
Short for “Expressions”, is a regular expression library. Download, extract and install PCRE:
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.13.tar.gz
tar -zxvf pcre-8.13.tar.gz
cd pcre-8.13.tar.gz
./configure
sudo make
sudo make install
At this time, install the nginx-1.0.11.tar.gz we just downloaded
tar -zxvf nginx-1.0.11.tar.gz
cd nginx-1.0.11
./configure
sudo make
sudo make install
3. Primary interpretation of Nginx configuration files
According to the above operation, Nginx has been installed. Because we are using the default
configure, so it will be installed in the /usr/local/nginx directory. The corresponding configuration file is /usr/local/nginx/conf/nginx.conf. We open the configuration file and see that the structure looks like this:
…
events {
…
}
http {
…
server {
…
}
…
}
Among them, events and http are the two most common modules in nginx configuration, and there are other core modules, which will be introduced one by one in subsequent articles. server is a submodule of the http module and is its most commonly used module.
4. Write a simple Nginx configuration
Create a /home/michael/test_space directory to store our test cases. Then directly in Nginx
Modify in the default configuration file /usr/loca/nginx/conf/nginx.conf, add a server module in http, as follows:
server {
listen 8011;
server_name localhost;
charset utf-8;
location / {
alias /home/michael/test_space/;
}
}
Among them, listen indicates the listening port number, and sever_name is the name of the web server (it can be a domain name, host or IP
address), charset specifies the coded character set, and the location here specifies the file directory of the web service through alias.
5. Start Nginx
Enter the /usr/local/nginx directory and enter:
sudo ./sbin/nginx
6. Test
In the /home/michael/test_space/ directory, resume an index.html file. In the file enter:
Hello World!
Then try to visit: http://localhost:8011/index.html If the following content appears, it means you have succeeded! ~
–