快速部署LEMP

254 阅读1分钟

这是我参与8月更文挑战的第1天,活动详情查看:8月更文挑战

此文为在CentOS7上通过Yum快速安装Nginx、PHP、MySQL或MariaDB,方便以后查阅。

创建虚拟机

使用vagrant创建虚拟机

$ mkdir lemp && cd lemp
$ vagrant init centos7
$ vagrant up
$ vagrant ssh

查看系统版本

$ cat /etc/redhat-release
CentOS Linux release 7.1.1503 (Core)

检查SELinux

$ sudo sestatus
$ sudo setenforce 0

Nginx

CentOS源更新比较慢,所以安装Nginx前,需要安装Fedora的epel

$ wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm
$ sudo rpm -ivh epel-release-7-9.noarch.rpm
or
$ sudo yum install epel-release

搜索Nginx

$ yum search nginx
nginx.x86_64 : A high performance web server and reverse proxy server

下面安装Nginx并开启服务运行,CentOS7使用Systemd管理服务,需要使用systemctl开启Nginx服务和查看状态等。

$ sudo yum install nginx
$ sudo systemctl enable nginx.service
$ sudo systemctl start nginx.service
$ sudo systemctl status nginx.service

查看默认页

$ curl localhost

PHP

为了安装高版本PHP,需要安装webtatic-release.rpm

$ sudo rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
$ yum search php71

安装PHP及相关扩展库并启用php-fpm

$ sudo yum install php71w-common php71w-cli php71w-fpm \
                   php71w-mysqlnd php71w-pdo \
                   php71w-mbstring php71w-mcrypt \
                   php71w-curl php71w-gd php71w-pecl-redis
$ sudo systemctl enable php-fpm
$ sudo systemctl start php-fpm
$ sudo systemctl status php-fpm

编辑/etc/php.ini

cgi.fix_pathinfo=0

添加虚拟机

$ mkdir /data/www/
$ vi /data/www/index.php
<?php 
    phpinfo();
?>

编辑 /etc/nginx/conf.d/demo.conf

server {
    listen       80 default_server;
    server_name  localhost;
    root         /data/www;

    access_log /data/log/nginx/demo-access.log;
    error_log  /data/log/nginx/demo-error.log error;

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    charset utf-8;
    index index.html index.htm index.php;
    include /etc/nginx/default.d/*.conf;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi.conf;
    }
}

重启Nginx

$ sudo service nginx reload

如果启动出错可以sudo systemctl status nginx查看原因,大多数是目录权限问题。

MySQL(Mariadb)

安装Mariadb并启动服务进行初始化安全设置

$ sudo yum install mariadb-server
$ sudo systemctl enable mariadb
$ sudo systemctl start mariadb
$ sudo systemctl status mariadb

$ sudo mysql_secure_installation

Done

至此快速部署LEMP完成。