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