LNMP 环境搭建

安装 Nginx :

yum install nginx

配置 PHP 安装源 :

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

安装 PHP7.2 ( * 号表示安装所有有关 PHP7.2 的,扩展之类的, --exclude 排除有冲突的 mysql 与 mysqlnd 这两个PHP扩展有冲突)

yum install php72w*  --exclude php72w-mysqlnd

配置 MariaDB ( Mysql 分支版本 ) yum 安装源

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

# MariaDB 10.0 CentOS repository list - created 2014-10-13 13:04 UTC
# http://mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://mirrors.aliyun.com/mariadb/yum/10.3/centos7-amd64/
gpgkey =  http://mirrors.aliyun.com/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck=1

:wq # 保存退出

安装 MariaDB

yum install MariaDB-server MariaDB-client

设置 Nginx MariaDB PHP-FPM 开机启动

systemctl enable nginx
systemctl enable mariadb
systemctl enable php-fpm

开启 Nginx MariaDB PHP-FPM

systemctl start nginx
systemctl start mariadb
systemctl start php-fpm

配置 MariaDB (设置数据库密码等)

mysql_secure_installation

配置Nginx 与 PHP-FPM 之间通信(这里采用 tcp socket 方式; unix socket 方式性能会更好,该 nginx 和 php-fpm 必须在同一服务器下)

netstat -tunlp # 查看 php-fpm 端口号
vi /etc/nginx/php-fpm.conf
location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000; # PHP-FPM IP:端口号
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
}

网站域名配置

vi /etc/nginx/conf.d/example.conf
server
    {
        listen 80;
        #listen 443 ssl http2; 

        # server_name 域名;

        index index.html index.htm index.php default.html default.htm default.php;
        # root  网站根目录;

        #error_page   404   /404.html;

        # Deny access to PHP files in specific directory
        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

        location / { # PHP 框架 ThinkPHP5.0 重写规则
            if (!-e $request_filename) {
                rewrite  ^(.*)$  /index.php?s=/$1  last;
                break;
            }
        }

        include php-fpm.conf; # 引入FPM配置

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /.well-known {
            allow all;
        }

        location ~ /\.
        {
            deny all;
        }

      access_log  off;

      # ssl_certificate     公钥文件路径;
      # ssl_certificate_key 私钥文件路径;
}

发表评论