2018 - CentOS 7.5 搭建 WordPress 個人部落格
一、搭建 LNMP 環境( Linux 、Nginx 、MySQL 、PHP)
一、搭建 LNMP 環境( Linux 、Nginx 、MySQL 、PHP)
1.1 安裝 Nginx 、MySQL 、PHP
yum install nginx php php-fpm php-mysql mysql-server -y
1.2 配置 Nginx
1.2.1 請使用 Vim 命令開啟default.conf
檔案,取消對 IPv6 地址的監聽同時配置 Nginx,實現與 PHP 的聯動。
vim /etc/nginx/conf.d/default.conf
1.2.2 按字母“I”鍵或 “Insert” 鍵切換至編輯模式,將已有內容全部清除,複製並貼上以下內容到 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; } }
第二種:
user www www;
worker_processes auto;
error_log /home/wwwlogs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
multi_accept on;
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";
#limit_conn_zone $binary_remote_addr zone=perip:10m;
##If enable limit_conn_zone,add "limit_conn perip 10;" to server section.
server_tokens off;
access_log off;
server
{
listen 80 default_server;
#listen [::]:80 default_server ipv6only=on;
server_name _;
index index.html index.htm index.php;
root /home/wwwroot/default;
#error_page 404 /404.html;
# Deny access to PHP files in specific directory
#location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
include enable-php.conf;
location /nginx_status
{
stub_status on;
access_log off;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /.well-known {
allow all;
}
location ~ /\.
{
deny all;
}
access_log /home/wwwlogs/access.log;
}
include vhost/*.conf;
}
1.2.3 修改完成後,按 “Esc” 鍵,輸入 “:wq”,儲存檔案並返回。
1.2.4 啟動 Nginx
service nginx start
# 啟動 Nginx 出現 Failed to start nginx.service:unit not found
# 錯誤的原因就是沒有新增nginx服務,所以啟動失敗。
# 解決方法:
1. 建立一個檔案
vim /etc/init.d/nginx
2. 插入以下程式碼
#!/bin/sh
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
3. 進入此目錄
cd /etc/init.d
4. 依次執行以下命令
# chmod 755 /etc/init.d/nginx
# chkconfig --add nginx (注意add前面是兩個短橫線-)
5. 啟動 nginx
service nginx start
6. 輸入域名或IP地址
1.3 配置 MySQL
傳送門:https://blog.csdn.net/Hack_Different/article/details/82699854
1.4 配置 PHP
1.4.1 啟動 PHP-FPM 服務。
service php-fpm start
1.4.2 配置 PHP Session 的儲存路徑。
開啟/etc/php.ini
檔案。
vim /etc/php.ini
進入後直接輸入以下內容,回車定位到 “session.save_path” 的位置:
/session.save_path
按字母“i”鍵或 “Insert” 鍵切換至編輯模式,將其改為 :
session.save_path = "/var/lib/php/session"
更改/var/lib/php/session
目錄下所有檔案的屬組都改成 nginx 和 nginx。
chown -R nginx:nginx /var/lib/php/session
1.5 驗證配置環境
1.5.1 請使用以下命令在 Web 目錄下建立index.php
檔案:
vim /usr/share/nginx/html/index.php
1.5.2 按字母“i”鍵或 “Insert” 鍵切換至編輯模式,寫入如下內容:
<?php
echo "<title>Test Page</title>";
echo "Hello World!";
?>
輸入完成後,按“Esc”鍵,輸入 “:wq”,儲存檔案並返回。
1.5.3 在瀏覽器中,訪問該index.php
檔案,檢視環境配置是否成功:
http://雲伺服器例項的公網 IP/index.php
1.5.4 頁面顯示 “Hello World!”,則說明 LNMP 環境配置成功。
二、安裝和配置 WordPress
2.1 下載 WordPress
2.1.1 先刪除網站根目錄下的index.html
檔案。
rm /usr/share/nginx/html/index.html
2.1.2 下載 WordPress 並解壓到當前目錄。此版本為中文版。
wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
tar zxvf wordpress-4.9.4-zh_CN.tar.gz
2.2 配置資料庫
2.2.1 登入 MySQL 伺服器。
mysql -uroot -p
2.2.2 為 WordPress 建立 MySQL 資料庫 “wordpress”。
CREATE DATABASE wordpress;
2.2.3 為已建立好的 MySQL 資料庫建立一個新使用者 “[email protected]”。
CREATE USER [email protected];
2.2.4 併為此使用者設定密碼“wordpresspassword”。
SET PASSWORD FOR [email protected]=PASSWORD("wordpresspassword");
2.2.5 為建立的使用者開通資料庫 “wordpress” 的完全訪問許可權。
GRANT ALL PRIVILEGES ON wordpress.* TO [email protected] IDENTIFIED BY 'wordpresspassword';
2.2.6 使用以下命令使所有配置生效。重新整理許可權。
FLUSH PRIVILEGES;
2.2.7 配置完成,退出 MySQL。
exit
2.3 寫入資料庫資訊
2.3.1 建立新配置檔案,將wp-config-sample.php
檔案複製到名為wp-config.php
的檔案
cd wordpress/
cp wp-config-sample.php wp-config.php
2.3.2 開啟並編輯新建立的配置檔案。
vim wp-config.php
找到檔案中 MySQL 的部分,按字母“I”鍵或 “Insert” 鍵切換至編輯模式,將步驟 3.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”,儲存檔案返回。
2.4 安裝 WordPress
2.4.1 移動安裝檔案至 Web 伺服器文件根目錄,以便可以執行安裝指令碼完成安裝。
mv * /usr/share/nginx/html/
2.4.2 在瀏覽器訪問 WordPress 安裝檔案
http://ip//wordpress//wp-admin/install.php
2.4.3 根據提示填寫即可