CentOS 7.6版本服務器部署JavaWeb應用環境及安裝流程完整版包含(NGINX、PostgreSQL、Java)

CentOS 7.6版本服務器部署JavaWeb部署應用環境及安裝流程包含(NGINX、PostgreSQL、Java),該記錄主要是爲了方便以後查看部署運行環境而記錄。

一、安裝PostgreSQL

1/ 進入PostgreSQL官網下載頁面https://www.postgresql.org/download/

2/選擇Red Hat family Linux (including CentOS/Fedora/Scientific/Oracle variants)版本,選擇適合自己的版本按提示進行相關下載安裝等操作。

3/安裝流程及執行代碼

1、Install the repository RPM:

yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

2、Install the client packages:

yum install postgresql11

3、Optionally install the server packages:

yum install postgresql11-server

4、Optionally initialize the database and enable automatic start:

/usr/pgsql-11/bin/postgresql-11-setup initdb

systemctl enable postgresql-11

systemctl start postgresql-11

這步初始化數據庫命令會在 /var/lib/pgsql 目錄下創建名稱爲11文件夾,11爲數據庫版本,如果安裝的是其他版本,對應的是其版本號(9.4、9.5);這裏如果已經有對應版本的文件夾了,初始化這一步會報錯,需要你自行去刪除對應的文件夾,再去初始化。

5、修改postgres用戶的linux登陸的賬號密碼 

passwd postgres

默認會創建一個名爲postgres的linux登錄用戶,這裏進行密碼修改(注意,這裏修改的是linux登陸的賬號密碼,不是數據庫的)

6、開啓遠程訪問

      打開postgresql.conf文件進行修改,去掉#,localhost改爲*

      修改後按下esc退出插入編輯模式,並輸入:wq保存退出

vim /var/lib/pgsql/11/data/postgresql.conf

    打開pg_hba.conf文件進行修改,127.0.0.1/32改爲0.0.0.0/0,ident改爲md5

    輸入a進入插入編輯模式,對該文件進行編輯。修改後如下,按下esc退出插入編輯模式,並輸入:wq保存退出

vim /var/lib/pgsql/11/data/pg_hba.conf

7、重新啓動服務

systemctl restart postgresql-11

8、切換到postgres賬號,並修改數據庫用戶postgres的密碼

su postgres

psql -U postgres

\password

9、客戶端測試登錄

雲端安全組記得放行端口,不然鏈接不了。

二、安裝NGINX

1/安裝前的環境準備

1、安裝gcc gcc-c++

安裝 nginx 需要先將官網下載的源碼進行編譯,編譯依賴 gcc 環境,如果沒有 gcc 環境,則需要安裝

yum install gcc-c++

2、安裝PCRE pcre-devel庫

PCRE(Perl Compatible Regular Expressions) 是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx 的 http 模塊使用 pcre 來解析正則表達式,所以需要在 linux 上安裝 pcre 庫,pcre-devel 是使用 pcre 開發的一個二次開發庫。nginx也需要此庫。

yum install -y pcre pcre-devel

3、安裝zlib庫

zlib 庫提供了很多種壓縮和解壓縮的方式, nginx 使用 zlib 對 http 包的內容進行 gzip ,所以需要在 Centos 上安裝 zlib 庫。

yum install -y zlib zlib-devel

4、安裝OpenSSL

OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及 SSL 協議,並提供豐富的應用程序供測試或其它目的使用。
nginx 不僅支持 http 協議,還支持 https(即在ssl協議上傳輸http),所以需要在 Centos 安裝 OpenSSL 庫。

yum install -y openssl openssl-devel

2/配置及編譯安裝

1、查看官網(http://nginx.org)最新穩定版NGINX,並使用wget命令下載,把命令中的版本號換成最新的就行。

wget -c https://nginx.org/download/nginx-1.16.1.tar.gz

2、解壓安裝包

tar -zxvf nginx-1.16.1.tar.gz

3、配置NGINX

跳入目錄中

cd nginx-1.16.1

執行配置命令

./configure --user=nobody --group=nobody --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-http_sub_module --with-http_ssl_module

4、編譯安裝

make

make install

5、查找安裝路徑

whereis nginx

6、啓動NGINX

//啓動
/usr/local/nginx/sbin/nginx

//停止
/usr/local/nginx/sbin/nginx -s stop

//重啓
/usr/local/nginx/sbin/nginx -s reload

//測試配置文件是否正常
/usr/local/nginx/sbin/nginx -t

//強制關閉
pkill nginx

//查詢nginx進程
ps aux|grep nginx

3/NGINX配置反向代理

1、打開nginx.conf配置

 vim /usr/local/nginx/conf/nginx.conf

2、找到proxy模塊,去掉#,取消註釋,並改爲自己的HTTP地址。修改後按下esc退出插入編輯模式,並輸入:wq保存退出

3、執行重啓命令,讓配置生效

/usr/local/nginx/sbin/nginx -s reload

4/NGINX配置SSL證書,即HTTPS

1、雲端免費申請一個SSL證書,騰訊雲、阿里雲都可以 。

2、創建cert文件夾,存放證書

mkdir /usr/local/nginx/conf/cert

3、通過FTP工具或者其他遠程連接工具把證書文件上傳至cert目錄中

4、修改 Nginx 根目錄下的 conf/nginx.conf 文件

vim /usr/local/nginx/conf/nginx.conf

附阿里雲、騰訊雲官方NGINX配置SSL證書文檔,具體內容可查看文檔配置

騰訊雲:https://cloud.tencent.com/document/product/400/35244

阿里雲:https://help.aliyun.com/document_detail/98728.html

完整的nginx.conf 文件內容如下


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;

    server {
        listen       80;
        server_name  tnafcs.com;
	    #把http的域名請求轉成https
	    rewrite ^(.*)$ https://$host$1 permanent; 
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /www/static;
            index  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   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        location /api/ {
            proxy_pass   http://127.0.0.1:9999;
        }

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

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


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    server {
	    #SSL 訪問端口號爲 443
        listen       443 ssl;
	    #填寫綁定證書的域名
        server_name  tnafcs.com;
        #啓用 SSL 功能
        ssl on;
	    #證書文件名稱
        ssl_certificate      cert/1_ tnafcs.com_bundle.crt;
	    #私鑰文件名稱
        ssl_certificate_key  cert/2_ tnafcs.com.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
	    #請按照以下協議配置
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
	    #請按照以下套件配置,配置加密套件,寫法遵循 openssl 標準。
        ssl_ciphers  ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_prefer_server_ciphers  on;

        location / {
	    #網站主頁路徑。此路徑僅供參考,具體請您按照實際目錄操作。
            root   /www/static;
            index  index.html index.htm;
        }
	# 配置反向代理,重定向到API接口
	location /api/ {
	    proxy_pass http://127.0.0.1:9999;
	}
    }

}

6、修改完成,重啓 Nginx

/usr/local/nginx/sbin/nginx -s reload

三、安裝Java環境

1、檢測有沒有安裝Java環境

java

2、查看yum源的java包

yum list java*

3、安裝java1.8 jdk版本

yum -y install java-1.8.0-openjdk

4、查看版本,檢測是否安裝成功

java -version

最後,整個基本的JavaWeb項目運行環境就安裝配置完成了,感謝你耐心看完。

 

 


參考文獻:

https://www.jianshu.com/p/b4a759c2208f

https://www.cnblogs.com/jackyzm/p/9600738.html

https://www.cnblogs.com/kaid/p/7640723.html

https://help.aliyun.com/document_detail/98728.html

https://help.aliyun.com/document_detail/98728.html

https://zixuephp.net/article-406.html

發佈了303 篇原創文章 · 獲贊 256 · 訪問量 175萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章