Ubuntu 22.04 LTS 自己安装WordPress

Ubuntu 22.04 LTS自己安装WordPress笔记

前言: 运行WordPress 以来MySQL数据库和PHP模块,以及nginx( apache也可以,但建议用nginx)Web服务。本文记录在全新Ubuntu 22.04 LTS是上安装和配置好WordPress的相关软件的过程,还是比较麻烦,其中一些没有找到现成的方法,是通过个人摸索总结出来,供个人建站爱好者参考,减少走弯路。

ubuntu下安装mysql

mysql启动非127.0.0.1监听

#安装mysql-server
apt install mysql-server -y

#如要远程连接mysql,例如本地图形管理mysql,
#则需要注释掉/etc/mysql/mysql.conf.d/mysqld.cnf如下2行 
#bind-address           = 127.0.0.1
#mysqlx-bind-address    = 127.0.0.1
#命令方式如下:
sed -i  -e 's/^bind-address/#bind-address/g' /etc/mysql/mysql.conf.d/mysqld.cnf
sed -i  -e 's/^mysqlx-bind-address/#mysqlx-bind-address/g' /etc/mysql/mysql.conf.d/mysqld.cnf

#重启mysql
service mysql restart

#查看mysql是否启动在非127.0.0.1上监听了
netstat -an |grep 3306



更改mysql root密码,缺省安装root无密码,只能本地连接(安全起见,建议不要更改为远程可root登录),可设置新密码,和创建普通账号和创建wordpress用数据库等

#在shell上执行命令
mysql -uroot -p
提示输入密码时直接按回车

登录进入SQL命令界面,输入如下命令:

#更改root密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'xxxxxx';
flush privileges;

#创建wordpress数据库
CREATE DATABASE `wordpress` CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_bin';

#创建wordpress账号
CREATE USER 'wordpress'@'localhost' IDENTIFIED WITH mysql_native_password BY 'yyyyy';
grant all on wordpress.* to wordpress@'localhost';

#创建可远程登录的普通账号
CREATE USER 'dbaUser'@'%' IDENTIFIED WITH mysql_native_password BY '这里输入复杂密码';
grant all on wordpress.* to dbaUser@'%';

ubuntu下安装nginx

相关账号需要在root下安装,如果已登录root,前面的sudo可以去掉,否则前面需要添加sudo。

#刷新 APT 包管理器索引缓存
apt update

#安装nginx
apt install nginx 

#查看nginx安装版本
nginx -v 

#查看nginx服务启动情况
service nginx status  #或
systemctl status nginx

#启动nginx
service nginx start  #或
systemctl start nginx 

#重新启动nginx
systemctl restart nginx 

#停止nginx
systemctl stop nginx

ubuntu下安装php模块支持

apt install php8.1
apt install php8.1-fpm
apt install php8.1-mysql
#如果是用apache安装如下这个
#apt install php libapache2-mod-php

#安装一些相关模块
apt-get install php-curl

#安装性能提升的模块
apt-get install php-gd
apt-get install php-zip
apt-get install php-intl 
apt-get install php-imagick

systemclt restart php8.1-fpm
systemctl status php8.1-fpm

配置php运行端口和允许端口:
vi /etc/php/8.1/fpm/pool.d/www.conf

;下面这个注释掉
;listen = /run/php/php8.1-fpm.sock
;修改:
listen = localhost:81
listen.allowed_clients = 127.0.0.1

ubuntu下安装WordPress

ubuntu下用apt安装的nginx的web目录为/usr/share/nginx/html,只需要将html、php等放到该目录下,就能够访问,php文件通过php cgi模块解析后输出(如果未安装配置好php支持模块,则会直接显示或下载php代码,安装完毕需要验证一下,例如做一个php文件登录http(s)://host:port/test.php等测试一下即可)。

cat test.php

<html>
<head>
  <title>My first PHP website</title>
</head>
<body>
  <?php echo "Hello, world!"; ?>
</body>
</html>

下载: wordpress-6.x.y-zh_CN.zip 或wordpress-6.x.y.zip

https://wordpress.org/latest.zip

https://cn.wordpress.org/latest-zh_CN.zip

将解压后的 wordpress/*的文件复制到如下目录/usr/share/nginx/html

如在ubuntu上操作,参考命令如下:

#例如通过ftp已zip将文件下载到/tmp目录
cd /tmp
unzip /upload/wordpress-6.x.y.zip
cp -rp wordpress/* /usr/share/nginx/html

#将文件所有者更改未nginx运行用户
ps -ef |grep nginx
chown -R www-data:www-data /usr/share/nginx/html

配置nginx配置文件http方式

未启用https, 使用http:

vi /etc/nginx/nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
}

http {
        sendfile on;
        tcp_nopush on;
        types_hash_max_size 2048;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        gzip on;

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;

 server {
        listen       80;
        server_name  www.itnext.top;
        #server_name  106.53.105.153;

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

        location  ^~ /api {
           proxy_pass http://localhost:8080/api; # 此处填写你的后端服务地址
        }

        location ~ \.php{
           include fastcgi_params;
           fastcgi_pass 127.0.0.1:81;  #要与fpm配置一致
           fastcgi_index index.php;
           fastcgi_param SCRIPT_FILENAME /usr/share/nginx/htmlfastcgi_script_name;
        }
    }
}

重启动nginx加载配置文件

nginx -s reload 
#或service nginx restart

检查是否能够访问(如果配置文件中配置为ip,则输入ip访问):

`https://www.itnext.top/

`https://www.itnext.top/wp-admin

配置nginx配置文件https方式

启用https, 需要有https证书密码

证书配置参考:

https配置 :https://help.aliyun.com/document_detail/98728.html?spm=0.2020520163.help.dexternal.63e83711VnUp2Q

免费ssl证书: https://www.aliyun.com/search?scene=all&k=ssl

vi /etc/nginx/nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
}

http {
        sendfile on;
        tcp_nopush on;
        types_hash_max_size 2048;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        gzip on;

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
 server {
   listen 80;
   server_name itnext.top;
   return 301 https://hostrequest_uri;
  }
 server {
     #HTTPS的默认访问端口443。
     #如果未在此处配置HTTPS的默认访问端口,可能会造成Nginx无法启动。
     listen 443 ssl;

     #填写证书绑定的域名
     server_name www.itnext.top;

     #填写证书文件名称
     ssl_certificate /etc/nginx/conf/cert/itnext.top.pem;
     #填写证书私钥文件名称
     ssl_certificate_key /etc/nginx/conf/cert/itnext.top.key;

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

     #默认加密套件
     ssl_ciphers HIGH:!aNULL:!MD5;

     #自定义设置使用的TLS协议的类型以及加密套件(以下为配置示例,请您自行评估是否需要配置)
     #TLS协议版本越高,HTTPS通信的安全性越高,但是相较于低版本TLS协议,高版本TLS协议对浏览器的兼容性较差。
     #ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
     ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;

     #表示优先使用服务端加密套件。默认开启
     ssl_prefer_server_ciphers on;

     location / {
           root html;
           index index.php index.html index.htm;
       try_files uriuri/ /index.php?args;
     }

     location  ^~ /api {
         proxy_pass http://localhost:8080/api; # 此处填写你的后端服务地址
     }

     location ~ \.php {
         include fastcgi_params;
         fastcgi_pass 127.0.0.1:81; #要与fpm配置一致
         fastcgi_index index.php;
         fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
     }
  }
}

重启动nginx加载配置文件

nginx -s reload 
#或service nginx restart

检查是否能够访问(如果配置文件中配置为ip,则输入ip访问):

` https://www.itnext.top/

` https://www.itnext.top/wp-admin

WordPress中设置数据库连接和更改配置

如果已配置好WordPress, 连接地址从IP地址更改为域名,http连接更改为https,注意新配置wordPress中的设置。 地址: /wp-admin/options-general.php

发表评论