如何构建LNMP环境

如何在CentOS中安装 Linux, Nginx, MySQL, PHP (LEMP)?

介绍

一个LEMP软件环境是通常安装在一起,使一台服务器主机动态网站和网络应用的一组开源软件。该术语实际上是其表示的缩写Linux下操作系统,与È Nginx的Web服务器(其取代了LAMP环境的Apache的角色)。该网站的数据存储在一个MySQL数据库(使用MariaDB的),以及动态内容通过处理PHP。

在本教程中,我们将得到一个堆栈LEM​​P安装在CentOS的VPS 7。CentOS的将履行我们的第一个要求:Linux操作系统

前提条件

  • CentOs 主机
  • Root 权限

一.安装Nginx

  1. 添加CentOS的7 EPEL库,打开终端,并使用下面的命令:
    sudo yum install epel-release

  2. 安装Nginx
    sudo yum install nginx

  3. 在VPS启动Nginx
    sudo systemctl start nginx

  4. 验证是否成功
    打开浏览器,输入的的ip
    http://server_domain_name_or_IP/

    您将看到默认的CentOS 7 Nginx的网页,其中有提供信息和测试目的。

    它应该是这个样子:

  5. 成功启动后,设置开机自动启动
    sudo systemctl enable nginx

二.安装Mysql(MariaDB)

MariaDB的是MySQL关系数据库的一个社区开发分支,MySql被Oracl收购后闭源,MySql的开发者拉取其分支继续开源。

  1. 安装MariaDB
    sudo yum install mariadb-server mariadb

  2. 启动MariaDB
    sudo systemctl start mariadb

  3. 运行安全脚本,激活MariaDB
    sudo mysql_secure_installation
    要求您输入当前root密码
    已经刚刚安装MariaDB,因此直接enter
    然后按Y,要你设置密码时,输入你想设置的数据库密码
    mysql_secure_installation prompts:

       Enter current password for root (enter for none):
    
       OK, successfully used password, moving on...
    
       Setting the root password ensures that nobody can log into the MariaDB
    
       root user without the proper authorisation.
    
       New password: 
       password
    
       Re-enter new password: 
       password
    
       Password updated successfully!
    
       Reloading privilege tables..
    
        ... Success!
    

    对于剩余的问题,您应该简单地通过每一个提示,点击“ENTER”键接受默认值。

  4. 设置开机启动MariaDB
    sudo systemctl enable mariadb

三.安装PHP7

1.安装php7

sudo yum install php70u-fpm-nginx php70u-cli php70u-mysqlnd

2.配置PHP

  1. 配置php-fpm
    sudo nano /etc/php-fpm.d/www.conf

    查找包含listen = 127.0.0.1:9000这行,用;注释掉
    并打开listen = /run/php-fpm/www.sock这行

    ; The address on which to accept FastCGI requests.
    
    ; Valid syntaxes are:
    
    ;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific IPv4 address on
    
    ;                            a specific port;
    
    ;   '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
    
    ;                            a specific port;
    
    ;   'port'                 - to listen on a TCP socket to all addresses
    
    ;                            (IPv6 and IPv4-mapped) on a specific port;
    
    ;   '/path/to/unix/socket' - to listen on a unix socket.
    
    ; Note: This value is mandatory.
    
    ; listen = 127.0.0.1:9000 
    
    ; WARNING: If you switch to a unix socket, you have to grant your webserver user
    
    ;          access to that socket by setting listen.acl_users to the webserver user.
    listen = /run/php-fpm/www.sock
    

    退出并保存文件。在nano,你可以通过按做到这一点按Ctrl-X退出,y 确认

  2. 设置listen.acl_users = nginx

    ; When POSIX Access Control Lists are supported you can set them using
    
    ; these options, value is a comma separated list of user/group names.
    
    ; When set, listen.owner and listen.group are ignored
    
    ;listen.acl_users = apache,nginx
    
    ;listen.acl_users = apache
    
    listen.acl_users = nginx
    
    listen.acl_groups = nginx
    

    退出并保存文件。在nano,你可以通过按做到这一点按Ctrl-X退出,y 确认

  3. 重启php-fpm和nginx
    sudo systemctl restart php-fpm sudo systemctl restart nginx

  4. 设置开机启动php-fpm
    sudo systemctl enable php-fpm

  5. 编辑nginx 默认站点配置文件
    sudo nano /etc/nginx/conf.d/default.conf
    寻找开头的块location ~ \.php$ {。在此块中,查找fastcgi_pass指令。注释或删除这条线,并将其替换为fastcgi_pass php-fpm,这将参考中所定义的上php-fpm.conf

      location ~ \.php$ {
    
          try_files $uri =404;
    
          fastcgi_split_path_info ^(.+\.php)(/.+)$;
    #
     fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
    
          fastcgi_pass php-fpm;
    
          fastcgi_index index.php;
    
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    
          include fastcgi_params;
    
      }
    
  6. 重启php-fpm和nginx
    sudo systemctl restart php-fpm sudo systemctl restart nginx

results for ""

    No results matching ""