Nginx

群晖 Nginx

位置在: /usr/local/etc/nginx/sites-available (最新编辑的一个) 里面相应 server 的 include user.conf 位置。


CentOS 安装 Nginx + PHP + MySQL + MariaDB

安装 Nginx

sudo yum install nginx

# 配置位置
/etc/nginx/nginx.conf

安装 PHP

  1. 解压

    tar zxf php-x.x.x.tar.gz cd php-x.x.x

  2. 配置

# 简单配置
./configure --enable-fpm --with-mysql

# 常用配置
./configure --prefix=/usr/local/php --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-xmlrpc --with-xsl --with-zlib --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-xml --enable-zip

# Magento Open Source 配置

# 扩展所需的一些前置依赖
yum install openssl-devel # 安装之后 /usr/lib64/pkgconfig 目录就有 openssl.pc
yum install libxml2-devel sqlite-devel libcurl libcurlyum-devel libpng-devel libicu-devel gcc gcc-c++ oniguruma libsodium-devel libxslt-devel libzip-devel
dnf --enablerepo=powertools install oniguruma-devel

./configure --prefix=/usr/local/php --enable-bcmath --with-curl --enable-gd --enable-intl --enable-mbstring --with-mysqli --with-openssl --with-pdo-mysql --enable-soap --enable-sockets --with-sodium --with-xsl --with-zip --enable-fpm
  1. 编译安装
make
sudo make install
  1. 配置
cp php.ini-development /usr/local/php/php.ini
cp sapi/fpm/php-fpm /usr/local/bin

cd /usr/local/etc
# 或者,很可能是
cd /usr/local/php/etc

cp php-fpm.d/www.conf.default php-fpm.d/www.conf
cp php-fpm.conf.default php-fpm.conf
vi php-fpm.conf

# 腾讯云未配置如下信息

# include=NONE/etc/php-fpm.d/*.conf
# 改为
# include=etc/php-fpm.d/*.conf

vi /usr/local/php/php.ini
cgi.fix_pathinfo=0

# vi /usr/local/etc/php-fpm.d/www.conf
# user = www-data
# group = www-data
  1. 启动
/usr/local/bin/php-fpm

重启 https://www.cnblogs.com/gazeon/p/5421906.html

Nginx支持PHP

location ~* \.php$ {
    fastcgi_index index.php;
    fastcgi_pass 127.0.0.1:9000;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}

安装 MySQL

官方文档 中文帮助

Composer

https://getcomposer.org/doc/00-intro.md#installation-linux-unix-macos

MariaDB(未整理)

cd /etc/yum.repos.d 
vi MariaDB.repo



# MariaDB 10.5 [Stable] CentOS repository list - created 2020-09-02 14:13 UTC
# https://mariadb.org/download-test/
[mariadb]
name = MariaDB
baseurl = https://mirrors.ustc.edu.cn/mariadb/yum/10.5/centos8-amd64
module_hotfixes=1
gpgkey=https://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck=1



dnf install MariaDB-server
systemctl start mariadb

其它: get http://mirrors.aliyun.com/repo/Centos-7.repo /etc/yum.repos.d/ yum install MariaDB-server MariaDB-client ##安装mariadb服务器端、客户端## vim /etc/my.cnf.d/server.cnf ##mariadb服务器端配置文件## [mysqld] ##以下项是优化项目可根据需要选择## innodb_file_per_table=ON ##使用独立表空间模式(建议使用)## skip_name_resolve=ON ##禁止域名反向解析(可选)## systemctl start mariadb.service ##启动mariadb服务 systemctl enable mariadb.service ##设置为开机启动## mysql_secure_installation ##安装完成后安全初始化,添加root密码,删除匿名登录账户,禁止远程使用root用户登录,删除测试数据库和使用##
mysql –uroot –hlocalhost –p’PASSWORD ##登陆数据库##’

Nginx 配置

root的处理结果是:root路径+location路径 alias的处理结果是:使用alias路径替换location路径

Location 匹配

location / 默认规则,未匹配到其它规则时生效。

  • = 精确匹配,搜索终止。
  • ~ 区分大小写正则
  • ~* 不区分大小写正则
  • ^~ 普通匹配,搜索终止。

优先级

location ^~ /a1/b1 { # A }
location ^~ /a1 { # B }
location /a2/b2 { # C }
location /a2 { # D }

/a1/b1/c1 匹配 A /a2/b2/c2 匹配 D

rewrite

~ 为区分大小写匹配 ~* 为不区分大小写匹配 !~ 分别为区分大小写不匹配 !~* 不区分大小写不匹配

server 模块下,会优先执行 rewrite 部分。

pathinfo

关闭默认站点

server {
    listen 80 default;
    
    # 返回错误
    return 500;
    
    # 导流到其他网站
    rewrite ^(.*) http://jb51.net permanent;

    # 禁止IP访问

    server_name _;
    return 500;
}

强制https

  1. 在监听80端口的server配置中跳转到https

    server {
    listen 80; server_name domain.com; rewrite ^(.*)$ https://$host$1 permanent; }

  2. 使用497状态码

server {
    # ...
    if ($server_port = 80) {
        return 497;
    }
    
    error_page 497 https://$host$request_uri;
}

Nginx配置示例

server {
    listen 80 default_server; # v0.8.21以上,之前是default
    # 有多个443 server 的时候,必须指定一个default_server,不然只有第一个能访问(何为第一个?)。
    # 设置 443 端口的 default_server 时,必须制定 ssl_certificate 和 ssl_certificate_key,不然会阻止服务器所有 server 的https访问。而且不能使用 ssl on; 要将ssl写在listen里面。
    listen 443 default ssl http2;  # http2提高性能,目前只支持https,且要求nginx版本
    # listen [::]:80;  # 监听所有ipv6地址
    index index.html;
    root /www;
    autoindex on;  # 开启目录浏览功能;   
    autoindex_exact_size off;  # 关闭详细文件大小统计,让文件大小显示MB,GB单位,默认为b;   
    autoindex_localtime on;  # 开启以服务器本地时区显示文件修改日期!  
    
    include enable-php.conf;
    
    location /nginx-status {
        stub_status on;  # 显示nginx运行状态(连接数等)
    }
    
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
        expires 30d;
    }
    location ~ .*\.(js|css)?$ {
        expires 48h;
    }
    location /deny-files {
        deny all;  # 禁止访问
        allow all;  # 允许访问,规则自上而下,匹配到即跳出。
    }
    
    location ^~ /api/ {
        proxy_pass http://feizhaojun.com:7001/;
    }
    # 需要php 用 proxy_pass 更方便
    location ^~ /demo/ {
        proxy_pass http://demo.feizhaojun.com/;
    }
    # 静态文件
    location ^~ /img/ {
        alias /mnt/www/com-feizhaojun-img/;
        expires 90d;
    }
    
    # Rewrite
    
    # rewrite 正则出现 {} 要使用引号 "[a-z]{3}"
    
    # Rewrite Demo
    
    # 全部跳转
    rewrite ^ https://www.domain.com$request_uri;
    
    # feizhaojun.com
    rewrite ^/resume(.html)? /index.php?p=184 last;
    rewrite ^/contact(.html)? /index.php?p=1180 last;
    rewrite "^/([0-9]{2,5})/*.*$" /index.php?p=$1 last;
    # old links 2019-01-10
    rewrite event-170214/.* /web/love-fortune last;
    rewrite event-170214/2017021301/.* /web/love-fortune last;
    
    # History try_files
    location ~ /web {
        try_files $uri $uri/ /web/index.html;
    }
    
    # $request_uri: /stat.php?id=1585378&web_id=1585378
    # $uri /stat.php
    # $document_uri: /stat.php
    #

    # Error Page
    error_page 404 /404.html;

    access_log /home/wwwlogs/feizhaojun.com.log;

    # ssl
    # ssl on;
    ssl_certificate /mnt/www/cert/2336481.pem;
    ssl_certificate_key /mnt/www/cert/2336481.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    
    # 腾讯云 SSL证书
    ssl_certificate /home/cert/grmlab.org.crt;
    ssl_certificate_key /home/cert/grmlab.org.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;
    
    # ----------
    
    # 强制https,用变量代替逻辑且
    set $redirect_https false;
    if ($server_port = 80) {
        set $redirect_https true;
    }
    if ($host !~ feizhaojun\.com) {
        set $redirect_https false;
    }
    if ($redirect_https = true) {
        return 497;
    }
    error_page 497 https://$host$request_uri;
    
    # ----------
    
    # 设置代理
    location / {
        # 保留代理之前的 host 包含客户端真实的域名和端口号
        proxy_set_header Host $host;
        proxy_set_header Host $http_host;
        # 保留代理之前的真实客户端ip
        proxy_set_header X-Real-IP $remote_addr;
        # 这个Header和X-Real-IP类似,但它在多级代理时会包含真实客户端及中间每个代理服务器的IP
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
        # 表示客户端真实的协议(http还是https)
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Nginx-Proxy true;
        # 指定修改被代理服务器返回的响应头中的 location 头域跟 refresh 头域数值
        # 如果使用"default"参数,将根据 location 和 proxy_pass 参数的设置来决定。
        proxy_redirect [ default|off|redirect replacement ];
        proxy_pass http://mukti_app;
    }
    
    # 需要配置相应的 server upstream
    
    # 设置代理,避免DNS缓存
    location / {
        set $mukti mukti.site:12368;
        proxy_pass http://$mukti;
    }
    
    # 转发子目录
    location /static {
        alias /neworiental/web/op-web_test/;
    }
}

upstream mukti_app {
    server mukti.site:12345;
}

阿里云

初始化

阿里云安全组允许22、21、3306端口

启动网络(如果必要)

systemctl start network

防火墙设置

启用ssh

systemctl start sshd

挂载磁盘

  1. 查看有哪些磁盘:fdisk -l

  2. 挂载:mount /dev/xvdb1 /mnt

  3. 开机自动挂载:vi /etc/fstab

    /dev/vdb1 /storage ext3 defaults 1 2

LNMP一键安装

  • https://lnmp.org/install.html

Nginx 配置

cd /usr/local/nginx/conf/vhost/*.conf

MySQL 配置

cd /etc/my.cnf

datadir = /mnt/mysql
# 如果有必要,记得修改 mysql 文件夹权限 chown mysql:mysql -R ./*

PHP 配置

启用 PHP 错误信息只需要修改 php.ini,重启 lnmp php-fpm restart

cd /usr/local/php/etc/php.ini

添加 PHP 模块

# 查看已安装模块
/usr/local/php/bin/php -m

LNMP 1.4上如果不想用防跨目录

vi /usr/local/nginx/conf/fastcgi.conf

# 删除

fastcgi\_param PHP\_ADMIN\_VALUE "open\_basedir=\$document\_root/:/tmp/:/proc/";

# 需要重启nginx。

# 注意删除其他虚拟主机下面错误的 .user.ini

wordpress 主题需要在 php.ini 允许 scandir() 函数

安装pureftpd

~/lnmp1.4/pureftpd.sh

https://lnmp.org/faq/ftpserver.html

lnmp ftp add
lnmp pureftpd start

其他

421: https://www.kkpan.com/article/3160.html

[26-Oct-2018 02:19:43] NOTICE: [pool www] ‘user’ directive is ignored when FPM is not running as root [26-Oct-2018 02:19:43] NOTICE: [pool www] ‘group’ directive is ignored when FPM is not running as root

查找到 www.conf 把 user = _www 注视前面加 ; eg ; user = _www 把 group = _www 注视前面加 ; eg ; group = _www

我的文件目录在: /usr/local/etc/php/7.2/php-fpm.d/www.conf

1、查看php-fpm是否开启 ps -ef|grep php

2、查看php-fpm的位置 whereis php-fpm

3、开启php-fpm /usr/local/bin/php-fpm

4、再次输入命令 ps -ef|grep php,查看是否已经开启

查看php-fpm进程数 ps aux | grep -c php-fpm

重启php-fpm /etc/init.d/php-fpm restart

php-fpm 关闭:

kill -INT cat /var/run/php-fpm/php-fpm.pid php-fpm 重启:

kill -USR2 cat /var/run/php-fpm/php-fpm.pid