项目部署环境搭建

179 阅读2分钟

安装nginx

准备nginx安装环境,安装依赖包

yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

下载并解压安装包

cd /usr/local
mkdir nginx
cd nginx

wget -c wget http://nginx.org/download/nginx-1.21.4.tar.gz


tar -zxvf nginx-1.21.4.tar.gz

cd nginx-1.21.4/

./configure --prefix=/opt/nginx

make

make install

/opt/nginx/sbin

nginx

开机启动

cd /lib/systemd/system

vim nginx.service

[Unit]
Description=nginx service
After=network.target 
   
[Service] 
Type=forking 
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true 
   
[Install] 
WantedBy=multi-user.target

设置开机启动

systemctl enable nginx

如果不想开机自启动了,可以使用下面的命令取消开机自启动 systemctl disable nginx

服务的启动/停止/刷新配置文件/查看状态

systemctl start nginx.service          启动nginx服务

systemctl stop nginx.service           停止服务

systemctl restart nginx.service        重新启动服务

systemctl list-units --type=service     查看所有已启动的服务

systemctl status nginx.service          查看服务当前状态

systemctl enable nginx.service          设置开机自启动

systemctl disable nginx.service         停止开机自启动

配置防火墙

CentOS7使用firewall而不是iptables。所以解决这类问题可以通过添加firewall的端口,使其对我们需要用的端口开放。

1.使用命令  firewall-cmd --state查看防火墙状态。得到结果是running或者not running

2.在running 状态下,向firewall 添加需要开放的端口

命令为 firewall-cmd --permanent --zone=public --add-port=8080/tcp //永久的添加该端口。去掉--permanent则表示临时。

4.firewall-cmd --reload //加载配置,使得修改有效。

5.使用命令 firewall-cmd --permanent --zone=public --list-ports //查看开启的端口,出现8080/tcp这开启正确

6.再次使用外部浏览器访问,这出现tomcat的欢迎界面。

补充:

开启防火墙的命令

         systemctl start firewalld.service

关闭防火墙的命令

        systemctl stop firewalld.service

开机自动启动

        systemctl enable firewalld.service

关闭开机自动启动

        systemctl disable firewalld.service

查看防火墙状态

        systemctl status firewalld下列显示表示没有问题

nginx 配置: 更多配置待学习


#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       8080;  // 访问端口
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            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 ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # 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 {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

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

}