Ubuntu+Nginx+php搭建一个属于自己的Wordpress站点

1、搭建环境

本文笔者使用的服务器是腾讯云服务器。

Linux:Linux 系统(本文为 Ubuntu18.04);
Nginx:Web 服务器程序,用来解析 Web 程序;
MySQL:一个数据库管理系统;
PHP:Web 服务器生成网页的程序。

(1)安装配置Nginx

sudo apt-get install nginx

启动 Nginx

service nginx start

测试 Nginx 服务是否正常运行,在浏览器中,访问云主机公网 IP,查看 Nginx 服务是否正常运行。如果出现Welcome to Nginx 欢迎页面,说明安装配置成功。

(2)安装MySQL

sudo apt-get install mysql-server

修改密码

use mysql;

update mysql.user set authentication_string=password('root') 
where user='root' and Host ='localhost';

update user set plugin="mysql_native_password"; 

flush privileges;

quit;

重新启动mysql

sudo service mysql restart

mysql -u root -p // 启动后输入已经修改好的密码:root

(3)安装PHP

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.2-fpm

2、配置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

3、测试环境

在 Web 目录下创建info.php文件:

sudo vim /var/www/html/info.php

输入以下内容:

<?php
echo "<title>Test Page</title>";
echo "Hello World!";
?>

在浏览器中,访问该info.php文件,查看环境配置是否成功:页面显示Hello World!表示环境搭建成功。

4、安装Wordpress

下载WordPress

cd /tmp
curl -O https://wordpress.org/latest.tar.gz

笔者使用的是地址是 https://cn.wordpress.org/latest-zh_CN.zip

解压

tar xzvf latest.tar.gz

笔者在解压是遇到tar无法解压的问题,采用了本地下载wordpress解压后使用Transmit上传至云服务器目录下,如果非macOS可以使用Xftp去上传文件。

复制WordPress到网站根目录:

#备份配置文件
cp /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php

#创建upgrade目录,这样在执行自动更新时,WordPress不会遇到权限问题:
mkdir /tmp/wordpress/wp-content/upgrade

#目录的全部内容复制到网站根目录中。使用-a标志来确保我们的权限得到保持。
目录的末尾使用了一个点来表示目录中的所有内容都应该被复制,包括任何隐藏的文件
sudo cp -a /tmp/wordpress/. /var/www/html

调整WordPress的所有权和权限

设置合理的文件权限和所有权。我们需要能够以普通用户的身份写入某些文件,并且我们需要Web服务器也能够访问和调整某些文件和目录。

首先,将文档根目录中的所有文件的所有权分配给我们设置的用户名。这里的用户名是你sudo的用户名:

sudo chown -R sudouser:sudouser /var/www/html

接着,我们将在文档根目录下的每个目录上设置setgid位。

这会导致在这些目录中创建的新文件继承父目录(我们只设置为sudouser)的组而不是创建用户的主组。

这只是确保当我们通过命令行在目录中创建一个文件时,Web服务器仍然拥有组的所有权。

我们可以在WordPress安装的每个目录上设置setgid位,方法是输入:

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

配置数据库

使用 root 用户登录到 MySQL 服务器。

mysql -uroot -p

为 WordPress 创建 MySQL 数据库 “wordpress”。

CREATE DATABASE wordpress;

为已创建好的 MySQL 数据库创建一个新用户 “user@localhost”。

CREATE USER user@localhost IDENTIFIED BY '123456';

为创建的用户开通数据库 “wordpress” 的完全访问权限。

GRANT ALL PRIVILEGES ON wordpress.* TO user@localhost IDENTIFIED BY 'wordpresspassword';

使用以下命令使所有配置生效。

FLUSH PRIVILEGES;

配置完成,退出 MySQL。

exit

写入数据库信息

完成数据库配置后,还需要将数据库信息写入 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', 'wordpresspassword');

/** MySQL hostname */
define('DB_HOST', 'localhost');

修改完成后,按“Esc”键,输入“:wq”,保存文件返回。

安装WordPress

在 Web 浏览器地址栏输入 WordPress 站点的 IP 地址(云主机的公网 IP 地址,或者该地址后跟 “wordpress文件夹”),可以看到 WordPress 安装屏幕,就可以开始配置 WordPress。

 

至此,wordpress站点搭建完毕。

 

个人站点:后续绑定域名后会发出来。

 

参考:

1.https://www.jianshu.com/p/a1ede27ad9b9

2.https://blog.csdn.net/qq_38737992/article/details/81090373

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值