安装编译环境和一些必要工具
1.1、例行更新源
apt-get update -y apt-get upgrade -y
1.2安装环境
apt-get install build-essential apt-transport-https software-properties-common lsb-release ca-certificates dirmngr
Nginx安装
2.1、获取Nginx包密钥(Key)
wget https://nginx.org/keys/nginx_signing.key apt-key add nginx_signing.key
2.2、添加Nginx官方源
echo -e "deb https://nginx.org/packages/mainline/debian/ $(lsb_release -sc) nginx\ndeb-src https://nginx.org/packages/mainline/debian/ $(lsb_release -sc) nginx" | tee /etc/apt/sources.list.d/nginx.list apt-get update
2.3、安装Nginx
apt-get remove nginx-common apt-get install nginx
运行Nginx并检测
systemctl start nginx curl -I 127.0.0.1
3.1显示类似如下信息则表示Nginx正常运行:
HTTP/1.1 200 OK Server: nginx/1.13.12 Date: Thu, 31 May 2018 16:39:32 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Mon, 09 Apr 2018 16:01:09 GMT Connection: keep-alive ETag: "5acb8e45-264" Accept-Ranges: bytes
浏览器检测
4.1、打开浏览器,输入 VPS Server 的IP,将会看到这样的页面:
这就说明Nginx安装成功啦。接下来安装php。
PHP安装
5.1添加PHP源
wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | tee /etc/apt/sources.list.d/php.list apt-get update
5.2、安装PHP以及部分库
apt-get install php7.2
注:php版本不一定为7.2,可按自己喜好即可,本文章以php7.2为例
安装完后会显示运行 Apache 服务失败,这是正常情况,因为我们先安装并运行了Nginx,Nginx占用了80端口,导致 Apache 服务运行失败。
5.3安装拓展库
apt-get install php7.2-fpm php7.2-cgi php7.2-curl php7.2-gd php7.2-xml php7.2-xmlrpc php7.2-mysql php7.2-bz2
该步骤不一定需要,但是部分WP插件可以需要拓展库,建议最好一起安装
安装MariaDB
apt-get install software-properties-common dirmngr apt-key adv --fetch-keys 'https://mariadb.org/mariadb_release_signing_key.asc' add-apt-repository 'deb [arch=amd64,arm64,ppc64el] http://ftp.hosteurope.de/mirror/mariadb.org/repo/10.5/debian buster main' apt-get update
这里由于本人使用的母鸡在德国,故使用的源地址为德国源,修改第三行为自己喜欢的源即可,源可以在MaraiaDB官网获取。
6.3、安装MariaDB
apt-get install mariadb-server mariadb-client
配置Nginx与PHP
7.1、修改PHP参数
打开 php-fpm配置文件 /etc/php/7.2/fpm/php.ini
vim /etc/php/7.2/fpm/php.ini
找到 cgi.fix_pathinfo 参数,改为:
cgi.fix_pathinfo=0
Vim编辑模式下可以直接查找到该参数
/cgi.fix_pathinfo
7.2、查看PHP监听方式
vim /etc/php/7.2/fpm/pool.d/www.conf
打开 www.conf 配置文件,找到 user 和 listen
可以看到 PHP 的用户是 www-data,请记住这个用户名。 注意啦这里监听的是一个文件,小伙伴都知道,在 Unix/Linux 里“一切皆文件”,所以在这里监听一个文件是完全正确并OK的,因为在 Unix/Linux 里本地 Socket 的本质就是文件。 不过我们要记住该文件路径,接下来就要用到它。
7.3、修改Nginx配置文件
vim /etc/nginx/nginx.conf
user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; 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; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; }
这里可以看到第一行的 user 为 nginx,跟 php 不一致,我们把它改成与 php 一致。 改为 www-data 。 除此之外,我们也可以把 gzip 功能打开,把注释符号(#)去掉即可。视自身情况按需开启,该功能要求php gzip 扩展库。 修改好后如下:
user www-data; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; 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; keepalive_timeout 65; gzip on; include /etc/nginx/conf.d/*.conf; }
最后我们可以看到最后一行:
include /etc/nginx/conf.d/*.conf;
这说明 conf.d 文件夹里还有其他配置文件。
vim /etc/nginx/conf.d/default.conf
server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
根据注释信息,我们把该去掉的注释都去掉,并修改 root 路径,以及修改 fpm 的本地端口监听为本地文件监听,即之前在 PHP 配置文件看到的 listen 的文件路径。
server { listen 80; server_name localhost; root /home/wordpress; #charset koi8-r; access_log /var/log/nginx/host.access.log main; location / { # root /usr/share/nginx/html; index index.php index.html index.htm; } error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { # root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { # root html; fastcgi_pass unix:/run/php/php7.2-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # location ~ /\.ht { deny all; } }
注意仔细比较,以免遗漏或改错,当然也可以touch新文件,直接复制粘贴的写好出来的进去
最后输入 Nginx 配置文件语法检查命令来检查语法是否正确:
nginx -t
如果正确无误的话会显示:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successfulz
最后再重启下Nginx即可
systemctl restart nginx
8. 检测Nginx和PHP配置
可以看到,在配置文件中,我将 root 路径设置为 /home/wordpress 。因此,创建该文件夹,添加 PHP 测试文件,并修改用户与用户组为 www-data 。
cd /home mkdir wordpress echo -e "<?php\nphpinfo();\n?>" | tee wordpress/index.php chown -R www-data.www-data wordpress/
打开浏览器,输入主机IP地址,如果页面显示 PHP 信息,则说明配置成功啦。如果提示了各种错误的话,请仔细检查配置文件哦。
9、配置MariaDB
9.1 MariaDB初始化
执行以下命令进行安全配置初始化:
mysql_secure_installation
输入 MariaDB 的 root 用户的密码后,根据提示信息进行配置即可
10、为WordPress建立一个数据库
10.1、登陆MariaDB
mysql -u root -p
输入密码,登陆。
10.2、创建数据库
创建一个用户:
create user '用户名'@'localhost' identified by '密码';
修改命令中用户名和密码为你喜欢的"英文单词"
创建一个数据库:
create database db_wordpress default charset utf8 collate utf8_general_ci;
db_wordpress 可以改为你喜欢的库名,utf8 和 utf8_general_ci 是为了设置该数据库使用的字符集。
给 user_wordpress 用户添加库 db_wordpress 的操作权限:
grant all privileges on db_wordpress.* to 'user_wordpress'@'localhost' identified by '密码'; flush privileges;
注意,请记住用户名、密码、以及库名,在配置 WordPress 的时候需要填写。
退出数据库,MariaDB配置完成
exit
10、安装WordPress并配置
10.1、安装WordPress
删除掉之前为了检验Nginx和PHP配置创建的 wordpress 文件夹:
rm -R wordpress/
下载并解压 WordPress 包:
wget http://wordpress.org/latest.tar.gz tar -zxvf latest.tar.gz
10.2、配置WordPress
复制配置文件:
cp wordpress/wp-config-sample.php wordpress/wp-config.php
打开配置文件并修改:
vim wordpress/wp-config.php
根据提示以及创建数据库时使用的信息,修改红框中的内容,从上往下依次是 数据库名、数据库用户、数据库用户密码、字符集:
保存修改,最后一步,修改 wordpress 文件夹以及全部子文件子文件夹的用户:
chown -R www-data.www-data wordpress/
至此全部后台操作已经完成,出了一口长气,还是挺复杂的。
好了,是时候开始我们的表演,打开浏览器,输入IP