Alibaba Cloud ECS (Ubuntu) to build nginx server detailed steps tutorial

Source: Internet
Author: User
Tags ftp install openssl openssl versions administrator password file transfer protocol nginx server port number

Assume that the ip address of the Alibaba Cloud server is 192.168.1.10 (you need to replace it with your own ip address), and no installation package is selected. Currently, ECS is completely empty, except for the required Linux system. Another point is that Ubuntu Linux 14.04 64-bit is selected. Linux saves a lot of memory, because there is no UI, and it is purely console-based.

This article only installs the nginx server. Other sections will be configured later, such as PHP, Java, and MySQL.

You need to complete the following two tasks before installation.

Find a tool to upload files (such as the nginx installation package)

Connect to the Console of the server to execute commands

The server is still empty, so no ftp service is started. Therefore, sftp can only be used. For what is sftp, how to use sftp (SSH File Transfer Protocol) and search for it on the Internet by yourself. In short, sftp uses the SSH protocol. The default port number is 22. The usage is similar to that of ftp. Most ftp clients support sftp. Anyone can find a tool that supports sftp. Then, use the ECS user name (root) and password to connect to sftp.

To connect to the Console of the server, find an SSH client. For example, on Mac OS X, run sudo ssh 192.168.1.10. At this time, the system will require you to enter the OS X Administrator password. After passing the password, the system will require you to enter the root user password of the ECS. If entered correctly, the system will enter the ECS Console. Enter the following welcome information.

Welcome to Ubuntu 14.04.2 LTS (GNU/Linux 3.small-32-generic x86_64)

* Documentation: https://help.ubuntu.com/

Welcome to aliyun Elastic Compute Service!

After both tasks are completed, perform the following steps.

PS: all the following commands are executed on the ECS Console.

Apt-get install zlib1g zlib1g. dev

Because nginx rewrite requires pcre library, you must first run the following command on Linux to install pcre (the following command is only for Ubuntu Linux, and other Linux versions require other commands ).

Apt-get install libpcre3 libpcre3-dev

Step 2: download and upload the nginx installation package

We can install nginx through apt-get, but it is not the latest version. If you have obsessive-compulsive disorder, you must install the latest version. This may not be suitable for apt-get, you can download the latest nginx source code package from the official website and install the compiled version.

You can download the latest version of nginx from the http://nginx.org/en/download.html. Currently, it is 1.9.3.

Download nginx from the black box.

 


After the download is complete, use the sftp tool to upload the file to the ECS. If you have not purchased an external hard drive, you can upload it to the system disk. 20 GB system disk for Linux. About 18 GB is left for the experiment.

Run the following command to decompress the nginx installation package.

Tar-xzf nginx-1.9.3.tar.gz

The directory after decompression is a nginx-1.9.3. Now go to this directory.

Step 2: install the corresponding library

To compile nginx, you must use http_stub_status_module and http_ssl_module. Therefore, you must install zlib and OpenSSL.

Run the following command to install zlib.

Apt-get install zlib1g zlib1g. dev

Use the following command to install OpenSSL.

Apt-get install libssl-dev

Step 2: use OpenSSL source code

If an error occurs during compilation, the possible cause is the OpenSSL issue. Therefore, the most secure way is to compile the OpenSSL source code together with the nginx source code. Readers can go to the https://www.openssl.org/source to download the latest OpenSSL source code. After the download, use the sftp tool to upload the file to the ECS. Use tar to decompress the package. Assume that the decompressed directory is/usr/local/openssl.

Step 2: hide the nginx version

Although this step is not necessary, for the sake of security, nginx versions are usually hidden before compilation, which can also cause some trouble for attackers.

Now go to the <nginx source code Root Directory>/src/core, and use vi to open the nginx. h header file. The code before modification is as follows:

# Ifndef _ NGINX_H_INCLUDED _
# Define _ NGINX_H_INCLUDED _
# Define nginx_version 1009003
# Define NGINX_VERSION "1.9.3"
# Define NGINX_VER "nginx/" NGINX_VERSION

# Ifdef NGX_BUILD
# Define NGINX_VER_BUILD NGINX_VER "(" NGX_BUILD ")"
# Else
# Define NGINX_VER_BUILD NGINX_VER
# Endif
# Define NGINX_VAR "NGINX"
# Define NGX_OLDPID_EXT ". oldbin"
# Endif/* _ NGINX_H_INCLUDED _*/


The modified code is as follows:

# Ifndef _ NGINX_H_INCLUDED _
# Define _ NGINX_H_INCLUDED _

# Define nginx_version 1009003
# Define NGINX_VERSION ""
# Define NGINX_VER "super" NGINX_VERSION
# Ifdef NGX_BUILD
# Define NGINX_VER_BUILD NGINX_VER "(" NGX_BUILD ")"
# Else
# Define NGINX_VER_BUILD NGINX_VER
# Endif

# Define NGINX_VAR "super"
# Define NGX_OLDPID_EXT ". oldbin"


Step 2: Configure nginx

Compiling the source code in Linux/OS X is basically a routine. First, configure and then make install. If "." is not set for PATH, you also need to use "./configure ".

Enter the nginx source code root directory, and then execute the following command

./Configure -- prefix =/mnt/disk1/nginx -- with-http_stub_status_module -- with-http_ssl_module -- with-openssl =/usr/local/openssl

"/Mnt/disk1/nginx" indicates the target directory to be installed. "/Usr/local/openssl" is the OpenSSL source code directory. In this way, nginx will be compiled together with OpenSSL.

Step 2: compile nginx

If the preceding steps are correct, run make install to compile and install nginx. After compilation, the target files will be put in the/mnt/disk1/nginx directory.

Step 2: Check whether the configuration file is correct

Enter the/mnt/disk1/nginx/sbin directory, and then run nginx-t. If the following information is entered, the configuration file is correct (after each configuration file is modified, before starting the nginx service, it is best to execute this command ).

Nginx: the configuration file/mnt/disk1/nginx/conf/nginx. conf syntax is OK

Nginx: configuration file/mnt/disk1/nginx/conf/nginx. conf test is successful

Step 2: Start the nginx service

Go to the/mnt/disk1/nginx/sbin directory and run the nginx command to start the nginx service. If port 80 is occupied, it may be that the nginx service has been started. Run pkill nginx to kill the nginx service. You can run the ps-ef | grep nginx command to check whether there are any nginx processes in the memory.

Step 2: Test the nginx service

Open the browser and enter http: // 192.168.1.10 in the address bar. If the following information is output, nginx is configured successfully.


Enter a path. For example, http: // 192.168.1.10/abcd does not exist because the path does not exist. Therefore, the error 404 is entered. However, because the nginx version is hidden, only the output information shown in the following figure is displayed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.