linux服務(wù)器上安裝nginx,部署多項目

Nginx安裝

Nginx下載

官網(wǎng)下載:http://nginx.org/en/download.html
或者直接在linux執(zhí)行命令:wget http://nginx.org/download/nginx-1.12.2.tar.gz
這里下載的版本是1.12.2

安裝步驟
# 安裝依賴
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
# 解壓縮
tar -zxvf linux-nginx-1.12.2.tar.gz
cd nginx-1.12.2/
# 執(zhí)行配置
./configure
# 編譯安裝(默認(rèn)安裝在/usr/local/nginx)
make
make install

防火墻配置
nginx默認(rèn)監(jiān)聽80端口,如果未關(guān)閉防火墻需要配置iptables規(guī)則開放80端口(以centos6為例)。
編輯配置文件:vim /etc/sysconfig/iptables
在文件中間添加iptables規(guī)則
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
重啟防火墻:service iptables restart
或者關(guān)閉iptables規(guī)則:iptables -F && iptables -t nat -F

Nginx驗證

nginx主配置文件:/usr/local/nginx/conf/nginx.conf
nginx日志文件:/usr/local/nginx/logs/access.log
啟動Nginx:/usr/local/nginx/sbin/nginx
然后直接訪問ip地址,比如:http://192.168.0.110/,如果能看到如下Nginx主頁說明安裝ok。

Nginx常用命令
查找安裝路徑:
whereis nginx
進(jìn)入sbin目錄,可以看到有一個可執(zhí)行文件nginx,直接./執(zhí)行就OK了。

測試配置文件:{Nginx}/sbin/nginx -t 啟動命令:{Nginx}/sbin/nginx
停止命令:{Nginx}/sbin/nginx -s stop/quit 重啟命令:{Nginx}/sbin/nginx -s reload
查看進(jìn)程命令:ps -ef | grep nginx
平滑重啟:kill -HUP [Nginx主進(jìn)程號(即ps命令查到的PID)]

【部署問題】解決Nginx: [error] open() "/usr/local/Nginx/logs/Nginx.pid" failed(2:No such file or directory)

問題:環(huán)境問題

image

解決方法:

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

[Nginx 配置多站點vhost]

假設(shè)你想在Linux Nginx中用不同的域名訪問不同的目錄,這時就要配置多個vhost,具體配置如下,假設(shè)網(wǎng)站根目錄設(shè)定在/var/www/

1、在/var/www/下新建兩個目錄

/var/www/ushark.net
/var/www/ushark.wang

2、編輯/etc/nginx/nginx.conf

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;   #主要是加入此行,如有則忽略
}

3、在/etc/nginx/conf.d下新建兩個conf文件

/etc/nginx/conf.d/ushark.net.conf
/etc/nginx/conf.d/ushark.wang.conf

4、復(fù)制如下配置信息到兩個文件中,只要修改紅色部分內(nèi)容  !!! server_name與root保持一致即目錄和域名一一對應(yīng) !!!

server {
    listen       80;
    server_name   www.ushark.net;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
    root   /var/www/ushark.net/;
    if (!-e $request_filename){   ?。!ewrite可根據(jù)網(wǎng)站需要增刪
            rewrite ^/(.*) /index.php last;  
    }  

    location / {
        index  index.php index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /var/www/ushark.net/;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~* \.php$ {
        fastcgi_index   index.php;
        fastcgi_pass    127.0.0.1:9000;
        include           fastcgi_params;
        fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

5、重啟Nginx

systemctl restart nginx

6、 編輯/etc/hosts  !!! 核心步驟 !!!

[root@bogon ~]# vi 127.0.0.1        localhost.localdomain localhost
::1              localhost6.localdomain6 localhost6
127.0.0.1        www.ushark.net
127.0.0.1        www.ushark.wang

3.因為這里使用的是虛擬機,所以需要配置host文件,注意host配置的應(yīng)該是我們使用的客戶端也就是瀏覽器所在的主機host,這里使用的是windows系統(tǒng)訪問,所以應(yīng)該在windows下配置host,位置:C://Windows/System32/drivers/etc
編輯hosts文件,在文件末尾添加內(nèi)容(虛擬機ip + 需要映射的域名):
192.168.0.110 http://www.silly.com
配置好了host后,打開cmd,執(zhí)行ping命令驗證是否生效。

image.png

  1. 重啟Nginx:/usr/local/nginx/sbin/nginx -s reload
    然后啟動tomcat:${tomcat}/bin/startup.sh
    5.訪問域名:http://www.silly.com/,就可以訪問到tomcat的主頁了。
    image.png

總結(jié):整個過程就是當(dāng)我們訪問域名時,首先根據(jù)host配置映射到了一個IP地址,也就是訪問到了虛擬機,不加端口默認(rèn)訪問的就是80端口,也就是訪問到了虛擬機的Nginx,在Nginx配置中對該域名進(jìn)行了反向代理,代理到了本機的8080端口服務(wù),所以最終就訪問到了虛擬機的Tomcat服務(wù)器。

更新
1,查看指定端口進(jìn)程
netstat -lnp|grep 80 # 查看占用80端口的進(jìn)程信息,主要看PID
ps [pid] #根據(jù)PID查看進(jìn)程的詳細(xì)信息;
kill -9 [PID] # 根據(jù)PID殺死進(jìn)程

2,重新啟動jar包
java -jar spring-boot-base-0.0.2-SNAPSHOT.jar

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容