本文最后更新于:2021年9月19日 晚上
前言 此文章详细记录了 WEB 服务器应用部署的全过程 包括 nginx 、php 、mysql 、curl 、openssl 等各种组件的部署方法。
一、服务器本地部署 Debian 系统安装好后开始配置 SSH
配置源: 打开配置文件
1 nano /etc/ apt/sources.list
注释掉所有源地址添加163源地址
1 deb http://mi rrors.163 .com/debian/ stretch main non-free contrib
更新源
安装配置 ssh: 安装 ssh
打开 ssh 配置文件 配置允许 root 远程登录 ssh
1 nano /etc/ ssh/sshd_config
在 Authentication 部分注释掉 PermitRootLogin without-password 并添加以下内容
重新启动 ssh 服务
配置网络: 安装 ipconfig
1 apt-get install net-tools
查看网络信息
修改配置文件配置静态 IP
1 nano /etc/ network/interfaces
配置 IP、子网掩码、网关
1 2 3 4 5 6 allow -hotplug enp3 s0 iface enp3 s0 inet staticaddress 192.168.1.99 netmask 255.255.255.0 gateway 192.168.1.254
重启服务器
二、ssh 远程部署 安装依赖: 配置源
1 nano /etc/ apt/sources.list
添加以下源地址
1 2 3 4 5 6 7 8 deb http://mi rrors.163 .com/debian/ stretch main non-free contrib deb http://mi rrors.163 .com/debian/ stretch-updates main non-free contrib deb http://mi rrors.163 .com/debian/ stretch-backports main non-free contrib deb-src http://mi rrors.163 .com/debian/ stretch main non-free contrib deb-src http://mi rrors.163 .com/debian/ stretch-updates main non-free contrib deb-src http://mi rrors.163 .com/debian/ stretch-backports main non-free contrib deb http://mi rrors.163 .com/debian-security/ stretch/updates main non-free contrib deb-src http://mi rrors.163 .com/debian-security/ stretch/updates main non-free contrib
更新源
安装各种包
1 apt-get install build-essential g++ make libpcre3 libpcre3-dev sudo git zip libxml2-dev zlib1g-dev php-gd libpng-dev libwebp-dev libjpeg-dev php-intl libgeoip-dev libfreetype6-dev libxslt1-dev
安装 Mysql: 下载地址
1 https:// downloads.mysql.com/archives/ community/
安装依赖
1 apt install libaio1 libnuma1 libmecab2
解压 Mysql
1 tar -xvf mysql-server_5 .7 .34 -1 debian9 _amd64 .deb-bundle.tar
按照顺序依次执行下面的命令进行安装
1 dpkg -i mysql-common_5 .7 .34 -1 debian9 _amd64 .deb
1 dpkg -i mysql-community-client_5 .7 .34 -1 debian9 _amd64 .deb
1 dpkg -i mysql-client_5 .7 .34 -1 debian9 _amd64 .deb
执行下面命令后会提示设置 root 密码
1 dpkg -i mysql-community-server_5 .7 .34 -1 debian9 _amd64 .deb
1 dpkg -i mysql-server_5 .7 .34 -1 debian9 _amd64 .deb
配置远程可以连接 mysql: 打开配置文件
1 nano /etc/my sql/mysql.conf.d/my sqld.cnf
注释掉以下配置信息,该配置信息表示只允许本地访问
进入 mysql
创建 root 用户并设置密码,如果报错 ERROR 1396 (HY000): Operation CREATE USER failed for 'root'@'%'
,说明已经存在该用户对应主机,直接下一步授权
1 create user 'root' @'%' identified by '密码' ;
授权 root 用户最大权限
1 grant all privileges on *.* to 'root' @'%' identified by '密码' ;
刷新权限
Mysql 常用命令: 启动 mysql
重启 mysql
停止 mysql
查看 mysql 运行状态
进入 mysql
显示所有数据库列表
创建一个名为 blog 的数据库
删除 blog 数据库
编译安装 openssl: 下载地址
1 https:// www.openssl.org/source/
解压 openssl
1 tar -xvf openssl-1 .1 .1 .tar.gz
编译安装
编译安装 curl: 下载地址
1 https:// curl.se/download.html
解压
编译安装
1 ./configure --with-amissl --with-bearssl --with-gnutls --with-mbedtls --with-mesalink --with-nss --with-openssl --with-schannel --with-secure-transport --with-wolfssl
编译安装 PHP 7.4: 下载地址
1 https:// www.php.net/downloads
安装依赖包
1 apt-get install libsqlite3-dev libcurl4-openssl-dev libonig-dev libpng-dev pkg-config libcurl4-gnutls-dev
解压
1 tar -xvf php-7 .4 .23 .tar.gz
编译安装
1 ./configure --enable-fpm --prefix=/usr/local/php --sysconfdir=/usr/local/php/etc --with-config-file-path=/usr/local/php/etc --with-config-file-scan-dir=/usr/local/php/etc/php .d --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype --with-jpeg --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-gd --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --with-zip --with-bz2 --enable-soap --without-pear --enable-opcache --with-webp --with-zlib
进入 php 安装包目录复制 php.ini-production
到 /usr/local/php/etc/
目录并重命名为php.ini
1 cp php.ini-production /usr/ local/php/ etc/php.ini
进入 /usr/local/php/etc/
目录复制 php-fpm.conf.default
重命名为 php-fpm.conf
1 cp php-fpm.conf .default php-fpm.conf
修改 php-fpm.conf
文件约125行内容为 include=/usr/local/php/etc/php-fpm.d/www.conf.default
1 nano /usr/ local/php/ etc/php-fpm.conf
修改 /usr/local/php/etc/php-fpm.d/www.conf.default
文件23和24行
1 nano /usr/ local/php/ etc/php-fpm.d/ www.conf.default
1 2 user = chziyue group = chziyue
编译安装 nginx: 下载地址
1 http:// nginx.org/en/ download.html
解压 nginx
解压 ngx_brotli 放到 nginx 同级目录下 使 nginx 支持 br 压缩格式,如不需要 br 也可省略此步骤,在编译 nginx 时去掉最后的 --add-module=../ngx_brotli
编译安装 nginx
1 ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-openssl= ../openssl --with-openssl-opt=enable-tls1_3 --with-openssl-opt= 'enable-tls1_3 enable-weak-ssl-ciphers' --add-module= ../ngx_brotli
进入 /usr/local/nginx/conf
目录
1 cd /usr/ local/nginx/ conf
打开 nginx 配置文件
server 里添加 php 配置信息
1 2 3 4 5 6 7 8 location ~ \.php$ { client_max_body_size 2M ; client_body_temp_path /tmp; fastcgi_pass 127.0.0.1:9000 ; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
http 里添加 gzip 和 br 配置信息 没有安装 br 可以去掉 br 配置信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 sendfile on ; tcp_nopush on ; tcp_nodelay on ; keepalive_timeout 60 ; gzip on ; gzip_vary on ; gzip_comp_level 6 ; gzip_buffers 16 8 k; gzip_min_length 1024 ; gzip_proxied any ; gzip_disable "msie6"; gzip_http_version 1.0 ; gzip_types text /plain text /css application/json application/x-javascript text /xml application/xml application/xml +rss text /javascript application/javascript image/svg+xml ; brotli on ; brotli_comp_level 6 ; brotli_buffers 16 8 k; brotli_min_length 20 ; brotli_types text /plain text /css application/json application/x-javascript text /xml application/xml application/xml +rss text /javascript application/javascript image/svg+xml ;
配置环境变量: 定位到~
编辑.bashrc
在最下面添加以下信息
1 2 3 4 PATH=$PATH :/usr/local/php/sbin export PATH PATH=$PATH :/usr/local/nginx/sbin export PATH
保存退出后刷新
现在在任何目录下都可以使用下面的命令来启动 nginx 和 php 启动 nginx
重启 nginx
启动 php
浏览器访问服务器 ip 地址即可打开主页