Install and configure Nginx in Linux

Source: Internet
Author: User

Install and configure Nginx in Linux
Application scenarios

For a large website, Server Load balancer is an eternal topic. As its name suggests, Server Load balancer distributes loads to different service units, ensuring service availability and fast response, provide a good user experience. With the rapid development of hardware technology, more and more Server Load balancer hardware devices are emerging, such as F5 BIG-IP, Citrix NetScaler, and Radware, however, the high price is often prohibitive, So load balancing software is still the best choice for most companies. As a rising star of webserver, Nginx has received wide attention in the industry for its excellent reverse proxy functions and Flexible Load Balancing policies.

The Nginx process is based on the Master + Slave (worker) multi-process model and has a very stable sub-process management function. In Master process allocation mode, the Master process never performs business processing, but distributes tasks to achieve high reliability of the Master process.

Keepalived is a high-reliability running tool that implements VRRP backup routing in Linux. The Service Model Based on Keepalived can truly achieve seamless IP address transfer when the master server and backup server fail. A stable software Load Balancing solution can be built by combining the two.

Step 1. install and configure Nginx 1.1 Basic System Configuration
Disable selinux # vi/etc/selinux/config ...... SELINUX = disabled ...... Disable the firewall. In Centos6.5, # service iptables stop # chkconfig iptables off the firewall. In Centos7, # systemctl stop firewalld # systemctl disable firewalld to modify the limit on opening system files, added at the end of the configuration file # vi/etc/security/limits. conf * soft noproc 65535 * hard noproc 65535 * soft nofile 409600 * hard nofile 409600 restart the machine # reboot
1.2 Set Time Synchronization
Install ntpdate, ntpd # yum install-y ntpdate ntp pcre-devel pcre to copy the time zone to the local time zone # cp/usr/share/zoneinfo/Asia/Shanghai/etc/localtime time synchronization, change the ip address to the ip address of the available time server, and write the timestamp # ntpdate ip # hwclock-w to enable the ntpd service. In Centos6, # service start ntpd # service enable ntpd to enable the ntpd service, in Centos7 # systemctl start ntpd # systemctl enable ntpd
1.3 install the software package
Installation Package https://pan.baidu.com/s/1hruhlowinstallation dependency component # yum install zlib-devel # yum install pcre-devel copy the files in the installation package to the server, and decompress the installation package # tar-zxvf nginx-1.8.1.tar.gz and then enter the directory to install # cd nginx-1.8.1 #. /configure -- prefix =/opt/nginx1.8.1 # make install # ln-sf/opt/nginx1.8.1/usr/local/nginx # echo 'export PATH =/usr/local/nginx /sbin: $ PATH '>/etc/profile & source/etc/profile
1.4 Configuration Service
Create a STARTUP script to send the nginx file in the startup script folder to the server and # cp nginx/etc/init. d/# chmod + x/etc/init. d/nginx modify user permissions # useradd-r-M nginx # mkdir-p/var/log/nginx # chown nginx-R/var/log/nginx register startup service # chmod + x/etc/init. d/nginx # chkconfig -- add nginx # chkconfig nginx on
1.5 start the service
 # service nginx start
2. Configure High Availability
Note: Keepalived must be installed on both the master and slave Nginx servers.
2.1 install Keepalived
Install the dependency package and send keepalived6 (in Centos6 environment) or keepalived7 (in Centos7) to the server # tar zxvf keepalived6.tar.gz or tar zxvf keepalived7.tar.gz # yum localinstall keepalived /*. rpm-ytransfers keepalived-1.20..tar.gz from the software source to the server and decompress # tar zxvf keepalived-1.2.16.tar.gz to compile and install # cd keepalived-1.2.16 #. /configure # make install Configuration Service # cp/usr/local/sbin/keepalived/usr/sbin/# cp/usr/local/etc/rc. d/init. d/keepalived/etc/init. d/# cp/usr/local/etc/sysconfig/keepalived/etc/sysconfig/# chmod + x/etc/init. d/keepalived # chkconfig -- add keepalived # chkconfig keepalived on # mkdir/etc/keepalived
2.2 configure the master server
Create a configuration file # vi/etc/keepalived. conf modify the configuration file and save it! Configuration File for keepalivedglobal_defs {icationication_email {# abc@example.com} # icationication_email_from admin@example.com # smtp_server smtp.example.com # smtp_connect_timeout 30 router_id nginx_master} vrrp_script chk_http_port {script"
2.3 configure Backup Server
Create a configuration file # vi/etc/keepalived. conf modify the configuration file and save it! Configuration File for keepalivedglobal_defs {icationication_email {# abc@example.com} # icationication_email_from admin@example.com # smtp_server smtp.example.com # smtp_connect_timeout 30 router_id nginx_backup} vrrp_script chk_http_port {script"
2.4 installation Verification
When the browser accesses https: // The following prompt appears, indicating that nginx is running properly.

View the situation where keepalived is bound to a virtual ip address # ip

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.