CentOS7: Build LNMP environment + WordPress

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

Install nginx

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

After the setting is completed, enter in the browser: 127.0.0.1(server public network IP) If you can see the nginx page, it means the installation is successful.

InstallMySQL

Add MySQL official source

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

If the above download address changes, please click here to view the latest one.

Use the following command to see which versions are available for installation:
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

It can be seen that in addition to MySQL 5.7, the official MySQL source also provides other versions, such as 5.5, 5.6, and 8.0. If there is a need for other versions, you can install them as needed. You need to use yum-config-manager to enable or disable related The source (enabling the required version), for example disabling MySQL5.6 and enabling MySQL5.7:

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

Determine the version that needs to be installed. Installation:
yum install mysql-community-server
Just one line of command above can be installed. The installation process does not require manual intervention and the installation process is completed directly.

Note: The root password of MySQL Server is a random password generated during the installation process. If after installation you find that you do not know the root password of MySQL and cannot log in, you can use the following command to find the random root password of MySQL:

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

The string following localhost: is the temporary root password of MySQL.

After finding the password, perform MySQL initialization operation and execute the command:
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!

If you find it troublesome, you can also turn off this plug-in, but it is not recommended:

vi /etc/my.conf

[mysqld]
validate_password=Off

Next execute:

mysql -u root -p     #用上面设置的密码登陆mysql
mysql>create database wordpress character set utf8mb4 collate utf8mb4_unicode_ci;    # 创建wordpress数据库
mysql>use wordpress;    #切换到wordpress数据库
mysql>exit  # 退出mysql
  • For compatibility reasons, it is best to specify the character set when creating a new database, which can reduce many inexplicable problems in subsequent development:
    ncreate database if not exists zocodev default character set utf8 collate utf8_unicode_ci;
  • If you have emoji requirements, you can also specify the utf8mb4 character set:
    create database if not exists zocodev default character set utf8mb4 collate utf8mb4_unicode_ci;

Install PHP

Install version 5.X

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

Install version 7.X


In CentOS7, when installing PHP7.X, the error libargon2.so.0()(64bit) may be reported. Dependency error

execution:

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 reported:

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)

The solution is to open the corresponding source in /etc/yum.repos.d, that is, set the corresponding enabled to 1:
vi /etc/yum.repos.d/epel.repo

After opening, modify enabled in the node below

[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

Another modification:
vi /etc/yum.repos.d/remi-php73.repo

After opening, modify enabled in the node below

[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

Save the above two modifications and retry the installation command:

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

Modify nginx configuration

Open the nginx main configuration file
vi /etc/nginx/nginx.conf
and add in 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;
    }
}

After the input is completed, press ESC to enter the command mode, enter: wq, press Enter to save and exit, enter:
nginx -t -c /etc/nginx/nginx.conf
Check configuration file
Reload:
systemctl reload nginx

Test whether php-fpm is installed successfully

Enter vi /usr/share/nginx/html/index.php, press i to enter edit mode and enter the following:

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

After the input is completed, press ESCto enter command mode, enter :wq, press Enter to save and exit.
Then enter it in the browser http://当前服务器公网IP/index.php.
If it appears in the browser Hello World!, it means the configuration is successful. You can continue with the following steps. If a file download pop-up window appears, the configuration fails. Check whether there are errors in the above steps.

Install wordpress and configure 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站点的根目录

After completion, enter in the browser
http://你的主机IP或者域名/wp-admin/install.php
to enter the WordPress configuration page. After entering the website title, username and password, you can enter the WordPress backend management interface, and you are done.

Follow up

  • Regarding website updates, an FTP account is required

Find the file in the WordPress directory wp-config.phpand edit it, adding in the last line
define('FS_METHOD', "direct");

  • If it prompts that the directory cannot be created at this time, you can enter the following content:
    chown -Rf apache:root /usr/share/nginx/html/
    Refresh to update smoothly.

Regarding permission issues, the following is excerpted from the WordPress official manual

After completing these 2 steps, you can make all files in the wp-content directory writable, but before making each file and folder writable, you should take safer measures to modify the directory. Please try each command, and if it doesn't work, try them one after another. Some can even make the appearance theme image file writable. Replace DIR with the folder you wish to write to

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

If none of the above allow you to write, try again in sequence, but this time replace -v with -R, which will recursively modify each file located in the folder. If you still cannot write after completing this operation, you can try 777.
chmod -R 777 /usr/share/nginx/
WordPress: Official Manual for Changing File Permissions

Picture sharing

Insert image description here

Guess you like

Origin blog.csdn.net/Jo_Francis/article/details/125041068