lnmp+wordpress部署教程

linux+nginx+mysql+php+wordpress

注意:如果想顺利完成,请按照我的版本和环境执行。或者如果你完全理解了,可以按照自己的思路进行。

linux

本文中使用的是CentOS 7.8版本,理论上7.x都可以。

安装必要的依赖包

yum install gcc-c++ openssl-devel cyrus-sasl-md5 bzip2-devel curl-devel freetype-devel libjpeg-devel libpng-devel libxslt-devel libxml2-devel pcre-devel zlib-devel sqlite-devel

nginx-1.18

wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar zxf nginx-1.18.0.tar.gz
cd nginx-1.18.0
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make -j4 && make install

添加nginx用户和用户组

groupadd nginx
useradd -g nginx -s /sbin/nologin -M nginx 
# -g:指定所属的group -s:指定shell,因为它不需要登录,所以用/sbin/nologin -M:不创建home目录,因为它不需要登录

配置nginx.conf

vim /usr/local/nginx/conf/nginx.conf

user  nginx nginx; # 指定用户和用户组
error_log  logs/error.log; # 放开日志,同时需要放开http{}里面的log_format
pid        logs/nginx.pid; # 指定pid文件

配置http{ }

# 放开日志格式
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
# 在上传wordpress或者图片的时候,如果不增加配置,文件过大容易出现 413 Request Entity Too Large这个错误                      
client_max_body_size 20M; 

配置server{ }

主要修改location{
     },追加index.php让nginx服务器默认支持index.php为首页:

location / {
     
            root   html;
            index  index.php index.html index.htm;
        }

然后配置.php请求被传送到后端的php-fpm模块,默认情况下php配置块是被注释的,此时去掉注释并修改为以下内容:

location ~* \.php$ {
     
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}
这里面很多都是默认的,root是配置php程序放置的根目录,主要修改的就是fastcgi_param中的/scripts为$document_root修改客户端参数

添加服务化

vim /usr/lib/systemd/system/nginx.service文件,内容如下:

[Unit]
Description=nginx-The High-performance HTTP Server
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

重新加载服务文件,并启动nginx,最后在浏览器访ip地址测试nginx是否运行正常

systemctl daemon-reload
systemctl start nginx

添加开机启动

#第一种方法(推荐)
systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
#第二种方法
vim /etc/rc.local
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf &
chmod +x /etc/rc.d/rc.local

配置环境变量(非必须)

vim /etc/profile
for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done
# 在这里加入环境变量
export PATH="$PATH:/usr/local/nginx/sbin:" 
unset i
unset -f pathmunge

mysql-5.7

yum -y install mysql57-community-release-el7-10.noarch.rpm

wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm

yum -y install mysql-community-server

启动服务

systemctl start mysqld

systemctl status mysqld

修改默认密码,并登陆

grep "password" /var/log/mysqld.log

mysql -uroot -p

ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';

set global validate_password_policy=LOW;

set global validate_password_length=6;

SHOW VARIABLES LIKE 'validate_password%';

开启远程访问权限(如果需要的话)

grant all privileges on *.* to 'root'@'your ip' identified by 'password' with grant option;

grant all privileges on *.* to 'root'@'202.108.53.254' identified by 'MAOmao08091020<>?' with grant option;

flush privileges;

开启防火墙

systemctl start firewalld

firewall-cmd --zone=public --add-port=3306/tcp --permanent # 开放3306端口

firewall-cmd --zone=public --remove-port=3306/tcp --permanent #关闭3306端口

firewall-cmd --reload  # 配置立即生效

查看防火墙

firewall-cmd --zone=public --list-ports  #查看防火墙所有开放的端口

修改mysql的字符集:

vim /etc/my.cnf

[client]
default-character-set=utf8mb4
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci

php-7.4.11(需要的时间较长)

安装正则表达式库

wget https://rpms.remirepo.net/enterprise/7/remi/x86_64/oniguruma5php-6.9.5+rev1-4.el7.remi.x86_64.rpm
rpm -Uvh oniguruma5php-6.9.5+rev1-4.el7.remi.x86_64.rpm

wget https://rpms.remirepo.net/enterprise/7/remi/x86_64/oniguruma5php-devel-6.9.5+rev1-4.el7.remi.x86_64.rpm
rpm -Uvh oniguruma5php-devel-6.9.5+rev1-4.el7.remi.x86_64.rpm

下载

wget https://www.php.net/distributions/php-7.4.11.tar.gz
tar zxf php-7.4.11.tar.gz
cd php-7.4.11

配置、检查依赖

./configure --prefix=/usr/local/php  --with-config-file-path=/usr/local/php/etc --with-libdir=lib64 --with-curl --enable-gd --with-gettext --with-iconv-dir --with-kerberos  --with-mysqli --with-openssl --with-pdo-mysql --with-pdo-sqlite --with-pear --with-xmlrpc --with-xsl --with-zlib --with-bz2 --with-mhash --enable-fpm --enable-bcmath --enable-inline-optimization --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-sysvshm --enable-xml --without-pear  --disable-phar

编译安装

# -j [jobs], --jobs[=jobs] Specifies the number of jobs (commands) to run simultaneously.If there is more than one -j option, the last one is effective.  If the -j option is given without an argument, make will not limit the number of job that can run simultaneously.
make -j4 # 由于时间较长,使用多任务编译。实测4核主频4.3Ghz的处理器,1job耗时6分16秒,2jobs耗时3分20秒,4jobs耗时2分15秒,8jobs耗时2分13秒。
make install 

# 结果
Installing build environment:     /usr/local/php/lib/php/build/
Installing helper programs:       /usr/local/php/bin/
  program: phpize
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20190902/
  program: php-config
Installing PDO headers:           /usr/local/php/include/php/ext/pdo/
Installing man pages:             /usr/local/php/php/man/man1/
  page: phpize.1
  page: php-config.1
Installing PHP CLI binary:        /usr/local/php/bin/
Installing PHP FPM binary:        /usr/local/php/sbin/
Installing phpdbg binary:         /usr/local/php/bin/
Installing PHP CLI man page:      /usr/local/php/php/man/man1/
Installing PHP CGI binary:        /usr/local/php/bin/
Installing PHP FPM defconfig:     /usr/local/php/etc/
Installing phpdbg man page:       /usr/local/php/php/man/man1/
Installing PHP CGI man page:      /usr/local/php/php/man/man1/
Installing PHP FPM man page:      /usr/local/php/php/man/man8/
Installing PHP FPM status page:   /usr/local/php/php/php/fpm/
Installing header files:          /usr/local/php/include/php/

配置环境变量

vim /etc/profile

#在最后添加
PATH=$PATH:/usr/local/php/bin
export PATH
#更新环境变量
source /etc/profile
php -v #查看版本

配置php-fpm

cp php.ini-production /usr/local/php/etc/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
# 不再使用service控制,改为systemctl控制
#cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
#chmod +x /etc/init.d/php-fpm

配置systemctl控制

新建php-fpm.service文件

vim /usr/lib/systemd/system/php-fpm.service

追加下面的内容

[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target

[Service]
Type=simple
PIDFile=/usr/local/php/var/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -SIGINT $MAINPID

[Install]
WantedBy=multi-user.target

重新加载服务文件,并启动nginx,最后在浏览器访ip地址测试nginx是否运行正常

systemctl daemon-reload
systemctl enable php-fpm
systemctl start php-fpm

在nginx的html目录写一个test.php页面进行测试


    phpinfo();
?>

wordpress-5.5.1

wget https://wordpress.org/latest.tar.gz

tar zxf latest.tar.gz

创建www用户及用户组

groupadd www
useradd -g www -s /sbin/nologin -M www 

修改WP上传文件参数

需要上传主题的时候,对内存有限制,所以需要手动调整到合适的大小。去修改/usr/local/php/etc/php.ini

upload_max_filesize = 20M # 最大文件大小
max_file_uploads = 200 #最大文件数

解决需要FTP的问题(没出现就不用管)

进入wordpress的wp-content目录,新建tmp文件夹,设置文件夹的权限为777,并设置wp-content目录中的plugins(插件)和themes(主题)文件夹权限为777pwd

在wp-config.php中
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');
的后面添加:
    
define('WP_TEMP_DIR', ABSPATH.'wp-content/tmp');
define("FS_METHOD", "direct");
define("FS_CHMOD_DIR", 0777);
define("FS_CHMOD_FILE", 0777);

编译安装GD图片处理模块

# 进入PHP的源码扩展目录
cd /data/php-7.4.11/ext/gd
# 在该目录下执行phpize
/usr/local/php/bin/phpize
# 配置
./configure --with-php-config=/usr/local/php/bin/php-config
# 编译
make
# 安装
make install 
# 在php.ini文件中添加
extension=gd.so
# 重启php
service php-fpm restart

wordpress相关操作

网页内嵌百度地图定位

百度地图目前我用过的有2个,分别为1.1和2.0版本。1.1版本不需要秘钥也能运行,但是显示模糊且需要修改API,推荐2.0版本显示效果好但需要申请密匙。

  • 百度地图生成器1.1 http://api.map.baidu.com/lbsapi/creatmap/index.html

  • 要使用2.0的API,需要注册成为百度开发者,然后再申请密匙。申请密匙地址:http://lbsyun.baidu.com/apiconsole/key/create

  • 百度地图生成器2.0 http://api.map.baidu.com/lbsapi/createmap/

地图标注图标无法显示的解决方法

查找代码中的icon地址,将它改为http://api.map.baidu.com/lbsapi/creatmap/images/us_mk_icon.png即可

浏览器地图加载不出来解决办法

需要修改JS链接

<script type="text/javascript" src="http://api.map.baidu.com/api?key=&v=1.1&services=true"></script>
改为
<script type="text/javascript" src="http://api.map.baidu.com/getscript?key=&v=1.1&services=true"></script>

地图标注显示乱码

在1.1版本中会出现这样的情况,需要将编码从gb2312改为utf-8,搜素charset修改即可。

附生成好的2.0版本代码(已抹除密匙)


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript" src="http://api.map.baidu.com/getscript?v=2.0&ak=这里填你的密匙">script>
  head>
  
  <body>
    <div style="width:1080px;height:550px;border:#ccc solid 1px;font-size:12px" id="map">div>
  body>
  <script type="text/javascript">
    //创建和初始化地图函数:
    function initMap(){
      
      createMap();//创建地图
      setMapEvent();//设置地图事件
      addMapControl();//向地图添加控件
      addMapOverlay();//向地图添加覆盖物
    }
    function createMap(){
       
      map = new BMap.Map("map"); 
      map.centerAndZoom(new BMap.Point(116.315273,40.042901),19);
    }
    function setMapEvent(){
      
      map.enableScrollWheelZoom();
      map.enableKeyboard();
      map.enableDragging();
      map.enableDoubleClickZoom()
    }
    function addClickHandler(target,window){
      
      target.addEventListener("click",function(){
      
        target.openInfoWindow(window);
      });
    }
    function addMapOverlay(){
      
      var markers = [
        {
      content:"北京市海淀区上地三街金隅嘉华大厦",title:"北京XX科技有限公司",imageOffset: {
      width:0,height:3},position:{
      lat:40.042797,lng:116.314878}},
      ];
      for(var index = 0; index < markers.length; index++ ){
      
        var point = new BMap.Point(markers[index].position.lng,markers[index].position.lat);
        var marker = new BMap.Marker(point,{
      icon:new BMap.Icon("http://api.map.baidu.com/lbsapi/createmap/images/icon.png",new BMap.Size(20,25),{
      
          imageOffset: new BMap.Size(markers[index].imageOffset.width,markers[index].imageOffset.height)
        })});
        var label = new BMap.Label(markers[index].title,{
      offset: new BMap.Size(25,5)});
        var opts = {
      
          width: 200,
          title: markers[index].title,
          enableMessage: false
        };
        var infoWindow = new BMap.InfoWindow(markers[index].content,opts);
        marker.setLabel(label);
        addClickHandler(marker,infoWindow);
        map.addOverlay(marker);
      };
    }
    //向地图添加控件
    function addMapControl(){
      
      var scaleControl = new BMap.ScaleControl({
      anchor:BMAP_ANCHOR_BOTTOM_LEFT});
      scaleControl.setUnit(BMAP_UNIT_IMPERIAL);
      map.addControl(scaleControl);
      var navControl = new BMap.NavigationControl({
      anchor:BMAP_ANCHOR_TOP_LEFT,type:0});
      map.addControl(navControl);
      var overviewControl = new BMap.OverviewMapControl({
      anchor:BMAP_ANCHOR_BOTTOM_RIGHT,isOpen:true});
      map.addControl(overviewControl);
    }
    var map;
      initMap();
  script>
html>

你可能感兴趣的:(其他,lnmp,wordpress)