Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install Nginx

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to install Nginx. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. A brief introduction to Nginx

Nginx is a free, open source, high-performance HTTP server and reverse proxy server; it is also an IMAP, POP3, SMTP proxy server; Nginx can be used as a HTTP server to publish websites, and Nginx can be used as a reverse proxy for load balancing.

For more information about Nginx and its role, please help yourself to Baidu.

two。 Install the Nginx basic dependency package

[root@nginx ~] # yum install gcc gcc-c++ pcre-devel zlib-devel openssl-devel-y

3. Download the related software package 3.1 download nginx stable version

Note: go to the download address of the Nginx installation package to copy the link to the version you want. I'll take Nginx-1.14.0 as an example here.

[root@nginx~] # cd / usr/local/src/ [root@nginx src] # wget http://nginx.org/download/nginx-1.14.0.tar.gz

Download nginx-sticky module (optional installation for cluster)

Function: the backend does load balancing to solve the session sticky problem.

[root@nginx src] # wget https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/master.tar.gz

Download ngx_cache_purge module (optional installation)

Purpose: compiles ngx_cache_purge into Nginx to clear the cache of the specified URL.

(official website address: http://labs.frickle.com/nginx_ngx_cache_purge/)

[root@nginx src] # wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz

4. Add Nginx users and groups

[root@nginx ~] # groupadd www [root@nginx ~] # useradd-g www www-s / sbin/nologin

Note: create nginx to run user www and join the www group. Www users are not allowed to log in directly to the system.

5. Extract the Nginx source code package and enter the decompression directory for compilation and installation

[root@nginx src] # tar zxf nginx-1.14.0.tar.gz # Note 3.2optional [root@nginx src] # tar zxf ngx_cache_purge-2.3.tar.gz # Note 3.3.optional [root@nginx src] # tar zxf nginx-goodies-nginx-sticky-module-ng-08a395c66e42.tar.gz [root@nginx src] # cd nginx-1.14.0/

Note: the'= 'sign is followed by the absolute path of your own installation package, and the configuration file path.

[root@nginx-master nginx-1.14.0] # / configure-- prefix=/usr/local/nginx >-- user=www-- group=www >-- with-http_stub_status_module >-- with-http_realip_module-- with-http_ssl_module >-- with-http_gzip_static_module >-- http-client-body-temp-path=/var/tmp/nginx/client >-- http-proxy-temp-path=/var/ Tmp/nginx/proxy >-- http-fastcgi-temp-path=/var/tmp/nginx/fcgi# Note 3.2options >-- with-pcre-- add-module=/usr/local/src/ngx_cache_purge-2.3 # Note 3.3options >-- add-module=/usr/local/src/nginx-goodies-nginx-sticky-module-ng-08a395c66e42

After the compilation is completed:

[root@nginx nginx-1.14.0] # make & & make install

Note: all modules of Nginx must be added at compile time and can no longer be dynamically loaded at run time.

6. Optimize the execution path of Nginx programs

[root@nginx nginx-1.14.0] # ln-s / usr/local/nginx/sbin/nginx / usr/local/sbin/ [root@nginx nginx-1.14.0] # nginx- t

If the following problems occur:

You need to create this directory:

[root@nginx nginx-1.14.0] # mkdir-p / var/tmp/nginx/client

7. Configure commands such as Nginx boot and self-boot to facilitate us to manage Nginx

The original command to manage nginx is not very convenient!

[root@nginx-master ~] # nginx start nginx [root @ nginx-master ~] # nginx- s reload restart nginx [root@nginx-master ~] # nginx- s stop stop nginx

Next, set up commonly used administrative commands:

1) create a boot command script file:

[root@nginx ~] # vim / etc/init.d/nginx

2) insert the startup script code into the Nginx file:

-- the script is as follows-- #! / bin/bash# chkconfig:-85 15PATHSPAGUSCUR _ NAME=nginxDAEMON=$PATH/sbin/$NAMECONFIGFILE=$PATH/conf/$NAME.confPIDFILE=$PATH/logs/$NAME.pidscriptNAME=/etc/init _ nginxDESC = "nginx daemon" .d / $NAMEset-e [- x "$DAEMON"] | | exit 0do_start () {$DAEMON-c $CONFIGFILE | | echo-n "nginx already running"} do_stop () {$DAEMON-s stop | | echo-n "nginx not running"} do_reload () {$DAEMON-s reload | | echo-n "nginx can't reload"} case "$1" instart) echo-n "Starting $DESC: $NAME" do_startecho. " ; stop) echo-n "Stopping $DESC: $NAME" do_stopecho ".;; reload | graceful) echo-n" Reloading $DESC configuration... "do_reloadecho".;; restart) echo-n "Restarting $DESC: $NAME" do_stopdo_startecho ";; *) echo" Usage: $scriptNAME {start | stop | reload | restart} "> & 2exit 3X esacexit 0 [root@nginx ~] # cd / etc/init.d/

3) set everyone has permission to execute the startup script nginx file:

[root@nginx init.d] # chmod axix nginx

4) add nginx to the system service:

[root@nginx init.d] # chkconfig-- add nginx

5) set the service to boot:

[root@nginx init.d] # chkconfig nginx on

Note: reboot restart system takes effect!

6) after restarting, you can execute the following command to manage

Systemctl start nginx.service # start nginxsystemctl stop nginx.service # end nginxsystemctl restart nginx.service # restart nginxsystemctl enable nginx.service # Boot start

8. If Nginx fails to start, try the following methods

1) if httpd has been installed before, the side needs to uninstall httpd first, otherwise the startup fails unless the Nginx startup port is modified.

[root@nginx ~] # yum remove httpd-y

2) if it still doesn't start, try this method again: after killing the Nginx-related process, start Nginx

[root@nginx ~] # ps-A | grep nginx [root@nginx ~] # kill-9 pid1 [root@nginx ~] # kill-9 pid2

3) if it still fails, check the log information to solve the problem.

9. Start Nginx and access the test

Note: continue to update Nginx optimization and reverse proxy functions!

Thank you for reading! This is the end of the article on "how to install Nginx". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report