LNMP 完整的搭建过程( CentOS 7、Nginx1.12、MySQL5.7、PHP7)

步骤一:准备编译环境

  1. 使用SSH密钥对连接Linux实例使用用户名密码验证连接Linux实例
  2. 关闭防火墙。
    1. 输入 systemctl status firewalld命令查看当前防火墙的状态。

    2. 如果防火墙的状态参数是inactive,则防火墙为关闭状态, 可跳过此步骤。如果防火墙的状态参数是active,则防火墙为开启状态。如上图所示,此处防火墙为开启状态,需要运行如下命令关闭防火墙:
      • 如果您想临时关闭防火墙,输入命令systemctl stop firewalld
      • 如果您想永久关闭防火墙,输入命令systemctl disable firewalld

步骤二:安装Nginx

  1. 运行以下命令安装Nginx。
    yum -y install nginx
  2. 运行以下命令查看Nginx版本。
    nginx -v                            
    返回结果如下所示,表示Nginx安装成功。
    nginx version: nginx/1.12.2                            

步骤三:安装MySQL

  1. 运行以下命令更新YUM源。
    rpm -Uvh  http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
  2. 运行以下命令安装MySQL。
    yum -y install mysql-community-server
  3. 运行以下命令查看MySQL版本号。
    mysql -V
    返回结果如下,表示MySQL安装成功。
    mysql  Ver 14.14 Distrib 5.7.25, for Linux (x86_64) using  EditLine wrapper

步骤四:安装PHP

  1. 依次运行以下命令更新YUM源。
    yum install -y http://dl.iuscommunity.org/pub/ius/stable/CentOS/7/x86_64/ius-release-1.0-15.ius.centos7.noarch.rpm
    rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

    说明 本教程以 ius-release-1.0-15.ius.centos7.noarch.rpm版本为例。实际安装过程中,请您使用最新版本ius-release软件包。

  2. 运行以下命令安装PHP。
    yum -y install php70w-devel php70w.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-gd.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64 php70w-mcrypt.x86_64  php70w-pdo.x86_64   php70w-mysqlnd  php70w-fpm php70w-opcache php70w-pecl-redis php70w-pecl-mongo
  3. 运行以下命令查看PHP版本。
    php -v
    返回结果如下所示,表示安装成功。
    PHP 7.0.33 (cli) (built: Dec  6 2018 22:30:44) ( NTS )
    Copyright (c) 1997-2017 The PHP Group
    Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
        with Zend OPcache v7.0.33, Copyright (c) 1999-2017, by Zend Technologies                

步骤五:配置Nginx

  1. 运行以下命令备份Nginx配置文件。
    cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
  2. 运行以下命令打开Nginx配置文件。
    vim /etc/nginx/nginx.conf
  3. i进入编辑模式。
  4. 在server大括号内,添加下列配置信息,使Nginx支持PHP请求。
            location / {
                index index.php index.html index.htm;
            }
            #配置Nginx通过fastcgi方式处理您的PHP请求
            location ~ .php$ {
                root /usr/share/php;
                fastcgi_pass 127.0.0.1:9000; #Nginx通过本机的9000端口将PHP请求转发给PHP-FPM进行处理。
                fastcgi_index index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include fastcgi_params;  #Nginx调用fastcgi接口处理PHP请求
            }                

    说明 若不添加此配置信息,则会导致Nginx无法处理您的PHP请求,即您请求的PHP页面将无法打开。

  5. 运行以下命令启动Nginx服务。
    systemctl start nginx
  6. 运行以下命令添加Nginx服务开机自启动。
    systemctl enable nginx

步骤六:配置MySQL

  1. 运行以下命令启动MySQL服务。
    systemctl start mysqld
  2. 运行以下命令设置MySQL服务开机自启动。
    systemctl enable mysqld
  3. 运行以下命令查看/var/log/mysqld.log文件,获取并记录root用户的初始密码。
    # grep 'temporary password' /var/log/mysqld.log
    2016-12-13T14:57:47.535748Z 1 [Note] A temporary password is generated for root@localhost: p0/G28g>lsHD

    说明 下一步重置root用户密码时,会使用该初始密码。

  4. 运行下列命令配置MySQL的安全性。
    mysql_secure_installation
    安全性的配置包含以下五个方面:
    1. 重置root账号密码。
      Enter password for user root: #输入上一步获取的root用户初始密码
      The 'validate_password' plugin is installed on the server.
      The subsequent steps will run with the existing configuration of the plugin.
      Using existing password for root.
      Estimated strength of the password: 100 
      Change the password for root ? ((Press y|Y for Yes, any other key for No) : Y #是否更改root用户密码,输入Y
      New password: #输入新密码,长度为8至30个字符,必须同时包含大小写英文字母、数字和特殊符号。特殊符号可以是()` ~!@#$%^&*-+=|{}[]:;‘<>,.?/
      Re-enter new password: #再次输入新密码
      Estimated strength of the password: 100 
      Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y
    2. 输入Y删除匿名用户账号。
      By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment.
      Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y  #是否删除匿名用户,输入Y
      Success.
    3. 输入Y禁止root账号远程登录。
      Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y #禁止root远程登录,输入Y
      Success.
    4. 输入Y删除test库以及对test库的访问权限。
      Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y #是否删除test库和对它的访问权限,输入Y
      - Dropping test database...
      Success.
    5. 输入Y重新加载授权表。
      Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y #是否重新加载授权表,输入Y
      Success.
      All done!

    更多详情,请参见官方文档

步骤七:配置PHP

  1. 在/usr/share/php目录下新建phpinfo.php文件,用于展示phpinfo信息。具体步骤如下:
    1. 运行vim /usr/share/php/phpinfo.php命令打开文件。
    2. i进入编辑模式。
    3. 输入下列内容。
      <?php echo phpinfo(); ?>
    4. :wq保存并退出。
  2. 运行以下命令启动PHP-FPM。
    systemctl start php-fpm
  3. 运行以下命令设置PHP-FPM开机自启动。
    systemctl enable php-fpm

步骤八:测试访问

  1. 打开浏览器。
  2. 在地址栏输入http://<ECS实例公网IP地址>/phpinfo.php

    返回结果如下图所示,表示LNMP环境部署成功。

下一步

测试访问LNMP平台成功后,建议您运行以下命令将 /usr/share/php/phpinfo.php文件删除,以避免安全隐患。

rm -rf /usr/share/php/phpinfo.php

附一:多站点配置

以上步骤完成之后,配置域名解析到 公网IP 即可打开 /usr/share/php/ 项目,如果需要新增配置 子域名 映射到  /usr/share/php/demo 项目,实现多站点配置

打开和 nginx.conf 同级的目录 /nginx/conf.d,创建任意名称的配置文件:

demo.conf 

server {
        listen       80;
        listen       [::]:80;
        server_name  demo.kder.top;
        root         /usr/share/php/demo;
        location / {
            index index.php admin.php index.html index.htm;
        }

        location ~ \.php(.*)$ {
            fastcgi_pass 127.0.0.1:9000; 
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            # 要开启 phpinfo 模式
            # 即去掉 php.ini 中 ;cgi.fix_pathinfo=1 的注释
            fastcgi_param PATH_INFO $1;
            include fastcgi_params;
        }    
            
}

重启nginx服务:

systemctl restart nginx

访问 demo.kder.top 即可直接打开  /usr/share/php/demo 的项目,如需要配置其它项目,只需复制和修改 server_name  root 对应的域名和文件目录即可。

附二:配置 SSL

这里需要到平台申请并下载 .pem证书文件 和 .key私钥文件 存放在指定位置,然后修改配置如下:


# http 自动跳转 https
server {
    listen 80;
    server_name ik.kder.top;
    rewrite ^(.*) https://$server_name$1 permanent;
}

server {
        listen       443 ssl;
        server_name  ik.kder.top;

        ssl_certificate      xxx._ik.kder.top.pem;
        ssl_certificate_key  xxx._ik.kder.top.key;


        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_session_timeout  5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_prefer_server_ciphers  on;

        root         /usr/share/php/ik/public;
        location / {
            index index.php admin.php index.html index.htm;
        }

        # 兼容入口文件 admin.php
        if (!-e $request_filename) {
            rewrite /admin.php(.*)$ /admin.php$1 last;
            rewrite ^(.*)$ /index.php/$1;
            break;
        }

        location ~ \.php(.*)$ {
            fastcgi_pass 127.0.0.1:9000; 
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            # 要开启 phpinfo 模式
            # 即去掉 php.ini 中 ;cgi.fix_pathinfo=1 的注释
            fastcgi_param PATH_INFO $1;
            include fastcgi_params;
        }
}

 

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值