1、Nginx的安装html
1.下载 : wget http://nginx.org/download/nginx-1.13.7.tar.gzjava
2.安装环境配置nginx
yum install gcc-c++
yum install pcre pcre-devel
yum install zlib zlib-devel
yum install openssl openssl-develc++
3.解压 : tar -zxvf nginx-1.13.7.tar.gzweb
4.编译和安装:shell
cd nginx-1.13.7 (即解压后的nginx文件中)vim
./configure 编译windows
先 make 再 make installbash
5. 查看版本 :/usr/local/nginx/sbin/nginx -V服务器
6. 启动与重启 :
/usr/local/nginx/sbin/nginx 启动
/usr/local/nginx/sbin/nginx -s reload 重启
7. 访问Nginx服务器
http://localhost(即阿里云的公有IP)
成功以下图:
8. 查看进程与中止:
/usr/local/nginx/sbin/nginx -s stop 快速中止
/usr/local/nginx/sbin/nginx -s quit 完整中止
ps -ef | grep nginx 查看全部进程的全面进程
kill -quit 主进程号 中止进程
kill -term 主进程号 快速中止
kill -9 nginx 强制中止
9. 添加防火墙例外
// 将80端口为防火墙例外
vim /etc/sysconfig/iptables
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCERT 新增到文件中
/etc/init.d/iptables restart 重启防火墙
10. Nginx开机启动
1. 编写shell脚本
# 添加修改此文件: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.设置文件的访问权限
chmod a+x /etc/init.d/nginx
3. 加入到rc.local文件中
# 修改vi /etc/rc.local,添加以下内容 /etc/init.d/nginx start
4. 设置开机启动
chkconfig --add nginx #添加系统服务 chkconfig --level 345 nginx on #设置开机启动,启动级别 chkconfig --list nginx #查看开机启动配置信息
11 经过端口区分不一样的虚拟主机
1.修改nginx的配置文件(即 /usr/local/nginx/conf/nginx.conf ),添加如下内容
# 在配置文件中添加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.重启服务
/usr/local /nginx/sbin/nginx -s reload
一个域名对应一个IP地址,一个IP地址能够被多个域名绑定
1. 本地windows电脑,修改 C:\Windows\System32\drivers\etc\hosts文件
192.168.31.117 manager.dhc.com 192.168.31.117 portal.dhc.com
2. 服务器Centos7中,修改/usr/local/nginx/conf/nginx.conf文件(在对应路径下添加mm和pp目录便可)
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. 重启服务加载配置文件
/usr/local /nginx/sbin/nginx -s reload