1. Installation of Nginx
1.下载 : wget http://nginx.org/download/nginx-1.13.7.tar.gz
2. Installation environment configuration
yum install gcc-c++
yum install pcre pcre-devel
yum install zlib zlib-devel
yum install openssl openssl-devel
3. Unzip: tar -zxvf nginx-1.13.7.tar.gz
4. Compile and install:
cd nginx-1.13.7 (that is, in the decompressed nginx file)
./configure compile
先 make 再 make install
5. Check the version: /usr/local/nginx/sbin/nginx -V
6. Start and restart:
/usr/local/nginx/sbin/nginx 启动
/usr/local/nginx/sbin/nginx -s reload 重启
7. Access the Nginx server
http://localhost (i.e. Alibaba Cloud's public IP)
The success is as follows:
8. View progress and stop:
/usr/local/nginx/sbin/nginx -s stop Quick stop
/usr/local/nginx/sbin/nginx -s quit Complete stop
ps -ef | grep nginx View comprehensive process of all processes
kill -quit Main process number Stop process
kill -term main process number to quickly stop
kill -9 nginx forced stop
9. Add firewall exceptions
// Make port 80 a firewall exception
vim /etc/sysconfig/iptables
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCERT added to the file
/etc/init.d/iptables restart restart the firewall
10. Nginx startup
1. Write a shell script
# 添加修改此文件:vi /etc/init.d/nginx
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
# 注意下面的两行要修改成你的实际路径
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL
2. Set the access permissions of the file
chmod a+x /etc/init.d/nginx
3. Add to rc.local file
# 修改vi /etc/rc.local,添加如下内容
/etc/init.d/nginx start
4. Set boot up
chkconfig --add nginx #添加系统服务
chkconfig --level 345 nginx on #设置开机启动,启动级别
chkconfig --list nginx #查看开机启动配置信息
11 Differentiate different virtual hosts by port
1. Modify the nginx configuration file (ie /usr/local/nginx/conf/nginx.conf ) and add the following content
# 在配置文件中添加server节点:写入多份server节点,其端口号不同来区分虚拟主机
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
}
server {
listen 81;
server_name localhost;
location / {
root html81;
index index.html index.htm;
}
}
2. Restart the service
/usr/local /nginx/sbin/nginx -s reload
- Differentiate different virtual hosts by domain name
- Note that the domain name resolution rules:
一个域名对应一个IP地址,一个IP地址可以被多个域名绑定
1. On the local Windows computer, modify the C:\Windows\System32\drivers\etc\hosts file
192.168.31.117 manager.dhc.com
192.168.31.117 portal.dhc.com
2. In the server Centos7, modify the /usr/local/nginx/conf/nginx.conf file (add mm and pp directories under the corresponding path)
server {
listen 80;
server_name manager.dhc.com;
location / {
root mm;
index index.html index.htm;
}
}
server {
listen 80;
server_name portal.dhc.com;
location / {
root pp;
index index.html index.htm;
}
}
3. Restart the service to load the configuration file
/usr/local /nginx/sbin/nginx -s reload