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.
OS:CentOS 6.7 x64
Nginx:1.10.2
(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
(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
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
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
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 ]
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:
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
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