一、搭建 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 数据库创建一个新用户 “user@localhost”。
CREATE USER user@localhost;
2.2.4 并为此用户设置密码“wordpresspassword”。
SET PASSWORD FOR user@localhost=PASSWORD("wordpresspassword");
2.2.5 为创建的用户开通数据库 “wordpress” 的完全访问权限。
GRANT ALL PRIVILEGES ON wordpress.* TO user@localhost 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 根据提示填写即可