Automatically install nginx and configure nginx on Amazon

Source: Internet
Author: User
ArticleDirectory
    • Note: # The chkconfig:-85 15 line must have a space between-and 85; otherwise, you may not be able to use chkconfig to configure boot.
    • Find the server configuration and modify it to the following configuration, where website.com is the website Domain Name
    • To ensure security, we can first test whether the configuration file has syntax errors.
    • After the configuration file is tested correctly, reload the configuration file to make the configuration take effect.

It is easy to download, compile, and install. install several common dependency packages first, so you can install the missing dependency packages in order not to repeatedly check the configure results, directly use this script to install it. Save the script to any text format. Run the sh command to install nginx on the cloud host of Amazon.

 
#! Bin/bashsudo Yum install updatesudo Yum install gcc pcre-devel zlib OpenSSL-ywget http://nginx.org/download/nginx-1.2.8.tar.gztar-xzvf nginx-1.2.8.tar.gzcd nginx-1.2.8sudo./configuresudo make & make install

Start nginx

 
Sudo/usr/local/nginx/sbin/nginx

Nginx Control

Sudo/usr/local/nginx/sbin/nginx-S Stop stop the daemon immediately (use the team signal) sudo/usr/local/nginx/sbin/nginx-s quit gently stops the daemon process (quit signal) sudo/usr/local/nginx/sbin/nginx-s reopen Log File sudo/usr/local/nginx/sbin/nginx-s reload re-load configuration file

If the configuration file is changed and the configuration file is invalid, you may not be able to terminate the nginx daemon. You can use this method to terminate the nginx daemon.

 
Sudo killall nginx termination process for emergency use

Test Configuration File

 
Sudo/usr/local/nginx/sbin/nginx-T [-C configuration file path]

Why test the configuration file? A configuration error may lead to loss of control over nginx. As mentioned above, even running services cannot be stopped and processes can only be ended forcibly, this is not good, so you can first use this command to test whether the configuration file is correct, and then decide whether to use the configuration file.

After setting the configuration file, you don't have to restart nginx. You only need to reload the configuration file (see the reload command above). This is the same as Apache (many people say that you need to restart Apache after changing the configuration file, in fact, you don't have to. You only need to reload the configuration file to take effect, which is very fast and will not cause service interruption ).

After nginx is installed, it is not registered as a system service by default. Therefore, you need to manually add a system service script to provide you with a nginx service script that I wrote and tested, and copy it directly, and put it in/etc/init. d/. Change the name to nginx and change the CHMOD + x/etc/init permission. d/nginx.

Start nginx

 
Service nginx start

Or

 
/Etc/init. d/nginx start

You can directly view the script using other commands, which is very simple.

#! /Bin/sh # The script used to register nginx as a system service # author cplushua # http://hua.219.me # chkconfig:-85 15 set-epath =/usr/local/sbin: /usr/local/bin:/sbin:/bin:/usr/sbin: /usr/bindesc = "nginx daemon" name = nginxdaemon =/usr/local/nginx/sbin/$ namescriptname =/etc/init. d/$ name # exit test-x $ daemon if the daemon process does not exist | exit 0d_start () {$ daemon | echo-n "aready running"} d_stop () {$ daemon-s quit | echo-n "not running"} d_reload () {$ daemon-s reload | echo-n "cocould not reload"} case "$1" instart) echo-n "Starting $ Desc: $ name" d_startecho ". "; stop) echo-n" Stopping $ Desc: $ name "d_stopecho ". "; RELOAD) echo-n" reloading $ Desc: configurationg ..... "d_reloadecho" reloaded. "; restart) echo-n" Restarting $ Desc: $ name "d_stopsleep 3d_startecho ". "; *) echo" Usage: $ scriptname {START | stop | restart | reload} "> & 2 exit 3; esacexit 0
Note: # The chkconfig:-85 15 line must have a space between-and 85; otherwise, you may not be able to use chkconfig to configure boot.

 

[Fast switch nginx as the website front-end Proxy Server]

 

Compared with Apache's high-performance and high-concurrency features, nginx has been widely used in production environments. If the website used apache, how can we quickly use nginx as a front-end proxy server to provide services?

Use a simple configuration file. Here, we discard complicated switching and many other factors to be considered in the production environment, and simply explain the implementation method.

Find the nginx configuration file, which is usually located in/usr/local/nginx/conf and named nginx. conf. To test the configuration of Apache, set the nginx service to port 81 without modifying any configuration of Apache.

Find the server configuration and modify it to the following configuration, where website.com is the website Domain Name
 
Server {Listen 81; SERVER_NAME website.com; # charset koi8-r; # access_log logs/host. Access. Log main; Location/{proxy_pass http://website.com; # Root HTML; # index index.html index.htm ;}
To ensure security, we can first test whether the configuration file has syntax errors.

Run the following command to test whether the syntax is correct.

 
Sudo/usr/local/nginx/sbin/nginx-T

If it is correct, content similar to the following will be displayed

Nginx: the configuration file/usr/local/nginx/CONF/nginx. conf syntax is oknginx: configuration file/usr/local/nginx/CONF/nginx. conf test is successful
Why must I test whether the configuration file is correct?

1. Each line of the nginx configuration file is followed by a semicolon. Many users forget to add a semicolon for the first time. Some inexplicable errors may occur, such as missing brackets}
2. loading the configuration file during nginx running may result in uncontrolled processes. Even if the Stop command cannot stop the process, you must first test whether the configuration file is correct.
(PS: if the process is not controlled, You have to force the process to be killed. You can use this command to kill The sudo killall nginx process)

After the configuration file is tested correctly, reload the configuration file to make the configuration take effect.
 
Sudo/etc/init. d/nginx reload

The configuration file is successfully loaded.
Open your browser and enter the Domain Name of the website configured above (the domain name of the original website) + port 81, such as the website.com given in the configuration file, you can use http://website.com: 81 to access, in this way, nginx is set as the front-end proxy server.
In Chrome, you can open the console, find the network, and check whether the response header of the first file to be loaded is nginx.
Here is my. You can see that the server line has changed to nginx

 

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.