机器信息
在云服务器上部署Ubuntu22.04镜像,输入cat /etc/os-release
显示如下信息:
PRETTY_NAME="Ubuntu 22.04.5 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.5 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy
更新软件包
sudo apt-get update
搭建LNMP环境
所谓LNMP就是linux、Nginx、MySQL、php四个wordpress所需要的包,在这里列表如下:
- Nginx 1.18
- MySQL 8.0.39
- PHP 8.1.2
- WordPress 6.6.2
如有需要添加apt源。
nginx安装
sudo apt-get update
sudo apt-get install nginx
nginx -v
此时应显示如下:
nginx version: nginx/1.18
安装MySQL
安装
sudo apt install mysql-server
输入mysql --version
显示如下
mysql Ver 8.0.39-0ubuntu0.22.04.1 for Linux on x86_64 ((Ubuntu))
开机自启动配置
sudo systemctl start mysql
sudo systemctl enable mysql
sudo systemctl daemon-reload
安全性配置
默认密码为空,可以直接运行命令来开始安全配置。
sudo mysql_secure_installation
运行流程如下:
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?
Press y|Y for Yes, any other key for No: Y # 设置密码强度
There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2 # 设置密码强度为强
Skipping password set for root as authentication with auth_socket is used by default.
If you would like to use password authentication instead, this can be done with the "ALTER_USER" command.
See https://dev.mysql.com/doc/refman/8.0/en/alter-user.html#alter-user-password-management for more information.
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 # 去除匿名用户
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y # 只允许root本地登陆
Success.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y # 删除测试数据库
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y # 重新加载权限表
Success.
All done!
修改root用户使用密码登录
mysql
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
PHP安装
sudo apt install php8.1 php8.1-cli php8.1-fpm php8.1-common php8.1-mysql php8.1-gd php8.1-mbstring
输入php -v
显示如下:
PHP 8.1.2-1ubuntu2.19 (cli) (built: Sep 30 2024 16:25:25) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
with Zend OPcache v8.1.2-1ubuntu2.19, Copyright (c), by Zend Technologies
综合配置LNMP
配置nginx支持php
先备份nginx配置文件:
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
修改配置文件/etc/nginx/nginx.conf
中server大括号内的部分,增加如下内容,主要是两个location,让nginx能解析php文件。
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
index index.php index.html index.htm;
}
#添加下列信息,配置Nginx通过fastcgi方式处理您的PHP请求。
location ~ .php$ {
root /usr/share/nginx/html; #将/usr/share/nginx/html替换为您的网站根目录,本文使用/usr/share/nginx/html作为网站根目录。
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
#fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params; #Nginx调用fastcgi接口处理PHP请求。
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
运行如下命令测试启动nginx,并配置开机自启动:
nginx -t
sudo systemctl start nginx
sudo systemctl enable nginx
配置php
检查php8.1-fpm服务是否已经运行:
sudo systemctl status php8.1-fpm.service
配置开机自启动启动服务:
sudo systemctl enable php8.1-fpm.service
在网页目录中(在nginx.conf
配置文件中location ~ .php$
大括号内,root
的参数值)添加一个文件phpinfo.php:
echo "<?php echo phpinfo(); ?>" > /usr/share/nginx/html/phpinfo.php
测试访问LNMP配置信息页面
在浏览器使用域名或者ip访问页面http://<域名或者公网IP>
,如果服务器的80端口正确打开,则可以看到nginx的开始界面。访问http://<域名或者公网IP>/phpinfo.php
则可以看到php的信息。至此LNMP配置完毕。
搭建WORDPRESS
下载解压到nginx目录
安装工具unzip
sudo apt install unzip
下载wordpress并解压
wget https://wordpress.org/wordpress-6.6.2.zip
unzip wordpress-6.6.2.zip
sudo cp -r wordpress /usr/share/nginx/html/.
配置WordPress数据库
输入命令进入MySQL数据库,创建名为wordpress数据库,然后创建新用户user并赋予他管理wordpress的全部权限。
mysql
create database wordpress;
create user 'user'@'localhost' identified by 'Hello123.';
grand all on wordpress.* to 'user'@'localhost';
flush privileges;
配置WordPress配置文件
进入网站的根目录cd /usr/share/nginx/html/wordpress
,备份并修改配置文件:
sudo cp wp-config-sample.php wp-config.php
sudo vim wp-config.php
分别设置为刚才配置MySQL时设置的值:
// ** MySQL 设置 - 具体信息来自您正在使用的主机 ** //
/** WordPress数据库的名称 */
define('DB_NAME', 'wordpress');
/** MySQL数据库用户名 */
define('DB_USER', 'user');
/** MySQL数据库密码 */
define('DB_PASSWORD', 'Hello123.');
/** MySQL主机 */
define('DB_HOST', 'localhost');
测试是否生效
修改nginx配置,将其中的 root 从html
到下一级目录html/wordpress
:
server {
...
root /usr/share/nginx/html/wordpress;
...
location ~ .php$ {
root /usr/share/nginx/html/wordpress;
...
}
}
重启nginx并打开网页
nginx -t && sudo systemctl restart nginx.service
打开http://<域名或者公网IP>
,开始Nginx的设置。