CentOS 6.x compile and install Nginx

nginx [engine x] is an HTTP and reverse proxy server, a mail proxy server, and a generic TCP/UDP proxy server, originally written by Igor Sysoev. For a long time, it has been running on many heavily loaded Russian sites including Yandex, Mail.Ru, VK, and Rambler. According to Netcraft, nginx served or proxied 24.68% busiest sites in June 2018. Here are some of the success stories: Dropbox, Netflix,Wordpress.com, FastMail.FM.

An excerpt from the official website of nginx http://nginx.org/en/, you can see that nginx is an HTTP proxy server and reverse proxy server, mail proxy server, TCP/UDP proxy server, load balancing The function is very powerful.

One, system environment##


OS:CentOS 6.7 x64

Nginx:1.10.2

Two, install Nginx


1 Dependent installation###

(1) Install gcc

Since it is a compilation and installation, a gcc compilation environment is required, execute the following command to check whether gcc has been installed

gcc -v

If gcc is not installed, execute the following command to install:

[ root@lab1 php]# yum -y install gcc

After the installation is complete, execute gcc -v to see the version information

[ root@lab1 php]# gcc -v

Using built-in specs.

Target: x86_64-redhat-linux

Configured with:../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=[http://bugzilla.redhat.com/bugzilla](http://bugzilla.redhat.com/bugzilla)--enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux

Thread model: posix

gcc version 4.4.720120313(Red Hat 4.4.7-18)(GCC)

(2) Install pcre/zlib/openssl

The root user executes the following command to install pcre/zlib/openssl

yum -y install pcre pcre-devel  

yum -y install zlib zlib-devel

yum -y install openssl openssl-devel

Note: Steps (1) (2) can also execute a comprehensive command to install together

yum -y install gcc zlib zlib-devel pcre pcre-devel openssl openssl-devel

2 Install Nginx

(1) Download nginx source code

Nginx official download address:

http://nginx.org/download/

Choose version 1.10.2 here, and the root user executes the following command to download the nginx source package

[ root@lab1 nginx]# pwd

/opt/nginx

[ root@lab1 nginx]# wget http://nginx.org/download/nginx-1.10.2.tar.gz

Note: If you use another version, just change the 1.10.2 above to another version, provided that the official version has a corresponding version...

(2) Unzip

[ root@lab1 nginx]# pwd

/opt/nginx

[ root@lab1 nginx]# ls

nginx-1.10.2.tar.gz

[ root@lab1 nginx]# tar zxf nginx-1.10.2.tar.gz

(3) Compile and install

The root user executes the following commands to compile nginx source code:

[ root@lab1 nginx]# cd nginx-1.10.2[root@lab1 nginx-1.10.2]# pwd

/opt/nginx/nginx-1.10.2[root@lab1 nginx-1.10.2]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

Compiled successfully:

Note:

(1) The compilation directory is specified above, --prefix=/usr/local/nginx means to compile to the /usr/local/nginx environment, and the /usr/local/nginx directory will not be generated at this time.

(2) If you do not specify the path, it will be installed in /usr/local/nginx by default

(3) After installation (make install), you can use whereis nginx to query the installation location

(4) - - with-httpstubstatusmodule --with-httpssl_module means adding http status module and http ssl module

Root continues to execute the following command to install:

[ root@lab1 nginx-1.10.2]#

[ root@lab1 nginx-1.10.2]# pwd

/opt/nginx/nginx-1.10.2[root@lab1 nginx-1.10.2]# make && make install

Execute the following command to view the nginx installation location:

[ root@lab1 nginx-1.10.2]# whereis nginx

nginx:/usr/local/nginx

So far, nginx has been successfully installed

Three, nginx commonly used commands##


1 Test configuration file###

Execute ./nginx -t in sbin under the nginx installation path to test the configuration file, as shown below:

[ root@lab1 sbin]# pwd

/usr/local/nginx/sbin

[ root@lab1 sbin]# ./nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

2 Start and stop commands###

Startup: execute under sbin under the installation path./nginx

[ root@lab1 sbin]# pwd

/usr/local/nginx/sbin

[ root@lab1 sbin]# ./nginx

Stop: execute ./nginx -s stop or ./nginx -s quit under sbin under the installation path

[ root@lab1 sbin]# pwd

/usr/local/nginx/sbin

[ root@lab1 sbin]# ./nginx -s stop

[ root@lab1 sbin]# ./nginx -s quit

Restart: execute ./nginx -s reload under sbin under the installation path

[ root@lab1 sbin]# pwd

/usr/local/nginx/sbin

[ root@lab1 sbin]# ./nginx -s reload

Graceful restart: kill -HUP [nginx main process number]

[ root@lab1 sbin]# ps -ef|grep nginx|grep master|awk '{print $2}'|xargs kill -HUP

3 Configure firewall###

The root user executes the following command to edit the firewall rule file

[ root@lab1 sbin]# vim /etc/sysconfig/iptables

Add the following line under the 22 port configuration:

- A INPUT -m state --state NEW -m tcp -p tcp --dport 80-j ACCEPT

The root user executes the following command to restart the firewall:

[ root@lab1 sbin]# vim /etc/sysconfig/iptables

[ root@lab1 sbin]# service iptables restart

iptables: Setting chains to policy ACCEPT: filter          [  OK  ]

iptables: Flushing firewall rules:[  OK  ]

iptables: Unloading modules:[  OK  ]

iptables: Applying firewall rules:[  OK  ]

4 Verify nginx

View the process:

[ root@lab1 sbin]# ps -ef|grep nginx

root       89171012:47?00:00:00 nginx: master process ./nginx

nobody     89348917012:48?00:00:00 nginx: worker process

root       89901477012:51 pts/100:00:00 grep nginx

Enter in the browser:

http://192.168.56.130/

If the following content appears, it means that nginx is running normally:

Appendix 1: Configure nginx environment variables##


The root user edits the system environment variable file:

[ root@lab1 sbin]# vim /etc/profile

Add the following information at the end of the file, save and exit:

##### nginx #####

export NGINX_HOME=/usr/local/nginx

export PATH=$NGINX_HOME/sbin:$PATH

Note:

(1) /usr/local/nginx is stored in the same directory as the installed nginx, you can use whereis nginx to view the nginx installation directory

The root user executes the following commands to make the environment variables take effect:

[ root@lab1 sbin]# source /etc/profile

verification:

The root user executes the following command to check whether there is a nginx directory in the PATH

[ root@lab1 sbin]# echo $PATH

/usr/local/nginx/sbin:/opt/hbase/hbase-1.3.1/bin:/opt/hadoop/hadoop-2.7.4/bin:/opt/storm/apache-storm-1.0.4/bin:/opt/zookeeper/zookeeper-3.4.10/bin:/usr/java/jdk1.8.0_20/bin:/opt/hbase/hbase-1.3.1/bin:/opt/hadoop/hadoop-2.7.4/bin:/opt/storm/apache-storm-1.0.4/bin:/opt/zookeeper/zookeeper-3.4.10/bin:/usr/java/jdk1.8.0_20/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

After configuring the environment variables, you can execute nginx directly in any directory

nginx #Start nginx

nginx -s stop    #Stop nginx

nginx -s quit    #Stop nginx

ngins -s reload    #Restart nginx

Appendix II. Startup after booting##


Add rc.local script###

The root user edits the /etc/rc.local script and adds the following content, save and exit:

vi /etc/rc.local

# nginx config

/usr/local/nginx/sbin/nginx

Example:

Note:

(1) /usr/local/nginx/sbin/nginx This configuration is related to your nginx installation directory. If your nginx is installed in other directories, then you need to configure the sbin/nginx command in your own installation directory.

(2) The rc.local script is executed after all other initialization scripts are executed. Therefore, the self-starting service configured in the script may have a partial delay.

Recommended Posts

CentOS 6.x compile and install Nginx
Centos compile and install Git
Centos7 compile and install ntp-4.2.8p11
CentOS 6.9 compile and install python
CentOS 6 compile and install python 3
Compile and install LAMP under Centos 5.2
CentOS7 compile and install L(A|N)MP environment
Centos7 install Nginx
Linux CentOS6 compile and install Pyt
Compile and install libmodbus library under CentOS7
Centos7 compile and install MySQL8 problem record
Compile and install nodejs and yum in Centos8
CentOS7.4 source code compile and install MySQL8.0
Install centos7 and connect
CentOS 6.X install VirtualBox-5.1
Centos7 install mongodb 4.x
Centos6.5 compile and install LNMP architecture web environment
Centos7.2 compile and install way to build phpMyAdmin
centos7 install python3 and ipython
Know Linux and install CentOS
Install ElasticSearch 7.x on CentOS 7
CentOS 7 install Mono and MonoDevelop
Ubuntu 16.04 compile and install PHP 7.2
ubuntu18.04 compile and install python3.8
CentOS6.5 install Java 8 and Tomcat8
Centos6.5 install and configure mongodb
Tencent Cloud Centos install nginx
CentOS7 install python3 and pip3
CentOS7 install OracleJDK and JRE
CentOS6.5 install Java 8 and Tomcat8
Centos6 install mysql 5.7.x series
CentOS6 install and crack Jira 7
CentOS6.5 install Java 8 and Tomcat8
CentOS6 install and crack confluence
CentOS6 install and crack Jira 7
Install Nginx server on CentOS 7
Centos compile and install LAMP (apache-2.4.7 + mysql-5.5.35 + php 5.5.8) + Redis
Compile and install Lnmp shell script under Linux centos
"Source Production Control" series (2) Using Nginx 1.18 on CentOS 8: Compile, Install and Basic Use
Centos 7 install jdk and package service service
CentOS7 yum install and start mysql
Install and configure keepalived under CentOS 5.9
Compile and install the open source EDA tool-Surelog on CentOS8
CentOS 8 install Git and basic configuration
CentOS 7 install Nginx, PHP, MySQL packages
How to install Nginx on CentOS 8
CentOS7.2 and Nginx configuration virtual host
CentOS7.3 install iptables and detailed use
CentOS quickly install Python3 and pip3
CentOS7 yum install and start mysql
CentOS 8 - install and configure NFS service
Centos7 and centos8 install mysql5.6 5.7 8.0 so simple
Centos7 uninstall openJdk, and install JDK1.8
CentOS 7.X system installation and optimization
Install Python3 and ansible under CentOS8
Install and use docker under CentOS 6.8
1.5 Install Centos7
CentOS7 install and use SQL Server
How to compile and install PHP and Nginx in Ubuntu environment
Centos install php73 fast cgi + nginx
CentOS Minimal install and configure TIPS