Nginx配置PHP网页(高级)

Nginx配置PHP网页(高级)

dkm
dkm
2022-12-26 / 0 评论 / 16 阅读 / 正在检测是否收录...

本篇为本系列教程第三个项目实战内容,本次内容需要配置Nginx,MariaDB,PHP7.3搭建Typecho网站。

搭建之前先下载Typecho的安装包: >>>下载<<<

CentOS下安装MariaDB

  #配置源
  nano /etc/yum.repo.d/mariadb.repo

  #-----粘贴下部分-----
    [mariadb]
    name = MariaDB
    baseurl = https://mirrors.ustc.edu.cn/mariadb/yum/10.2/centos7-amd64
    gpgkey=https://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB
    gpgcheck=1
  #-------------------
  #清除当前缓存并重新生成
  yum clean all
  yum makcahe
  #安装MariaDB数据库软件
  yum -y install MariaDB-server MariaDB-client
  #启动mariadb以及设置开机自启
  systemctl start mariadb && systemctl enable mariadb
  #--------------------分割-------------------------
  #初始化MariaDB服务
  sudo mysql_secure_installation
  #初始化的选项
  Enter current password for root (enter for none):<– 回车
  Set root password? [Y/n] <– 设置root密码y
  New password: <– 设置root用户的密码(密码设置成[email protected])
  Re-enter new password: <– 再输入一次你设置的密码
  Remove anonymous users? [Y/n] <– 删除匿名用户y
  Disallow root login remotely? [Y/n] <–禁止远程root用户n
  Remove test database and access to it? [Y/n] <– 删除Test数据库y
  Reload privilege tables now? [Y/n] <– 重新加载权限表y
  #----------------------初始化完毕------------------
  #使用root用户登录数据库
  mysql -u root -p -h localhost -P 3306
  #密码是[email protected],刚才设置的
  #设置typecho用户并配置本地登录,设置用户密码为typecho
  mysql> GRANT ALL PRIVILEGES ON *.* TO 'typecho'@localhost IDENTIFIED BY 'typecho' WITH GRANT OPTION;
  mysql > flush PRIVILEGES;
  #创建数据库(下面这一行需要输入分号)
  mysql> create database typecho_db;
  #查看数据库是不是创建了
  MariaDB [(none)]> SHOW DATABASES;
  +--------------------+
  | Database           |
  +--------------------+
  | information_schema |
  | mysql              |
  | performance_schema |
  | typecho_db         |
  +--------------------+
  4 rows in set (0.00 sec)
  
  MariaDB [(none)]>   

安装PHP-FPM模块

  #安装运行环境
  yum -y install gcc gcc-c++
  #安装第三方软件源
  yum -y install epel-release
  #安装软件包
  yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
  yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
  #安装包管理器组件
  yum -y install yum-utils
  #启用php7.3源
  yum-config-manager --enable remi-php73
  #安装PHP7需要的组件
  yum -y install php php-mcrypt php-devel php-cli php-gd php-pear php-curl php-fpm php-mysql php-ldap php-zip php-fileinfo
  
  
  #安装完了查看版本
  php -v
#安装版本信息
PHP 7.3.33 (cli) (built: Dec 19 2022 14:30:27) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.33, Copyright (c) 1998-2018 Zend Technologies

  #启动php-fpm,开机自启动
  systemctl start php-fpm
  systemctl enable php-fpm
  

不编译安装Nginx 1.22

  yum -y install epel-release
  yum -y install nginx
  systemctl start nginx && systemctl enable nginx

上传Typecho的安装包文件到CentOS,配置路径
使用WinSCP工具上传到CentOS
Test

下面对文件进行操作

  cd /www/blog.nginx.test
  unzip typecho.zip
      Archive:  typecho.zip
      inflating: LICENSE.txt             
       creating: admin/
       creating: admin/img/
      inflating: admin/img/[email protected]  
      inflating: admin/img/ajax-loader.gif  
      inflating: admin/img/typecho-logo.svg  
      inflating: admin/img/[email protected]  
      inflating: admin/img/icons.png
      ........
  #--------
  ls
  admin  index.php  install  install.php  LICENSE.txt  typecho.zip  usr  var
  #------------------------
  cd /www
  chmod 777 blog.nginx.test/

使用KeyManager签发一个SSL证书,域名为blog.nginx.test
将证书名字改成local.crt,私钥文件名字改成local.key,上传到服务器的/cert目录下,设置cert目录权限为777

配置Nginx网站配置文件

  nano /etc/nginx/conf.d/blog.nginx.test.conf

  #------下面为Nginx网站配置文件
  server {
        listen                     80;
        listen                     443 ssl;
        server_name                blog.nginx.test;
        alias         /www/blog.nginx.test;
        index        index.html index.htm index.php;
        ssl_certificate            /cert/local.crt;
        ssl_certificate_key        /cert/local.key;
        ssl_ciphers                ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
        ssl_prefer_server_ciphers  on;
        ssl_protocols              TLSv1 TLSv1.1 TLSv1.2;
        ssl_session_cache          shared:SSL:5m;
        ssl_session_timeout        5m;
    
        if ($scheme = http) {
            return  301 https://$host$request_uri;
        }
        
        if (!-e $request_filename) {
        rewrite ^(.*)$ /index.php$1 last;
        }
        
        location ~ ^/.+\.php {
            fastcgi_index            index.php;
            fastcgi_split_path_info  ^(.+\.php)(/.*)$;
            fastcgi_param            SCRIPT_FILENAME $request_filename;
            fastcgi_param            PATH_INFO $fastcgi_path_info;
            fastcgi_param            PATH_TRANSLATED $document_root$fastcgi_path_info;
            include                  fastcgi_params;
            fastcgi_pass             127.0.0.1:9000;
        }
        #设置缓存3d
        location ~* \.(js|css|jpg|gif|png|bmp|swf)$ {
            expires  3d;
        }
  }

  #---------Nginx配置文件配置完毕

重启Nginx服务器

  systemctl restart nginx

最后步骤

  #安装mbstring
  yum -y install php-mbstring
  #在php.ini设置mbstring弃用
  nano /etc/php.ini
  #在文件内加入这一行
  extension=mbstring.so
  #保存重启PHP-FPM和Nginx服务器
  systemctl restart php-fpm && systemctl restart nginx
  #设置上传目录权限
  cd /网站目录
  chmod 777 usr/uploads

打开浏览器输入https://blog.nginx.test/install.php

出现证书问题不需要管,证书是自签名的,自签名的证书一般不信任。
安装Typecho
Test
设置数据库参数(一开始设置的用户名密码和数据库名)
Test
设置用户名和密码(密码设置成[email protected]
Test
设置完成后发布一个文章
Test
查看数据库有没有出现数据表
Test

0

评论

博主关闭了所有页面的评论