一、首先需要一台公网服务器(阿里、京东、百度、腾讯),并且是Linux系统,这里的Linux可以是ubuntu、cent os7,ps:有些云厂家有集成环境一键搭建,但是不推荐此种方法
二、安装LAMP或者LNMP环境(L指的是linux系统、A指的是Apache服务、N指的是nginx服务、M指的是mysql数据库、P指的是php服务) ps:这里以ubuntu为例
三、首先更新国内源,文件在/etc/apt/sources.list,用vim打开,更改之前建议备份
sudo vim /etc/apt/sources.list
国内源粘贴进去(这里一般用中科大或者清华大学的)
# deb cdrom:[Ubuntu 16.04 LTS _Xenial Xerus_ - Release amd64 (20160420.1)]/ xenial main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security multiverse
保存并退出,然后用sudo apt-get update
命令更新
四、更新完之后开始安装LNMP环境
1.安装ngnix服务
sudo apt-get install nginx
设置开机自启动,先安装sysv-rc-con
sudo apt-get install sysv-rc-conf
sudo sysv-rc-conf nginx on
如果设置了防火墙,要设置允许nginx通过防火墙,可以通过以下命令查看当前防火墙设置,将Nginx HTTP加入到允许列表
sudo ufw status
将Nginx HTTP加入到允许列表
sudo ufw allow 'Nginx HTTP'
启动nginx
service nginx start
测试 Nginx 服务是否正常运行,在浏览器中,访问云主机公网 IP,查看 Nginx 服务是否正常运行。如果出现Welcome to Nginx 欢迎页面,说明安装配置成功。ps:由于是虚拟机,则输入127.0.0.1
2.安装mysql数据库
sudo apt-get install mysql-server
用于存储和处理我们的数据、内容,安装期间会叫你输入root密码,会输入两遍,输入能记住的就行。
3.安装php
我们现在安装了Nginx提供Web服务,并安装了MySQL来存储和管理我们的数据。但是,我们仍然没有任何可以产生动态内容的东西,PHP可以做到这一点。
由于Nginx不像其他Web服务器那样可以对PHP进行处理,因此我们需要安装php-fpm,“fast CGI进程管理器”,该程序的功能相当于告诉Nginx将PHP请求传递给这个PHP-FPM进行处理。
sudo apt-get install php-fpm php-mysql
打开php的配置文件
sudo vim /etc/php/7.0/fpm/php.ini
命令行模式下,输入“/”,在输入“cgi.fix_pathinfo=1"进行关键字查找,将;cgi.fix_pathinfo=1修改为cgi.fix_pathinfo=0,然后重启php服务。
sudo systemctl restart php7.0-fpm
4.Nginx 与 PHP-FPM集成
我们已经完成了所需软件的安装,现在,需要配置Nginx,使其能够调用PHP处理动态内容。我们在server block层面上执行此操作(server block类似于Apache虚拟主机)。输入以下命令打开Nginx默认配置文件:
sudo vim /etc/nginx/sites-available/default
将以下内容
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name server_domain_or_IP;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php7.0-fpm:
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
修改为
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name server_domain_or_IP;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php7.0-fpm:
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
打开default.conf文件,取消对 IPv6 地址的监听同时配置 Nginx,实现与 PHP 的联动。
sudo vim /etc/nginx/conf.d/default.conf
录入以下内容到 default.conf文件中,保存退出
server {
listen 80;
root /usr/share/nginx/html;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
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;
}
#pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
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;
}
}
通过以下命令测试配置文件是否错在语法错误:
sudo nginx -t
没有错误后重启Nginx服务
sudo systemctl reload nginx
5.测试环境配置
在 Web (/var/www/html)目录下创建info.php文件:
<?php
echo "<title>Test Page</title>";
echo "Hello World!";
?>
在浏览器中,访问该info.php文件,查看环境配置是否成功:页面显示Hello World!表示环境搭建成功。
五、安装wordpress博客
1.下载Wordpress,解压,复制到/var/www/html根目录
curl -O https://wordpress.org/latest.tar.gz
2.配置Wordpress
首先,将文档根目录中的所有文件的所有权分配给我们设置的用户名。这里的用户名是你sudo的用户名:
sudo chown -R sudouser:sudouser /var/www/html
接着,我们将设置Wordpress一些文件夹权限
sudo find /var/www/html -type d -exec chmod g+s {} \;
修改wp-content、themes、plugins三个文件夹的权限
sudo chmod g+w /var/www/html/wp-content
sudo chmod -R g+w /var/www/html/wp-content/themes
sudo chmod -R g+w /var/www/html/wp-content/plugins
3.配置mysql数据库
使用 root 用户登录到 MySQL 服务器。
mysql -uroot -p
为 WordPress 创建 MySQL 数据库 “wordpress”。
CREATE DATABASE wordpress;
为已创建好的 MySQL 数据库创建一个新用户“user@localhost”,by后面是密码
CREATE USER user@localhost IDENTIFIED BY '123456';
为创建的用户开通数据库 “wordpress” 的完全访问权限,by后面是密码。
GRANT ALL PRIVILEGES ON wordpress.* TO user@localhost IDENTIFIED BY '123456';
使用以下命令使所有配置生效
FLUSH PRIVILEGES;
配置完成,quit退出 MySQL。
3.写入数据库信息
完成数据库配置后,还需要将数据库信息写入 WordPress 的配置文件。打开并编辑新创建的配置文件。
sudo vim /var/www/html/wp-config.php
找到文件中 MySQL 的部分,步骤 (2)中已配置好的数据库相关信息写入:
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress / 这个是数据库名字
define(‘DB_NAME’, ‘wordpress’); 这个是数据库用户名 /* MySQL database username
/ define(‘DB_USER’, ‘user’); 这个是数据库密码 /* MySQL database password */ define(‘DB_PASSWORD’, ‘123456’);/** MySQL hostname */ define(‘DB_HOST’, ‘localhost’);
5.安装Wordpress
在 Web 浏览器地址栏输入 WordPress 站点的 IP 地址(云主机的公网 IP 地址,或者该地址后跟 “wordpress文件夹”),可以看到 WordPress 安装屏幕,就可以开始配置 WordPress。