«

怎么在CentOS7中使用Nginx和PHP7

时间:2024-4-23 09:32     作者:韩俊     分类: Linux


先决条件

  1. 64 位的 centos 7

  2. 服务器的 root 权限

  3. 步骤 1 - 在 centos 7 中安装 nginx 和 php7-fpm

    在开始安装 nginx 和 php7-fpm 之前,我们还学要先添加 epel 包的仓库源。使用如下命令:

    在第 8 行和第 10行,user 和 group 赋值为 nginx。

    user = nginx
    group = nginx

    取消第 366-370 行的注释,启用 php-fpm 的系统环境变量。

    env[hostname] = $hostname
    env[path] = /usr/local/bin:/usr/bin:/bin
    env[tmp] = /tmp
    env[tmpdir] = /tmp
    env[temp] = /tmp

    保存文件并退出 vim 编辑器。

    下一步,就是在 /var/lib/ 目录下创建一个新的文件夹 session,并将其拥有者变更为 nginx 用户。

    mkdir -p /var/lib/php/session
    chown nginx:nginx -r /var/lib/php/session/

    然后启动 php-fpm 和 nginx,并且将它们设置为随开机启动的服务。

    sudo systemctl start php-fpm
    sudo systemctl start nginx
    sudo systemctl enable php-fpm
    sudo systemctl enable nginx

    启动 mariadb,并将其添加到随系统启动的服务中去。

    systemctl start mariadb
    systemctl enable mariadb

    键入 y ,然后设置 mariadb 的 root 密码。

    set root password? [y/n] y
    new password:
    re-enter new password:
    remove anonymous users? [y/n] y
    disallow root login remotely? [y/n] y
    remove test database and access to it? [y/n] y
    reload privilege tables now? [y/n] y

    输入 mariadb 的 root 密码,即可登录 mysql shell。

    输入以下 mysql 查询语句来创建新的数据库和用户。

    create database nextcloud_db;
    create user nextclouduser@localhost identified by 'nextclouduser@';
    grant all privileges on nextcloud_db.* to nextclouduser@localhost identified by 'nextclouduser@';
    flush privileges;

    如下,使用 openssl 生成一个新的 ssl 证书。

    复制代码 代码如下:


    openssl req -new -x509 -days 365 -nodes -out /etc/nginx/cert/nextcloud.crt -keyout /etc/nginx/cert/nextcloud.key

    最后使用 chmod 命令将所有证书文件的权限设置为 600。

    chmod 700 /etc/nginx/cert
    chmod 600 /etc/nginx/cert/*

    先进入 /tmp 目录,然后使用 wget 从官网下载最新的 nextcloud 10。

    cd /tmp
    wget https://download.nextcloud.com/server/releases/nextcloud-10.0.2.zip

    解压 nextcloud,并将其移动到 /usr/share/nginx/html/ 目录。

    unzip nextcloud-10.0.2.zip
    mv nextcloud/ /usr/share/nginx/html/

    下一步,转到 nginx 的 web 根目录为 nextcloud 创建一个 data 文件夹。

    cd /usr/share/nginx/html/
    mkdir -p nextcloud/data/

    步骤 6 - 在 nginx 中为 nextcloud 配置虚拟主机

    在步骤 5 我们已经下载好了 nextcloud 源码,并配置好了让它运行于 nginx 服务器中,但我们还需要为它配置一个虚拟主机。在 nginx 的 conf.d 目录下创建一个新的虚拟主机配置文件 nextcloud.conf。

    cd /etc/nginx/conf.d/
    vim nextcloud.conf

    将以下内容粘贴到虚拟主机配置文件中:

    upstream php-handler {
      server 127.0.0.1:9000;
      #server unix:/var/run/php5-fpm.sock;
    }
    server {
      listen 80;
      server_name cloud.nextcloud.co;
      # enforce https
      return 301 https://$server_name$request_uri;
    }
    server {
      listen 443 ssl;
      server_name cloud.nextcloud.co;
      ssl_certificate /etc/nginx/cert/nextcloud.crt;
      ssl_certificate_key /etc/nginx/cert/nextcloud.key;
      # add headers to serve security related headers
      # before enabling strict-transport-security headers please read into this
      # topic first.
      add_header strict-transport-security "max-age=15768000;
      includesubdomains; preload;";
      add_header x-content-type-options nosniff;
      add_header x-frame-options "sameorigin";
      add_header x-xss-protection "1; mode=block";
      add_header x-robots-tag none;
      add_header x-download-options noopen;
      add_header x-permitted-cross-domain-policies none;
      # path to the root of your installation
      root /usr/share/nginx/html/nextcloud/;
      location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
      }
      # the following 2 rules are only needed for the user_webfinger app.
      # uncomment it if you're planning to use this app.
      #rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
      #rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json
      # last;
      location = /.well-known/carddav {
       return 301 $scheme://$host/remote.php/dav;
      }
      location = /.well-known/caldav {
       return 301 $scheme://$host/remote.php/dav;
      }
      # set max upload size
      client_max_body_size 512m;
      fastcgi_buffers 64 4k;
      # disable gzip to avoid the removal of the etag header
      gzip off;
      # uncomment if your server is build with the ngx_pagespeed module
      # this module is currently not supported.
      #pagespeed off;
      error_page 403 /core/templates/403.php;
      error_page 404 /core/templates/404.php;
      location / {
        rewrite ^ /index.php$uri;
      }
      location ~ ^/(?:build|tests|config|lib|3rdparty|templates|data)/ {
        deny all;
      }
      location ~ ^/(?:.|autotest|occ|issue|indie|db_|console) {
        deny all;
      }
      location ~ ^/(?:index|remote|public|cron|core/ajax/update|status|ocs/v[12]|updater/.+|ocs-provider/.+|core/templates/40[34]).php(?:$|/) {
        include fastcgi_params;
        fastcgi_split_path_info ^(.+.php)(/.*)$;
        fastcgi_param script_filename $document_root$fastcgi_script_name;
        fastcgi_param path_info $fastcgi_path_info;
        fastcgi_param https on;
        #avoid sending the security headers twice
        fastcgi_param modheadersavailable true;
        fastcgi_param front_controller_active true;
        fastcgi_pass php-handler;
        fastcgi_intercept_errors on;
        fastcgi_request_buffering off;
      }
      location ~ ^/(?:updater|ocs-provider)(?:$|/) {
        try_files $uri/ =404;
        index index.php;
      }
      # adding the cache control header for js and css files
      # make sure it is below the php block
      location ~* .(?:css|js)$ {
        try_files $uri /index.php$uri$is_args$args;
        add_header cache-control "public, max-age=7200";
        # add headers to serve security related headers (it is intended to
        # have those duplicated to the ones above)
        # before enabling strict-transport-security headers please read into
        # this topic first.
        add_header strict-transport-security "max-age=15768000;
        includesubdomains; preload;";
        add_header x-content-type-options nosniff;
        add_header x-frame-options "sameorigin";
        add_header x-xss-protection "1; mode=block";
        add_header x-robots-tag none;
        add_header x-download-options noopen;
        add_header x-permitted-cross-domain-policies none;
        # optional: don't log access to assets
        access_log off;
      }
      location ~* .(?:svg|gif|png|html|ttf|woff|ico|jpg|jpeg)$ {
        try_files $uri /index.php$uri$is_args$args;
        # optional: don't log access to other assets
        access_log off;
      }
    }

    保存文件并退出 vim。

    下载测试以下该 nginx 配置文件是否有错误,没有的话就可以重启服务了。

    nginx -t
    systemctl restart nginx

    然后以 root 用户来运行以下命令,以便让 nextcloud 运行于 selinux 环境之下。如果你是用的其他名称的目录,记得将 nextcloud 替换掉。

    semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/nextcloud/data(/.*)?'
    semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/nextcloud/config(/.*)?'
    semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/nextcloud/apps(/.*)?'
    semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/nextcloud/assets(/.*)?'
    semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/nextcloud/.htaccess'
    semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/nextcloud/.user.ini'
    restorecon -rv '/usr/share/nginx/html/nextcloud/'

    接下来,我们要启用 firewalld 服务,同时为 nextcloud 开启 http 和 https 端口。

    启动 firewalld 并设置随系统启动。

    systemctl start firewalld
    systemctl enable firewalld

    现在使用 firewall-cmd 命令来开启 http 和 https 端口,然后重新加载防火墙。

    firewall-cmd --permanent --add-service=http
    firewall-cmd --permanent --add-service=https
    firewall-cmd --reload

    至此,服务器配置完成。

    步骤 8 - nextcloud 安装

    打开你的 web 浏览器,输入你为 nextcloud 设置的域名,我这里设置为 cloud.nextcloud.co,然后会重定向到安全性更好的 https 连接。

    设置你的管理员用户名和密码,然后输入数据验证信息,点击 '完成安装 (finish setup)'。

    标签: linux

    热门推荐