Linux下部署Nginx+Mysql+PHP搭建WordPress教程

趁阿里云活动期间购买了一台ECS,准备部署LNMP环境并搭建Wordpress,若不是期间遇到些坎坷,比如wordpress的ftp权限问题、Rewrite问题等,也不至于写这篇教程。

操作系统: CentOS 7.5 64位
 数据库:Mysql 5.7.26
运行环境:Nginx 1.12,PHP 7.2.17

注:本文部分资料来自网络,可惜忘了具体网址,十分抱歉!

部署mysql

站内有详细的Mysql安装教程,本文不详述,具体请参考CentOS7 安装 MySQL 5.7 详细手册

登录mysql,创建数据库及账户:

ceate database wpdb;

grant all privileges on wpdb.* to 'wpuser'@'localhost' identified by 'WPdb@123';

flush privileges;

部署nginx

yum -y install nginx

配置nginx

修改nginx主要参数:

vim /etc/nginx/nginx.conf

...

http {

    ...

    server_tokens        off;
    client_max_body_size 50m;

    gzip            on;
    gzip_min_length 1k;
    gzip_buffers    32 8k;
    gzip_comp_level 3;
    gzip_types      text/plain text/css application/javascript application/xml+rss application/x-httpd-php image/jpeg image/png;
    gzip_types      application/vnd.ms-fontobject font/ttf font/opentype font/x-woff image/svg+xml;
    gzip_vary       on;

    ...

    include /etc/nginx/conf.d/*.conf;
}

修改域名配置文件:

vim /etc/nginx/conf.d/andylouse.conf

    server {
        listen      80;
        listen      [::]:80;
        server_name andylouse.net www.andylouse.net;
        root        /www/andylouse;
        index       index.php;

        location / {
            if (!-e $request_filename) {
                rewrite ^([_0-9a-zA-Z-]+)?(/wp-.*) $2 last;
                rewrite ^([_0-9a-zA-Z-]+)?(/.*\.php)$ $2 last;
                rewrite ^ /index.php last;
            }
#           try_files $uri $uri/ /index.php?$args;
        }

        location = /favicon.ico {
            expires       max;
            access_log    off;
            log_not_found off;
        }

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

        location ~ \.php {
            set $path_info "";
            set $real_script_name $fastcgi_script_name;
            if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
                set $real_script_name $1;
                set $path_info $2;
            }
            include       fastcgi_params;
            include       fastcgi.conf;
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_intercept_errors on;
            fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
            fastcgi_param SCRIPT_NAME $real_script_name;
            fastcgi_param PATH_INFO $path_info;
        }

        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
            expires       max;
            access_log    off;
            log_not_found off;
        }

        location ~* ^.+\.(eot|ttf|otf|woff|svg)$ {
            expires       max;
            access_log    off;
        }

    }

更多nginx配置参数请参见WordPress.com.org

启动nginx

systemctl start nginx && systemctl enable nginx

部署PHP

推荐方案一:yum安装

yum -y install epel-release
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

yum -y install php72w php72w-cli php72w-fpm php72w-common php72w-devel php72w-embedded php72w-gd php72w-mbstring php72w-mysqlnd php72w-opcache php72w-pdo php72w-xml php72w-bcmath

systemctl start php-fpm && systemctl enable php-fpm

方案二:编译安装

安装php依赖包

yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel

安装php支持包

yum -y install php-fpm php-mysql

systemctl start php-fpm && systemctl enable php-fpm

安装PHP

cd /opt

wget https://www.php.net/distributions/php-7.0.33.tar.gz

tar zxf php-7.0.33.tar.gz

cd phhp-7.0.33

./configure \
--prefix=/opt/php7 \
--with-config-file-path=/etc \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-libxml-dir \
--with-xmlrpc \
--with-openssl \
--with-mcrypt \
--with-mhash \
--with-pcre-regex \
--with-sqlite3 \
--with-zlib \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--with-cdb \
--enable-dom \
--enable-exif \
--enable-fileinfo \
--enable-filter \
--with-pcre-dir \
--enable-ftp \
--with-gd \
--with-openssl-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir \
--with-freetype-dir \
--enable-gd-native-ttf \
--enable-gd-jis-conv \
--with-gettext \
--with-gmp \
--with-mhash \
--enable-json \
--enable-mbstring \
--enable-mbregex \
--enable-mbregex-backtrack \
--with-libmbfl \
--with-onig \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-zlib-dir \
--with-pdo-sqlite \
--with-readline \
--enable-session \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-wddx \
--with-libxml-dir \
--with-xsl \
--enable-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-opcache

make && make install

配置php.ini

默认最大上传文件大小为2M,可修改以下参数进行调整:

post_max_size = 50M
upload_max_filesize = 50M

配置php环境变量

注意:yum安装PHP时不需要该操作!

vim /etc/profile

添加如下参数:

PATH=$PATH:/opt/php7/bin;
export PATH

使设置生效:

source /etc/profile

至此,运行wordpress的环境均已配置好:

配置网站目录

下载wordpress

mkdir /www

cd /www

wget https://cn.wordpress.org/latest-zh_CN.tar.gz

tar zxf latest-zh_CN.tar.gz

mv latest-zh_CM andylouse

设置权限

chown -R apache:apache andylouse/

chmod -R +x andylouse/

现在访问站点开始安装Wordpress吧!

赞 (0) 打赏

评论 0

评论前必须登录!

登陆 注册

感谢您的支持与帮助

支付宝扫一扫打赏

微信扫一扫打赏