在腾讯云 Ubuntu 16.04 LST 系统中搭建 LNMP 环境

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_42451060/article/details/81057943

安装 PHP

sudo apt-get install php7.0-cli php7.0-cgi

安装 MySQL

sudo apt-get install mysql-server php7.0-mysql

安装过程中会提示填写数据库的密码

安装 FastCgi

sudo apt install spawn-fcgi

安装 Nginx

sudo apt-get install nginx

Nginx 配置文件

修改默认配置文件

vim /etc/nginx/sites-available/default

参考以下配置进行修改:

server {

    # 定义使用 www.baidu.com 访问
    server_name www.baidu.com;

    # 侦听80端口
    listen 80;
    listen [::]:80;

    # 设定本虚拟主机的访问日志和错误日志
    access_log /var/log/nginx/www.baidu.com.access.log;
    error_log /var/log/nginx/www.baidu.com.error.log;

    # 定义服务器的默认网站根目录位置
    root /var/www/html;

    # 定义首页索引文件的名称
    index index.php index.html index.htm index.nginx-debian.html;

    # 默认请求
    location / {
        # 如果使用的是 Laravel 等单一入口框架程序使用下面配置
        # try_files $uri $uri/ /index.php?$query_string;

        try_files $uri $uri/ =404;
    }

    # PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置.
    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

Nginx 文件结构

  • /etc/nginx 所有的配置文件目录
  • /etc/nginx/sites-available 虚拟主机的配置文件目录
  • /usr/sbin/nginx 程序文件目录
  • /var/log/nginx 日志文件目录
  • /etc/init.d/nginx Nginx启动脚本
  • /var/www/nginx-default 默认虚拟主机目录

启动服务

# 重启 Nginx
/etc/init.d/nginx restart

# 启动 fastcgi php
spawn-fcgi -a 127.0.0.1 -p 9000 -C 10 -u www-data -f /usr/bin/php-cgi

创建测试文件

sudo vim /var/www/html/info.php

输入以下内容:

<?php phpinfo(); ?>

在浏览器中打开 http://服务器ip或域名/info.php

如果打开后直接下载 php 文件,请确认 fastcgi 是否已经运行、nginx 是否重启成功。

懒人专属

sudo apt-get install php7.0-cli php7.0-cgi mysql-server php7.0-mysql spawn-fcgi nginx

PS :本文是在腾讯云服务器上进行测试的,不同的服务器在配置过程中肯定会有不同的问题,欢迎反馈。

猜你喜欢

转载自blog.csdn.net/qq_42451060/article/details/81057943