CentOS7: 搭建LNMP环境+WordPress

CentOS7 (CentOS 7.6 64位)
Nginx (Nginx 1.17)
MySQL (Server version 5.7)
PHP (PHP 7.3)

安装Nginx

yum install nginx       # 安装nginx
systemctl start nginx       # 启动nginx
systemctl enable nginx.service      # 设置为开机启动

设置完成后在浏览器输入:127.0.0.1(服务器 公网ip)能看到nginx页面则表示安装成功

安装MySQL

添加MySQL官方源

wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
yum localinstall mysql57-community-release-el7-11.noarch.rpm

以上下载地址如果有变,点击这里查看最新的就行

使用下面的命令查看有哪些版本可选择安装:
yum repolist all | grep mysql

 [root@K ~]#  yum repolist all | grep mysql
 mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community   disabled
 mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community - disabled
 mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community   disabled
 mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community - disabled
 mysql-connectors-community/x86_64  MySQL Connectors Community    enabled:     74
 mysql-connectors-community-source  MySQL Connectors Community -  disabled
 mysql-tools-community/x86_64       MySQL Tools Community         enabled:     74
 mysql-tools-community-source       MySQL Tools Community - Sourc disabled
 mysql-tools-preview/x86_64         MySQL Tools Preview           disabled
 mysql-tools-preview-source         MySQL Tools Preview - Source  disabled
 mysql55-community/x86_64           MySQL 5.5 Community Server    disabled
 mysql55-community-source           MySQL 5.5 Community Server -  disabled
 mysql56-community/x86_64           MySQL 5.6 Community Server    disabled
 mysql56-community-source           MySQL 5.6 Community Server -  disabled
 mysql57-community/x86_64           MySQL 5.7 Community Server    enabled:    307
 mysql57-community-source           MySQL 5.7 Community Server -  disabled
 mysql80-community/x86_64           MySQL 8.0 Community Server    disabled
 mysql80-community-source           MySQL 8.0 Community Server -  disabled

可以看到,除了MySQL 5.7,MySQL官方源也提供别的版本,比如说5.5、5.6、8.0,如果对别的版本有需求,按需安装即可,需用yum-config-manager启用或禁用相关的源(启用需要的版本),比如说禁用MySQL5.6启用MySQL5.7:

yum-config-manager --disable mysql56-community
yum-config-manager --enable mysql57-community

确定需要安装的版本,安装:
yum install mysql-community-server
只需上面一行命令即可安装好,安装过程不需要人工干预,直接就完成了安装过程。

注意:MySQL Server的root密码在安装过程中生成了随机密码,如果安装之后发现不知道MySQL的root密码而无法登录千万别方,可以通过下述命令找到MySQL的root随机密码:

systemctl start mysqld       #启动MySQL

grep 'temporary password' /var/log/mysqld.log    #执行此命令查看随机密码

2019-08-25T04:07:08.536233Z 1 [Note] A temporary password is generated for 
root@localhost: DlKq&l6PMT1X

localhost: 后面的字符串就是MySQL的临时root密码了。

找到密码后进行MySQL初始化操作,执行命令:
mysql_secure_installation

 [root@K ~]#mysql_secure_installation
 
 Securing the MySQL server deployment.
 
 Enter password for user root:          ##输入上面的临时root密码
 
 The existing password for the user account root has expired. Please set a new password.
 
 New password:     ##设置新密码

【这里有一点需要注意的是,由于validate_password的存在,
 所更改的密码不能太简单,必须符合validate_password要求才能更改成功,
 根据官方文档,MySQL密码要求是:
 至少包含一个大写字母,一个小写字母,一个数字,一个特殊符号,且不能少于8位】


 Re-enter new password:  ##重复密码
 The 'validate_password' plugin is installed on the server.
 The subsequent steps will run with the existing configuration
 of the plugin.
 Using existing password for root.

 Estimated strength of the password: 100
 Change the password for root ? ((Press y|Y for Yes, any other key for No) :y #是否更改root密码

  ... skipping.
 By default, a MySQL installation has an anonymous user,
 allowing anyone to log into MySQL without having to have
 a user account created for them. This is intended only for
 testing, and to make the installation go a bit smoother.
 You should remove them before moving into a production
 environment.

 Remove anonymous users? (Press y|Y for Yes, any other key for No) :y ##y是否移除匿名用户

  ... skipping.


 Normally, root should only be allowed to connect from
 'localhost'. This ensures that someone cannot guess at
 the root password from the network.
 
 Disallow root login remotely? (Press y|Y for Yes, any other key for No) :y ##是否禁止root远程登录
 Success.
 
 By default, MySQL comes with a database named 'test' that
 anyone can access. This is also intended only for testing,
 and should be removed before moving into a production
 environment.


 Remove test database and access to it? (Press y|Y for Yes, any other key for No) :y ##是否删除测试数据库
  - Dropping test database...
 Success.

  - Removing privileges on test database...
 Success.

 Reloading the privilege tables will ensure that all changes
 made so far will take effect immediately.

 Reload privilege tables now? (Press y|Y for Yes, any other key for No) :y ##是否立即刷新权限
 Success.

 All done!

如果觉得麻烦,也可以把这个插件关闭,但是不建议:

vi /etc/my.conf

[mysqld]
validate_password=Off

接下来执行:

mysql -u root -p     #用上面设置的密码登陆mysql
mysql>create database wordpress character set utf8mb4 collate utf8mb4_unicode_ci;    # 创建wordpress数据库
mysql>use wordpress;    #切换到wordpress数据库
mysql>exit  # 退出mysql
  • 出于兼容性考虑,我们最好在新建数据库的时候指定字符集,这样在后面的开发中可以减少很多莫名其妙的问题:
    ncreate database if not exists zocodev default character set utf8 collate utf8_unicode_ci;
  • 如果有emoji需求,也可以指定为utf8mb4字符集:
    create database if not exists zocodev default character set utf8mb4 collate utf8mb4_unicode_ci;

安装PHP

安装5.X版本

yum install php-fpm php-mysql   #php-fpm使php与nginx关联,php-mysql为php与mysql关联

安装7.X版本

在CentOS7中,安装PHP7.X可能会报错误
libargon2.so.0()(64bit) 依赖错误

执行:

yum install -y php73-php-fpm php73-php-cli php73-php-bcmath php73-php-gd php73-php-json php73-php-mbstring php73-php-mcrypt php73-php-mysqlnd php73-php-opcache php73-php-pdo php73-php-pecl-crypto php73-php-pecl-mcrypt php73-php-pecl-geoip php73-php-recode php73-php-snmp php73-php-soap php73-php-xmll

报错:

Error: Package: php-cli-7.2.12-1.el7.remi.x86_64 (remi-php72)
           Requires: libargon2.so.0()(64bit)
Error: Package: php-7.2.12-1.el7.remi.x86_64 (remi-php72)
           Requires: libargon2.so.0()(64bit)

解决方法是到 /etc/yum.repos.d 中 将对应的源开启,即把对应的enabled设置为1:
vi /etc/yum.repos.d/epel.repo

打开后 修改 下面节点里的 enabled

[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
#baseurl=http://download.fedoraproject.org/pub/epel//$basearch
mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch
failovermethod=priority
enabled=1     #此处修改为1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

另一处修改:
vi /etc/yum.repos.d/remi-php73.repo

打开后 修改 下面节点里的 enabled

[remi-php73]
name=Remi's PHP 7.3 RPM repository for Enterprise Linux 7 - $basearch
#baseurl=http://rpms.remirepo.net/enterprise/7/php73/$basearch/
#mirrorlist=https://rpms.remirepo.net/enterprise/7/php73/httpsmirror
mirrorlist=http://cdn.remirepo.net/enterprise/7/php73/mirror
enabled=1          #此处修改为1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

保存以上两个修改,重试安装命令:

yum install php73w-fpm php73w-mysql   # php-fpm使php与nginx关联,php-mysql为php与mysql关联
systemctl start php-fpm     # 启动php-fpm
systemctl enable php-fpm    # 设置开机启动

修改nginx配置

打开nginx主配置文件
vi /etc/nginx/nginx.conf
在http内加入:

 server {
     listen       80 default_server;
     listen       [::]:80 default_server;
     server_name  binnear.com.cn; # 此处修改为域名或者公网IP
     root         /usr/share/nginx/html; # 站点的目录
 
     # Load configuration files for the default server block.
     include /etc/nginx/default.d/*.conf;
 
    location / {
        index index.php index.html index.htm;
        try_files $uri $uri/ /index.php?$args;
    }

    rewrite /wp-admin$ $scheme://$host$uri/ permanent;

    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;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

输入完成后,按ESC进入命令模式,输入:wq,回车保存并退出,输入:
nginx -t -c /etc/nginx/nginx.conf
检查配置文件
重载:
systemctl reload nginx

测试php-fpm是否安装成功

输入vi /usr/share/nginx/html/index.php,按i进入编辑模式,输入以下内容:

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

输入完成后,按ESC进入命令模式,输入:wq,回车保存并退出。
接着在浏览器中输入http://当前服务器公网IP/index.php
如果浏览器中出现Hello World!则表示配置成功,可继续进行以下步骤,若出现文件下载弹窗,则配置失败,检查以上步骤是否出错。

安装wordpress与配置wordpress

 wget https://cn.wordpress.org/latest-zh_CN.tar.gz  # 下载wordpress安装包
 tar zxvf latest-zh_CN.tar.gz   # 解压缩
 cd wordpress/   # 进入到wordpress目录
 cp wp-config-sample.php wp-config.php   # 复制wp- config-sample.php并重命名为wp-config.php
 vi wp-config.php   # 打开该文件
 找到mysql设置的配置部分,按i进入编辑模式,将步骤2中配置的mysql信息填入以下内容中
 
 // ** 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', 'root');  # 数据库用户名

 /** MySQL database password */
 define('DB_PASSWORD', '123456');    # 数据库密码

 /** MySQL hostname */
 define('DB_HOST', 'localhost'); # 一般不修改
 输入完成后,按ESC进入命令模式,输入:wq,回车保存并退出;

 rm /usr/share/nginx/html/index.html # 删除nginx中的主页文件
 mv * /usr/share/nginx/html/ # 将wordpress文件移动web站点的根目录

完成后,在浏览器中输入
http://你的主机IP或者域名/wp-admin/install.php
进入到wordpress的配置页面,输入网站标题,用户名和密码后,就可以进入wordpress后台管理界面,到此便大功告成。

后续

  • 关于网站更新需要FTP账户

在 WordPress 目录下找到 wp-config.php 文件并编辑,在最后一行加上
define('FS_METHOD', "direct");

  • 如果这个时候提示无法创建目录,则可输入以下内容:
    chown -Rf apache:root /usr/share/nginx/html/
    刷新便可顺利更新。

关于权限问题,以下摘自WordPress官方手册

完成2个步骤你就可以让wp-content目录下的所有文件可写,但在让每个文件及文件夹可写之前,你应当采取较安全的手段来修改目录。请尝试各命令,如果没用的话,请依次进行尝试,有的甚至可以让外观主题图片文件变成可写。将DIR替换为你希望进行写入操作的文件夹

chmod 746 -v DIR
chmod 747 -v DIR
chmod 756 -v DIR
chmod 757 -v DIR
chmod 764 -v DIR
chmod 765 -v DIR
chmod 766 -v DIR
chmod 767 -v DIR

如果以上这些均不允许你进行写操作,请按次序重试一次,不过这次请将 -v替换为 -R,这将递归式地修改位于文件夹中的各文件。如果完成此操作后仍无法写入的话,就可以尝试777了。
chmod -R 777 /usr/share/nginx/
WordPress:更改文件权限 官方手册

图片分享

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Jo_Francis/article/details/125041068