不败君

前端萌新&初级后端攻城狮

Centos7安装LNMP环境

Centos7安装LNMP环境

2019-09-17 18:48:09

围观(3031)

一.首先搭建 Nginx 服务

使用 yum 安装 Nginx:

yum install nginx -y

修改 /etc/nginx/conf.d/default.conf,去除对 IPv6 地址的监听:

如果 conf.d 里面没有 default.conf 则修改 nginx 目录的nginx.conf

server {
    listen       80 default_server;
    # listen       [::]:80 default_server;
    server_name  _;
    root         /usr/share/nginx/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }

}

启动 Nginx:

nginx

此时,可访问机器外网 IP 地址来确认是否已经安装成功。

将 Nginx 设置为开机启动:

chkconfig nginx on


二.安装 MySQL 数据库服务

下载 yum 及 安装 yum 源:

wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm

如果需要 MySQL8.0 则执行命令:

wget https://repo.mysql.com/mysql80-community-release-el7-1.noarch.rpm
rpm -ivh mysql80-community-release-el7-1.noarch.rpm

使用 yum 安装 MySQL:

yum install mysql-server -y

安装完成后,启动 MySQL 服务:

service mysqld restart

接下来需要为 MySQL 设置一个密码,使用命令查看当前生成的随机密码:

cat /var/log/mysqld.log | grep "generated for root@localhost"

输出结果:

A temporary password is generated for root@localhost: ******

使用此密码登录 MySQL

mysql -u root -p

执行以上登录命令后 会提示输入密码。

修改密码:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'Passbubaijun.com99&&';

注意:密码必须有数字+大写字母+小写字母+字符,不然会提示这个:

ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

密码设置好之后,将 MySQL 设置为开机启动:

chkconfig mysqld on

如果需要查看 MySQL 进程:

ps -A | grep mysql


三.安装 PHP

使用 yum 安装 PHP:

yum install php php-fpm php-mysql -y

安装之后,启动 PHP-FPM 进程:

service php-fpm start

启动之后,可以使用下面的命令查看 PHP-FPM 进程监听哪个端口

netstat -nlpt | grep php-fpm

把 PHP-FPM 也设置成开机自动启动:

chkconfig php-fpm on

注意:以上方法安装的 PHP 版本可能是 5.X 的。如果要7.X版本:

安装 EPEL 和 Webtatic 库

yum install epel-release
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

安装 PHP

yum install -y php70w php70w-mysql php70w-mcrypt php70w-dom php70w-mbstring

安装 PHP 的进程管理器

yum -y install php70w-fpm php70w-opcache

然后可以执行上面的启动 PHP-FPM 命令及开机自启。


四.配置 Nginx 并运行 PHP 程序

在 /etc/nginx/conf.d 目录中新建一个名为 php.conf 的文件,并配置 Nginx 端口 ,配置示例如下:

server {
    listen 8000;
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location / {
        root /usr/share/php;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        index index.html index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

修改配置完成后,重启 Nginx 服务

service nginx restart

如果报错:

Redirecting to /bin/systemctl restart nginx.service

使用以下命令重启 Nginx :

systemctl restart nginx.service

在/usr/share/php 目录下新建一个 info.php 文件来检查 PHP 是否安装成功了,文件内容:

<?php phpinfo();?>

此时,访问机器外网 IP + 8000端口 + /info.php 可浏览到刚刚创建的 info.php 页面。


Nginx常用命令:

systemctl start nginx.service //启用 Nginx
systemctl status nginx.service //查看当前状态
systemctl restart nginx.service //重启服务
nginx -s quit //关闭服务

如果程序连接数据库时报错:

authentication plugin 'caching_sha2_password'

那么需要再次进入到 MySQL 命令行模式并执行:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Passbubaijun.com99&&';

本文的“Passbubaijun.com99&&”为数据库密码,请执行命令时修改。


安装 PHP 后可执行命令检测是否安装成功:

php -v

如果报错,可执行命令:

yum install php70w-cli


如果使用 Laravel 框架,Nginx 配置:

server
{
    listen 8000;
    #server_name bubaijun.com;
    index index.html index.htm index.php default.html default.htm default.php;
    root  /usr/share/php/public;
    location ~ .*.(php|php5)?$
    {
         try_files $uri = 404;
         fastcgi_pass 127.0.0.1:9000;
         fastcgi_index index.php;
         include fastcgi.conf;
     }

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

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

     if (!-d $request_filename)
     {
            rewrite ^/(.+)/$ /$1 permanent;
      }

      # removes trailing "index" from all controllers
     if ($request_uri ~* index/?$)
     {
            rewrite ^/(.*)/index/?$ /$1 permanent;
      }

      # unless the request is for a valid file (image, js, css, etc.), send to bootstrap
      if (!-e $request_filename)
      {
            rewrite ^/(.*)$ /index.php?/$1 last;
            break;
      }
}


本文地址 : bubaijun.com/page.php?id=137

版权声明 : 未经允许禁止转载!

评论:我要评论
发布评论:
Copyright © 不败君 粤ICP备18102917号-1

不败君

首 页 作 品 微 语