一直以来我都强烈建议各位使用LAMP环境,因为这似乎是最合适的选择。但是Apache在内存占用和并发能力方面并不是那么尽人意,相比之下Nginx更加优秀。本文将介绍如何在CentOS7系统上部署LNMP环境并安装Nextcloud。
[ssbluelist]
本教程以Vultr上的一台VPS为例。
CentOS默认的yum源中并不包含Nginx和php-fpm,首先要为CentOS添加epel源:
yum -y install epel-release
完成安装后,就可以用yum安装Nginx了:
yum -y install nginx
现在需要再添加一个yum源来安装php-fpm,我这儿使用的是webtatic(这个yum源对国内网络来说恐怕有些慢,当然你也可以选择其它的yum源)。
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
接下来,安装php7-fpm和一些其它的必要的组件:
yum -y install php70w-fpm php70w-cli php70w-gd php70w-mcrypt php70w-mysql php70w-pear php70w-xml php70w-mbstring php70w-pdo php70w-json php70w-pecl-apcu php70w-pecl-apcu-devel
完成后,检查一下php-fpm是否已正常安装:
php -v
php与Nginx并不像与Apache那样易于相处,需要让Nginx分离静态和动态请求,并将动态请求交给php-fpm来处理,接下来是一些必要的配置。
使用vim编辑php-fpm的配置文件:
vim /etc/php-fpm.d/www.conf
[infobox]注:在vim的普通模式下输入直接键入
:set number即可显示行号。[/infobox]
在配置文件的第8~10行,找到这两项配置:
user = apache group = apache
将用户和组都改为
nginx
:
user = nginx group = nginx
在第22行可以看到php-fpm所监听的端口为9000:
listen = 127.0.0.1:9000
在第366~370行找到有关环境变量的设置:
;env[HOSTNAME] = $HOSTNAME ;env[PATH] = /usr/local/bin:/usr/bin:/bin ;env[TMP] = /tmp ;env[TMPDIR] = /tmp ;env[TEMP] = /tmp
删掉每行前面的
;
,取消注释。
完毕后保存文件并退出。
接下来,在
/var/lib/
目录下为session路径创建一个新的文件夹,并将用户名和组设为
nginx
。
mkdir -p /var/lib/php/session
chown nginx:nginx -R /var/lib/php/session/
启动Nginx和php-fpm服务,并添加开机启动:
systemctl start php-fpm
systemctl start nginx
systemctl enable php-fpm
systemctl enable nginx
php-fpm的配置到此完成。
使用MaraiDB作为Nextcloud数据库。yum安装MaraiDB服务:
yum -y install mariadb mariadb-server
启动MariaDB服务并添加开机启动:
systemctl start mariadb systemctl enable mariadb
接下来设置MariaDB的root密码:
mysql_secure_installation
按照提示设置密码,首先会询问当前密码,密码默认为空,直接回车即可:
Enter current password for root (enter for none):          #直接回车
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的密码后,使用命令行登录MariaDB,并为Nextcloud创建相应的用户和数据库。例如将要创建的数据库为
nextcloud_db
,用户名为
nextclouduser
,密码为
nextcloudpasswd
(这只是个示例,为确保安全请尽量将密码设置得复杂些)。
mysql -u root -p
然后键入密码
使用SQL语句创建用户和数据库:
create database nextcloud_db; create user nextclouduser@localhost identified by 'nextcloudpasswd'; grant all privileges on nextcloud_db.* to nextclouduser@localhost identified by 'nextcloudpasswd'; flush privileges;
数据库
nextcloud_db
和用户
nextclouduser
,创建完成,密码是
nextcloudpasswd
。
在本教程中,我将使用客户端的https连接运行nextcloud。您可以使用免费的SSL证书,例如让加密或创建自签名的SSL证书。我将使用OpenSSL命令创键自签名SSL证书文件:
为SSL证书创建一个新的文件夹:
mkdir -p /etc/nginx/cert/
生成一个新的SSL证书文件:
openssl req -new -x509 -days 365 -nodes -out /etc/nginx/cert/nextcloud.crt -keyout /etc/nginx/cert/nextcloud.key
然后将证书文件的权限设置为660:
chmod 700 /etc/nginx/cert chmod 600 /etc/nginx/cert/*
我将使用wget命令直接将Nextcloud下载到服务器上,在此之前需要安装两样必要的工具:
yum -y install wget unzip
前往
/tmp
文件夹,使用wget命令下载Nextcloud:
cd /tmp
wget https://download.nextcloud.com/server/releases/nextcloud-12.0.0.zip
将下载完成的zip文件解压,然后移动到Nginx的目录
/usr/share/nginx/html/
中:
unzip nextcloud-12.0.0.zip
mv nextcloud/ /usr/share/nginx/html/
然后进入Nginx的root目录,并为Nextcloud创建data目录:
cd /usr/share/nginx/html/
mkdir -p nextcloud/data/
将Nextcloud的用户和组修改为
nginx
:
chown nginx:nginx -R nextcloud/
Nextcloud并不能在Nginx完美地运行,我们需要为它量身定做一个虚拟主机配置。
进入Nginx的虚拟主机配置文件所在目录并创建一个新的虚拟主机配置:
cd /etc/nginx/conf.d/ vim nextcloud.conf
然后将下面的配置信息粘贴进去(记得修改两个
server_name
为你自己的域名):
upstream php-handler {
    server 127.0.0.1:9000;
    #server unix:/var/run/php5-fpm.sock;
}
 
server {
    listen 80;
    server_name cloud.dreampacific.cn;
    # enforce https
    return 301 https://$server_name$request_uri;
}
 
server {
    listen 443 ssl;
    server_name cloud.dreampacific.cn;
 
    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;
    }
}
检查一下有没有漏掉的地方,完成后保存退出。
接下来测试以下配置文件是否有错误,确保没有问题后重启Nginx服务。
nginx -t systemctl restart nginx
安装SElinux管理工具policycoreutils-python:
yum -y install policycoreutils-python
执行以下命令来使Nextcloud在SELinux下正常运行:
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/'
接下来设置防火墙,为Nextcloud开放http和https两个端口。
启动Firewalld服务并设置开机启动:
systemctl start firewalld systemctl enable firewalld
用firewalld-cmd命令开放http和https端口:
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
访问你的服务器,可以给服务器绑定一个域名,我的是cloud.dreampacific.cn。
访问域名,会自动跳转到https连接,按照提示安装即可。