手摸手教你从空壳服务器到部署项目

1,971 阅读3分钟

这是我参与11月更文挑战的第3天,活动详情查看:2021最后一次更文挑战

前言

各位双十一买服务器了吗,我服务器快到期了,续费好贵,所以用新用户薅一个三年的服务器。上次没有记录自己从零到一的过程,这次特此来记录一下

首先,当然得知道自己服务器的远程登陆密码是多少啦。

  1. 购买云服务器
  2. 打开控制台
  3. 进入实例,点击更多=》点击密码/密钥=》点击重置实例密码
  4. 记住服务器的密码(这个很重要)

安装访问服务器的软件

这里我推荐xshellxftp

然后用xshell登陆

linux账户是:root

密码就是刚才重置的密码。

主机号就是阿里云控制台的外网ip

安装git

yum install git

等待安装完成即可

安装nginx 和 br

安装br

  1. cd /usr/local/src
  2. git clone github.com/google/ngx_…
  3. cd ngx_brotli
  4. git submodule update --init
  5. 然后可以愉快的安装nginx啦

安装nginx

  1. 首先安装nginx最新版(例如我装的是1.18.0版本)
  2. 下载解压包放置到/usr/local/src 并cd到该位置
  3. tar -zxvf nginx-1.18.0.tar.gz 解压
  4. cd nginx-1.18.0 进入到nginx目录
  5. ./configure --prefix=/usr/local/src/nginx --with-http_gzip_static_module --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module --add-module=/usr/local/src/ngx_brotli (这里--prefix后面带的是安装目录)

第五步成功的样子是这样的

nginx1.png

  1. make && make install

nginx2.png

这里有一个特别要注意的,假如你装完nginx之后,想装nginx其他的插件,只需要重复4,5,6即可,在第五步把配置写上去

启动nginx

进入你所指定的 --prefix 目录,也就是安装目录,cd sbin

然后输入 ./nginx 启动

tips:启动nginx之后,你nginx.conf server里面的listen 后面写的端口,服务器会自动打开 例如 listen 80 listen 443 重新启动nginx服务器会打开该端口

阿里云打开80端口

点击更多,安全组,入方向,打开80端口即可,想要用https就顺便打开443端口

然后可以把ip复制到浏览器,出现welcome nginx 即代码成功

配置nginx环境变量(全局访问)

  1. 进入/etc/profile
  2. 在文件最后加入一行代码(根据自己的安装目录填写)

export PATH=$PATH:/usr/local/nginx/sbin

  1. source /etc/profile

搞定,现在不必到nginx/sbin下面去执行了

配置开机自启动(配不配置都行)

  1. vim /usr/lib/systemd/system/nginx.service

写入

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

路径是你安装后的目录

设置开机自启动:systemctl enable nginx

关闭开机启动:systemctl disable nginx.service

拓展(看一看就行)

nginx 其他命令

  1. 以特定目录下的配置文件启动:nginx -c /特定目录/nginx.conf
  2. 重新加载配置:nginx -s reload 执行这个命令后,master进程会等待worker进程处理完当前请求,然后根据最新配置重新创建新的worker进程,完成Nginx配置的热更新。
  3. 立即停止服务:nginx -s stop
  4. 从容停止服务:nginx -s quit 执行该命令后,Nginx在完成当前工作任务后再停止。
  5. 检查配置文件是否正确:nginx -t
  6. 检查特定目录的配置文件是否正确:nginx -t -c /特定目录/nginx.conf
  7. 查看版本信息:nginx -v

安装git

安装:yum install -y git 查看版本:git version

安装node

  1. curl --silent --location rpm.nodesource.com/setup_12.x | sudo bash -

  2. yum install -y nodejs

  3. node -v

这里版本不重要,等会安装nvm会安装到最新版

安装nvm

  1. curl -o- raw.githubusercontent.com/nvm-sh/nvm/… | bash

  2. 命令行执行: export NVM_DIR="([z"([ -z "{XDG_CONFIG_HOME-}" ] && printf %s "HOME/.nvm"printf{HOME}/.nvm" || printf %s "{XDG_CONFIG_HOME}/nvm")"[ -s "NVM_DIR/nvm.sh" ] && \. "NVM_DIR/nvm.sh"

  3. source ~/.bashrc

官方文档点这里

输入 nvm -v 查看版本

用nvm安装node最新稳定版本

nvm ls-remote 查看所有可安装的node版本号

找到 有后缀 **(Latest LTS: Gallium)**的

nvm install 该版本即可

然后用 nvm use 切换到该版本

node -v可以看到node版本已被切换

安装mysql

任选一个版本安装即可

安装5.7版本

请看阿里云安装数据库得文档

阿里云的mysql是5.7版本的,如果想装8.x版本的看下面

安装8.0以上版本

  1. wget dev.mysql.com/get/mysql80…

  2. yum install mysql80-community-release-el8-1.noarch.rpm (安装数据源)

  3. yum install mysql-community-server (安装数据库)

  4. service mysqld start (启动mysql)

  5. service mysqld status (查看mysql)状态

  6. grep 'temporary password' /var/log/mysqld.log (显示mysql的随机密码,重要,等会登陆mysql需要,如果多次生成就看最下面一条)

    说明 下一步对MySQL进行安全性配置时,会使用该初始密码。

  7. 运行下列命令对MySQL进行安全性配置。

    mysql_secure_installation
    
    1. 重置root用户的密码。
    Enter password for user root: #输入上一步获取的root用户初始密码
    The 'validate_password' plugin is installed on the server.
    The subsequent steps will run with the existing configuration of the plugin.
    Using existing password for root.
    Estimated strength of the password: 100 
    Change the password for root ? ((Press y|Y for Yes, any other key for No) : Y #是否更改root用户密码,输入Y
    New password: #输入新密码,长度为8至30个字符,必须同时包含大小写英文字母、数字和特殊符号。特殊符号可以是()` ~!@#$%^&*-+=|{}[]:;‘<>,.?/
    Re-enter new password: #再次输入新密码
    Estimated strength of the password: 100 
    Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y #是否继续操作,输入Y
    
    1. 删除匿名用户账号。
    By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment.
    Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y  #是否删除匿名用户,输入Y
    Success.
    
    1. 禁止root账号远程登录。
    Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y #禁止root远程登录,输入Y
    Success.
    
    1. 删除test库以及对test库的访问权限。
    Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y #是否删除test库和对它的访问权限,输入Y
    - Dropping test database...
    Success.
    
    1. 重新加载授权表。
    Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y #是否重新加载授权表,输入Y
    Success.
    All done!
    
  8. 远程访问MySQL数据库

    1. 运行以下命令后,输入root用户的密码登录MySQL。

       mysql -uroot -p
      
    2. 依次运行以下命令创建远程登录MySQL的账号。示例账号为dms、密码为123456

         mysql> create user 'jie'@'%' identified by '你的密码哦哦哦';  #使用root替换jie,可设置为允许root账号远程登录。
         mysql> grant all privileges on *.* to 'jie'@'%' with grant option; # 账户要跟上面一致
         mysql> flush privileges;
      
      

    说明 > 建议您使用非root账号远程登录MySQL数据库。 > 实际创建账号时,需将你的密码哦哦哦6更换为符合要求的密码: 长度为8至30个字符,必须同时包含大小写英文字母、数字和特殊符号。可以使用以下特殊符号:()` ~!@#$%^&*-+=|{}[]:;‘<>,.?/

  9. 去阿里云安全组打开3306端口和防火墙打开3306端口

已经完成了,可以用数据库软件连接了,按我的配置来登陆就是

账户:jie 密码:你的密码哦哦哦

结语

部署项目很简单,只需要修改conf配置文件即可,网上有很多资料,你们可以自行查阅

然后自己配置nginx玩去吧

下面是我记录的东西,你们看看就好

  1. nginx/conf,里面新建 proxy.conf 文件

proxy_temp_path和proxy_cache_path 是nginx的路径

写入如下配置

proxy_temp_path /usr/local/src/nginx/proxy_temp_dir;
proxy_cache_path /usr/local/src/nginx/proxy_cache_dir levels=1:2 keys_zone=cache_one:20m inactive=1d max_size=5g;
client_body_buffer_size 512k;
proxy_connect_timeout 60;
proxy_read_timeout 60;
proxy_send_timeout 60;
proxy_buffer_size 32k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_cache cache_one;

系统操作

  1. cd xx 跟windows一样
  2. ls 查看当前目录下的所有文件

端口相关

开放端口

  1. firewall-cmd --zone=public --add-port=5000/tcp --permanent # 开放5000端口

  2. firewall-cmd --zone=public --remove-port=5000/tcp --permanent #关闭5000端口

  3. firewall-cmd --reload # 配置立即生效

  4. firewall-cmd --zone=public --list-ports 查看防火墙所有开放的端口

  5. systemctl stop firewalld.service 关闭防火墙

  6. firewall-cmd --state 查看防火墙状态

  7. netstat -lnpt (重要) 查看监听的端口

  8. netstat -lnpt |grep 5672 (5672是端口号)检查端口被哪个进程占用

  9. ps 6832 (6832是进程id)查看进程的详细信息

  10. kill -9 6832 中止进程

可能访问页面会出现的403错误

这是因为nginx配置指定的用户不对,要指向你当前的用户,请看下面nginx配置第一行

默认是root

我的nginx命令

  
user  root root;
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 {
    use epoll;
    worker_connections 51200;
    multi_accept on;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    # 设置缓存路径并且使用一块最大100M的共享内存,用于硬盘上的文件索引,包括文件名和请求次数,每个文件在1天内若不活跃(无请求)则从硬盘上淘汰,硬盘缓存最大10G,满了则根据LRU算法自动清除缓存。
    proxy_cache_path /var/cache/nginx/cache levels=1:2 keys_zone=imgcache:100m inactive=1d max_size=10g;

    include proxy.conf;

    # default_type  application/octet-stream;
    
    server_names_hash_bucket_size 512;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 100m;

    sendfile   on;
    tcp_nopush on;

    keepalive_timeout 60;

    tcp_nodelay on;

    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 256k;
    fastcgi_intercept_errors on;

    #开启和关闭gzip模式
    gzip on;
    #gizp压缩起点,文件大于1k才进行压缩
    gzip_min_length  1k;
    # 设置压缩所需要的缓冲区大小,以4k为单位,如果文件为7k则申请2*4k的缓冲区 
    gzip_buffers     4 16k;
    #nginx对于静态文件的处理模块,开启后会寻找以.gz结尾的文件,直接返回,不会占用cpu进行压缩,如果找不到则不进行压缩
    gzip_static on;
    # 识别http协议的版本,早起浏览器可能不支持gzip自解压,用户会看到乱码
    gzip_http_version 1.1;
    # gzip 压缩级别,1-9,数字越大压缩的越好,也越占用CPU时间
    gzip_comp_level 1;
    # 进行压缩的文件类型。
    gzip_types     text/plain application/json application/javascript application/x-javascript text/javascript text/css application/xml image/jpeg image/gif image/png video/mpeg audio/x-pn-realaudio audio/x-midi audio/basic audio/mpeg audio/ogg audio/* video/mp4;
    # 启用应答头"Vary: Accept-Encoding"
    gzip_vary on;
    # nginx做为反向代理时启用,off(关闭所有代理结果的数据的压缩),expired(启用压缩,如果header头中包括"Expires"头信息),no-cache(启用压缩,header头中包含"Cache-Control:no-cache"),no-store(启用压缩,header头中包含"Cache-Control:no-store"),private(启用压缩,header头中包含"Cache-Control:private"),no_last_modefied(启用压缩,header头中不包含"Last-Modified"),no_etag(启用压缩,如果header头中不包含"Etag"头信息),auth(启用压缩,如果header头中包含"Authorization"头信息)
    gzip_proxied   expired no-cache no-store private auth;
    # (IE5.5和IE6 SP1使用msie6参数来禁止gzip压缩 )指定哪些不需要gzip压缩的浏览器(将和User-Agents进行匹配),依赖于PCRE库
    gzip_disable   "MSIE [1-6]\.";

    limit_conn_zone $binary_remote_addr zone=perip:10m;
	limit_conn_zone $server_name zone=perserver:10m;

    server_tokens off;
    access_log off;

    # 是否启用在on-the-fly方式压缩文件,启用后,将会在响应时对文件进行压缩并返回。
    brotli on;
    # 启用后将会检查是否存在带有br扩展的预先压缩过的文件。如果值为always,则总是使用压缩过的文件,而不判断浏览器是否支持。
    brotli_static always;
    # 设置压缩质量等级。取值范围是0到11.
    brotli_comp_level 6;
    # 设置缓冲的数量和大小。大小默认为一个内存页的大小,也就是4k或者8k。
    brotli_buffers 16 8k;
    # 设置需要进行压缩的最小响应大小。
    brotli_min_length 20;
    # 指定对哪些内容编码类型进行压缩。text/html内容总是会被进行压缩
    brotli_types text/plain application/json application/javascript application/x-javascript text/javascript text/css application/xml image/jpeg image/gif image/png video/mpeg audio/x-pn-realaudio audio/x-midi audio/basic audio/mpeg audio/ogg audio/* video/mp4;

    server {
        listen    80;
        listen    443 ssl http2 default_server;
        ssl_certificate     /usr/local/src/nginx/conf/5630007_xxx.pem;
        ssl_certificate_key  /usr/local/src/nginx/conf/5630007_xxx.key;
        root /root/dist;
        location / {
            # root /root/dist;
            index index.htm index.html;
        }
    }
}