Ubuntu24安装WordPress

1、更新现有软件

apt update && sudo apt upgrade

2、安装nginx

apt install -y nginx

启用系统重启后自动启动
systemctl enable nginx

启动Nginx
systemctl start nginx

查看Nginx状态
systemctl status nginx

3、防火墙增加http

ufw allow http

4、更改目录属组

chown www-data:www-data /usr/share/nginx/html -R

5、安装 MariaDB 数据库服务器

安装Mysql开源版本客户端和服务端
apt install -y mariadb-server mariadb-client

启用自动启动

systemctl enable mariadb

启动和查看Mysql状态
systemctl start mariadb
systemctl status mariadb

6、安装php

  • Add ondrej's repo:
  • add-apt-repository ppa:ondrej/php
  • apt update
  • apt upgrade

安装php和php扩展包

apt install -y php8.1 php8.1-fpm php8.1-mysql php8.1-curl php8.1-xml php8.1-mbstring php8.1-igbinary php8.1-imagick php8.1-intl php8.1-zip php8.1-soap

开启php系统启动后自动启动

systemctl enable php8.1-fpm

启动php和查看php状态

systemctl start php8.1-fpm

systemctl status php8.1-fpm

7、配置Nginx

删除缺省配置,增加自定义缺省配置

rm /etc/nginx/sites-enabled/default
vi /etc/nginx/conf.d/default.conf

server {
listen 80;
listen [::]:80;
server_name runoffer.gbudu.com;
root /usr/share/nginx/html/;
index index.php index.html index.htm index.nginx-debian.html;

location / {
try_files $uri $uri/ /index.php;
}

location ~ .php$ {
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
include snippets/fastcgi-php.conf;
}

# A long browser cache lifetime can speed up repeat visits to your page
location ~* .(jpg|jpeg|gif|png|webp|svg|woff|woff2|ttf|css|js|ico|xml)$ {
access_log off;
log_not_found off;
expires 360d;
}

# disable access to hidden files
location ~ /.ht {
access_log off;
log_not_found off;
deny all;
}
}

测似配置正确性

nginx -t

重新加载和启动Nginx
systemctl reload nginx
systemctl restart nginx

8、安装证书工具

apt install -y certbot python3-certbot-nginx
systemctl reload nginx
systemctl start nginx

9、下载并安装wordpress

wget https://wordpress.org/latest.zip

解压缩下载的wordpress zip文档

apt install -y unzip
unzip latest.zip -d /usr/share/nginx/html

创建数据库用户

mariadb -u root
create database slimplifylife;
grant all privileges on slimplifylife.* to slimplifylife@localhost identified by 'Hh43420**';
flush privileges;
exit;

chown www-data:www-data /usr/share/nginx/html/wordpress/ -R
cd /usr/share/nginx/html/wordpress/

cp wp-config-sample.php wp-config.php
vi wp-config.php

/** The name of the database for WordPress */
define('DB_NAME', '这里填你自己创建的数据库名称');

/** MySQL database username */
define('DB_USER', '这里是你自己创建的用户名');

/** MySQL database password */
define('DB_PASSWORD', '这里是你自己设置的密码');

设置域名
vi /etc/nginx/conf.d/runoffer.gbudu.com.conf


10、安装证书

apt install snapd
snap install core
snap install --classic certbot
ln -s /snap/bin/certbot /usr/bin/certbot
certbot --nginx
systemctl restart nginx
访问wordpress安装

11、优化

启用 OPCache
默认情况下 PHP 会安装 OPCache,但是不会启用,我们可以通过在 php.ini 中添加如下代码,开启 OPCache。

; 开关打开
opcache.enable=1
; 可用内存酌情而定,单位 megabytes
opcache.memory_consumption=256
; 对多缓存文件限制,命中率不到 100% 的话,可以试着提高这个值
opcache.max_accelerated_files=5000
; Opcache 会在一定时间内去检查文件的修改时间,这里设置检查的时间周期,默认为 2,定位为秒
opcache.revalidate_freq=240
; 是否快速关闭,打开后在 PHP Request Shutdown 的时候回收内存的速度会提高
opcache.fast_shutdown=1
; 不保存文件/函数的注释
opcache.save_comments=0

为 MySQL 开启 Query Cache
通过为 MySQL 开启 Cache,可以加快 WordPress 的查询速度。

在 MySQL 的配置文件 my.cnf 中添加如下代码,并重启 MySQL,即可开启 MySQL Query Cache。

query_cache_type = 1
query_cache_limit = 1M
query_cache_size = 16M
但是,需要注意的是,MySQL Query Cache 并不适合所有场景,如果你的博客浏览量较大、且更新频率不高,可以考虑开启 Query Cache,如果更新频繁且浏览量不大,那么 Query Cache 反而可能带来负效应。

为服务器开启 Gzip 压缩
为程序加入 Gzip 压缩,可以有效减少传输数据的大小。一般来说,我们可以使用如下几种方式开启 Gzip。

cPanel 可以直接在面板的“优化网站”中开启 Gzip。

在 WordPress 中开启 Gzip
可以在根目录下的 index.zip 中添加如下代码,以开启 Gzip 压缩。

// 此代码需要放在 define('WP_USE_THEMES', true); 后,
ob_start('ob_gzhandler');

通过 Nginx 的 Conf 文件开启
在 nginx.conf 中添加如下代码,则可以开启 Gzip 压缩:

gzip on;
gzip_disable "msie6";

gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_min_length 256;
gzip_http_version 1.1;

gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

通过 php.ini 开启
可以通过给 php.ini 中添加如下代码,来开启 gzip 压缩:

zlib.output_compression=On
zlib.output_compression_level = 5

参考:
https://juejin.cn/post/6844903741678698510
https://www.cnblogs.com/54chensongxia/p/12938929.html

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注