hylerrix / university

:mortar_board: my university code & article collection: create & share, thought & works
Creative Commons Attribution Share Alike 4.0 International
45 stars 10 forks source link

[A17]在云上搭建你的个人站点 #27

Open hylerrix opened 7 years ago

hylerrix commented 7 years ago

前注:持续更新中。


SHOW.TIME. 写作背景


前段时间,腾讯云+校园由全国各大高校主动申请过的团队承办,分享课题要求基于腾讯云,“在云上搭建个人站点”和“在云上搭建微信小程序”主题二选一。于是目光转到西邮,由我来当“在云上搭建个人站点”主讲,并于近期写文整理和分享推送。

因此,通过本篇可以


FIRST.BLOOD 认知环节


认知环节

云计算 腾讯云 云 + 校园 云+校园 云端 WP

创建一台主机并登录

云服务器

登陆云主机

购买一个数据库实例并初始化

安装并配置必要的软件


DOUBLE.KILL 腾讯云 UI 界面



TRIPLE.KILL 实操环节



一、安装PHP

yum install php php-common php-cli php-pear php-pdo php-mysqlnd php-pgsql  php-sqlite php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml php-fpm


二、安装 mysql/mariadb

yum install mariadb-devel mariadb-server mysql
  1. 启动mysql/mariadb
systemctl start mariadb
  1. 登录mysql
mysql -u root -p

2.1 创建数据库

CREATE  DATABASE  wordpress;

2.2 创建数据库用户

CREATE  USER   wordpress@localhost;

2.3 设置数据库用户密码

SET  PASSWORD  FOR  wordpress@localhost=PASSWORD("wppassword");

2.4 给数据库用户相应的数据库权限

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

2.5 更新数据库权限表

FLUSH PRIVILEGES;


三、nginx

  1. 安装 nginx
yum install nginx
  1. 启动nginx
systemctl start nginx
  1. 修改 php-fpm 默认用户
vim /etc/php-fpm.d/www.conf
  1. 把其中的 apache 修改为 nginx
chown -R nginx:nginx /var/lib/php
chown -R nginx:nginx /var/www/html 
  1. 启动php-fpm
systemctl start php-fpm

四、WordPress

wget http://download-10012769.cos.myqcloud.com/wordpress-4.5.3-zh_CN.tar.gz
tar -zxvf wordpress-4.5.3-zh_CN.tar.gz -C /var/www/html
cd /var/www/html/wordpress
cp wp-config-sample.php wp-config.php
vim wp-config.php 

四、填写刚才创建的数据库、用户、密码

rm /etc/nginx/nginx.conf
vim /etc/nginx/nginx.conf
user  nginx;
worker_processes  1;

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

pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    access_log    /var/log/nginx/access.log;
    sendfile        on;
    keepalive_timeout  65;

    #php max upload limit cannot be larger than this
    client_max_body_size 13m;
    index              index.php index.html index.htm;

    # Upstream to abstract backend connection(s) for PHP.
    upstream php {
      #this should match value of "listen" directive in php-fpm pool
      #server unix:/tmp/php-fpm.sock;
        server 127.0.0.1:9000;
    }

    #
    #
    include conf.d/*.conf;
}
mkdir /etc/nginx/global
vim /etc/nginx/global/restrictions.conf
# Global restrictions configuration file.
# Designed to be included in any server {} block.</p>
location = /favicon.ico {
  log_not_found off;
  access_log off;
}

location = /robots.txt {
  allow all;
  log_not_found off;
  access_log off;
}

# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
location ~ /\. {
  deny all;
}

# Deny access to any files with a .php extension in the uploads directory
# Works in sub-directory installs and also in multisite network
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
location ~* /(?:uploads|files)/.*\.php$ {
  deny all;
}
vim /etc/nginx/global/wordpress.conf   //commond
# WordPress single blog rules.
# Designed to be included in any server {} block.

# This order might seem weird - this is attempted to match last if rules below fail.
# http://wiki.nginx.org/HttpCoreModule
location / {
  try_files $uri $uri/ /index.php?$args;
}

# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;

# Directives to send expires headers and turn off 404 error logging.
location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
       access_log off; log_not_found off; expires max;
}

# Uncomment one of the lines below for the appropriate caching plugin (if used).
#include global/wordpress-wp-super-cache.conf;
#include global/wordpress-w3-total-cache.conf;

# Pass all .php files onto a php-fpm/php-fcgi server.
location ~ [^/]\.php(/|$) {
  fastcgi_split_path_info ^(.+?\.php)(/.*)$;
  if (!-f $document_root$fastcgi_script_name) {
    return 404;
  }
  # This is a robust solution for path info security issue and works with "cgi.fix_pathinfo = 1" in /etc/php.ini (default)

  include fastcgi.conf;
  fastcgi_index index.php;
#   fastcgi_intercept_errors on;
  fastcgi_pass php;
}
vim /etc/nginx/conf.d/mywordpress.conf
server {
  server_name mywordpress;
  root /var/www/html/wordpress;

  index index.php;

  include global/restrictions.conf;
  include global/wordpress.conf;
}

测试 nginx 配置文件

nginx -t

重新平滑加载 nginx 配置文件

nginx -s reload

nginx 配置

https://segmentfault.com/a/1190000002556269



/etc/php-fpm.d/www.conf

user  nginx;
worker_processes  1;

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

pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    access_log    /var/log/nginx/access.log;
    sendfile        on;
    keepalive_timeout  65;

    #php max upload limit cannot be larger than this
    client_max_body_size 13m;
    index              index.php index.html index.htm;

    # Upstream to abstract backend connection(s) for PHP.
    upstream php {
      #this should match value of "listen" directive in php-fpm pool
      #server unix:/tmp/php-fpm.sock;
        server 127.0.0.1:9000;
    }

    #
    #
    include conf.d/*.conf;
}