上周更新 php7.3 的時候順手執行了下 yum update 導致無法進入系統,在客服的幫助下複製出了數據,然後自己重裝了系統,記錄下。本文主要參考了G. T. Wang的CentOS 7 安裝 Nginx、MySQL/MariaDB、PHP7,架設 LEMP 網頁伺服器筆記,非常感謝!
安全設置
阻止內核升級
#在 /etc/yum.conf 中加入 exclude=kernel*
上次進不了系統比較懷疑是升級內核後grub沒更新導致的,但是也不確定,為了系統穩定性最好還是禁用內核升級。不過禁止了內核升級也就不能安裝 kernel 相關庫,需要的時候臨時打開就行。
新建 sudo 用戶
# 新增使用者 adduser USERNAME # 設定密碼 passwd USERNAME # 將 USERNAME 加入 wheel 群組 usermod -aG wheel USERNAME
設定時區
# 使用 timedatectl 列出可選擇的時區: timedatectl list-timezones # 設定時區為亞洲的台北: sudo timedatectl set-timezone Asia/Shanghai # 其他常用時區 sudo timedatectl set-timezone Asia/Hong_Kong sudo timedatectl set-timezone Asia/Taipei # 查看本機時間 date
防火牆
參考:CentOS Linux 7 以 firewalld 指令設定防火牆規則教學。我這裡只記錄下我的操作。
# 檢查 firewalld 服務狀態 systemctl status firewalld #啟動 firewalld 服務,默認是有開啟22端口的, #如果你的 SSH 端口已經更改,啟用防火牆可能會導致 SSH 連不上 systemctl start firewalld # 停止 firewalld 服務 systemctl stop firewalld # 重新啟動 firewalld 服務 service firewalld restart # 設定開機自動啟動 firewalld 服務 systemctl enable firewalld # 列出預設區域 firewall-cmd --get-default-zone #一般是public # 將 http 服務新增至 public 區域中 sudo firewall-cmd --zone=public --add-service=http # 永久將 http https 服務新增至 public 區域中 sudo firewall-cmd --zone=public --permanent --add-service=http sudo firewall-cmd --zone=public --permanent --add-service=https # 開啟 tcp 的 8080 連接埠 sudo firewall-cmd --zone=public --add-port=8080/tcp # 永久開啟 tcp 的 8080 連接埠 sudo firewall-cmd --zone=public --permanent --add-port=8080/tcp # 若欲將 ssh 端口改為 20222 firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT_direct 0 -p tcp --dport 20222 -m state --state NEW -m recent --set # 增加30秒4次的登錄頻率限制 firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT_direct 1 -p tcp --dport 20222 -m state --state NEW -m recent --update --seconds 30 --hitcount 4 -j REJECT --reject-with tcp-reset # 列出 public 區域永久的服務設定值 sudo firewall-cmd --zone=public --permanent --list-services # 將 http 服務從 public 區域中移除 sudo firewall-cmd --zone=public --remove-service=http # 永久將 http 服務從 public 區域中移除 sudo firewall-cmd --zone=public --permanent --remove-service=http
SSH加固
#如果本機沒有~/.ssh/id_rsa.pub #則應該先生成密鑰對 mkdir -p ~/.ssh chmod 700 ~/.ssh ssh-keygen #問題默認即可
#複製本機 id_rsa.pub 到服務器 ssh-copy-id -i ~/.ssh/id_rsa.pub USER@HOST #然後嘗試直接密鑰登錄 ssh USER@HOST
#修改 sshd 配置文件 /etc/ssh/sshd_config #關閉密碼登錄 PasswordAuthentication no #如果已新建用戶,可以禁止 root 遠程登錄 PermitRootLogin no #避免無操作時自動斷開 ClientAliveInterval 20 ClientAliveCountMax 30 #修改端口,注意防火牆應開啟對應端口 Port 42222 #重啟 sshd 服務 sudo systemctl restart sshd
安裝LNMP服務
更新系統套件
#更新下系統套件 sudo yum upgrade #啟用 EPEL sudo yum install epel-release sudo yum update
安裝 Nginx
sudo yum install nginx #啟動 Nginx 服務 sudo systemctl start nginx #開機自動啟動 Nginx 服務 sudo systemctl enable nginx
安裝 MariaDB
yum install mariadb-server mariadb #啟動 MariaDB 服務,並設定開機自動啟動 sudo systemctl start mariadb sudo systemctl enable mariadb #強化 MySQL/MariaDB 資料庫設定的安全性 sudo mysql_secure_installation #修改 /etc/my.cnf 綁定本機 IP [mysqld] bind-address = 127.0.0.1
新建數據庫用戶和導入備份的數據可以參考mysql tricks。
安裝 PHP 7.3
#開啟 Remi repository sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm #安裝 yum-utils sudo yum install yum-utils #開啟 php 7.3的軟件源 sudo yum-config-manager --enable remi-php73 #或者開啟其他版本 php 的軟件源 sudo yum-config-manager --enable remi-php72 sudo yum-config-manager --enable remi-php71 #安裝常用的 php 庫 sudo yum install php php-mcrypt php-cli php-fpm php-gd php-curl php-mysql php-ldap php-zip php-fileinfo php-xml php-mbstring #修改 /etc/php-fpm.d/www.conf listen = 127.0.0.1:9000 user = nginx group = nginx #修改 /etc/php.ini cgi.fix_pathinfo=0 upload_max_filesize = 32M post_max_size = 32M max_file_uploads = 50 #啟動 php-fpm 服務,並設定開機自動啟動 sudo systemctl start php-fpm sudo systemctl enable php-fpm
我的服務器只有1核 CPU 和 512 MB內存,默認的php配置會耗光服務器資源,可以通過修改下面這些值來使服務器穩定。如果有更好的配置方案歡迎留言。
#修改 /etc/php-fpm.d/www.conf listen.allowed_clients = 127.0.0.1 pm = dynamic # Total number of processes allowed pm.max_children = 5 # The number of child processes created on startup pm.start_servers = 2 # The minimum number of idle processes pm.min_spare_servers = 1 # The maximum number of idle processes pm.max_spare_servers = 2 # The number of seconds an idle process will be alive pm.process_idle_timeout = 10 # The execution time of each child process and is used to curb memory leaks pm.max_requests = 200
配置 nginx
#修改 /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    client_max_body_size 40M;
    gzip on;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;
}
#新增 /etc/nginx/nginx.d/ft.wupo.info.conf
    server {
        listen      443 ssl;
        listen      [::]:443;
        server_name ft.wupo.info ;
        root        /usr/share/nginx/html/ft.wupo.info;
        index       index.php index.html index.htm;
        ssl         on;
        ssl_certificate      /etc/letsencrypt/live/ft.wupo.info/fullchain.pem;
        ssl_certificate_key  /etc/letsencrypt/live/ft.wupo.info/privkey.pem;
        location ~ \.php$ {
    	    fastcgi_pass   127.0.0.1:9000;
    	    fastcgi_index  index.php;
    	    fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
    	    include        fastcgi_params;
	}
        location / {
                try_files $uri $uri/ /index.php?$args;
        }
    }
	server {
		listen 80;
		server_name ft.wupo.info;
		return 301 https://$server_name$request_uri;
	}
#https://certbot.eff.org/lets-encrypt/centosrhel7-nginx #啟用 certbot 的源 yum-config-manager --enable rhui-REGION-rhel-server-extras rhui-REGION-rhel-server-optional #安裝 certbot sudo yum install certbot python2-certbot-nginx #生成證書 certbot -d ft.wupo.info --nginx certonly
如果是首次獲取生成證書,nginx 應該會報錯,因為證書找不到。可以先只打開 80 端口,待證書獲取成功後再打開 443。
本文更新於 2025/01/28。