CentOS--配置LNMP环境记录

CentOS7.2配置LNMP环境记录

CentOS7.2配置LNMP环境记录

php 5.6+ nginx 1.10+ mysql 5.5+

LNMP是Linux、Nginx、MySQL(MariaDB)和PHP的缩写,这个组合是最常见的WEB服务器的运行环境之一。本文将带领大家在CentOS 7操作系统上搭建一套LNMP环境。

本教程适用于CentOS 7.x版本。

准备工作

更新 yum 源,自带的源没有 PHP5.6 :
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
安装 epel:
yum install epel-release
升级系统
yum update

准备工作完成,开始安装!

安装Nginx

CentOS系统模板中配置了内网源,下载速度较快,推荐使用yum安装Nginx:

sudo yum install nginx

按照提示,输入yes后开始安装。安装完毕后,Nginx的配置文件在/etc/nginx目录下。使用以下命令启动Nginx:

sudo systemctl start nginx

检查系统中firewalld防火墙服务是否开启,如果已开启,我们需要修改防火墙配置,开启Nginx外网端口访问。

sudo systemctl status firewalld

如果显示active (running),则需要调整防火墙规则的配置。

修改/etc/firewalld/zones/public.xml文件,在zone一节中增加:

<zone>
    ...
    <service name="nginx"/>
<zone>

保存后重新加载firewalld服务:

sudo systemctl reload firewalld

您可以通过浏览器访问 http://外网IP地址 来确定Nginx是否已经启动。

注意:很重要!!!

部分童鞋发现按照教程操作最后无法访问,这是云服务器默认关闭了80端口。这个请移步云服务里控制台打开80端口的外网访问,如需步骤,请自行百度!

最后将Nginx设置为开机启动:

sudo systemctl enable nginx.service

这么Nginx就安装成功了!

安装MySQL(MariaDB)

MariaDB是MySQL的一个分支,主要由开源社区进行维护和升级,而MySQL被Oracle收购以后,发展较慢。在CentOS 7的软件仓库中,将MySQL更替为了MariaDB。

我们可以使用yum直接安装MariaDB:

sudo yum install mariadb-server

安装完成之后,执行以下命令重启MariaDB服务:

sudo systemctl start mariadb

MariaDB默认root密码为空,我们需要设置一下,执行脚本:

sudo /usr/bin/mysql_secure_installation

这个脚本会经过一些列的交互问答来进行MariaDB的安全设置。

首先提示输入当前的root密码:

  • Enter current password for root (enter for none):
    初始root密码为空,我们直接敲回车进行下一步。
  • Set root password? [Y/n]
    设置root密码,默认选项为Yes,我们直接回车,提示输入密码,在这里设置您的MariaDB的root账户密码。
  • Remove anonymous users? [Y/n] 是否移除匿名用户,默认选项为Yes,建议按默认设置,回车继续。
  • Disallow root login remotely? [Y/n]
    是否禁止root用户远程登录?如果您只在本机内访问MariaDB,建议按默认设置,回车继续。 如果您还有其他云主机需要使用root账号访问该数据库,则需要选择n。
  • Remove test database and access to it? [Y/n] 是否删除测试用的数据库和权限?
    建议按照默认设置,回车继续。
  • Reload privilege tables now? [Y/n]
    是否重新加载权限表?因为我们上面更新了root的密码,这里需要重新加载,回车。

完成后你会看到Success!的提示,MariaDB的安全设置已经完成。我们可以使用以下命令登录MariaDB:

mysql -uroot -p

按提示输入root密码,就会进入MariaDB的交互界面,说明已经安装成功。

最后我们将MariaDB设置为开机启动。

sudo systemctl enable mariadb

安装PHP

我们可以直接使用yum安装PHP:

sudo yum install php56w-fpm php56w-mysql php56w-mysqli php56w php56w-opcache php56w-gd php56w-intl php56w-mbstring php56w-exif php56w-mcrypt php56w-openssl

//把该安装的一次性装到位

安装完成后我们将php-fpm启动:

sudo systemctl start php-fpm

将php-fpm设置为开机启动:

sudo systemctl enable php-fpm

接下来需要注意了!配置Nginx–多个站点

我给大家提供一个范本作为参考:

nginx.conf
//里面我会详细的给予中文注释
vi /etc/nginx/nginx.conf
//编辑nginx.conf的命令

以下为conf文件内容:

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log; #错误日志记录的位置
pid /run/nginx.pid; #nginx.pid为记录nginx主进程pid文件;切勿修改、移动
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
#引入/usr/share/nginx/modules/ 目录下的所有以.conf结尾的文件

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    #这句很重要,引入所有etc/nginx/conf.d/目录下的.conf文件
    
    #***etc/nginx/conf.d/目录存放的就是分站点的文件(下面会给出实例代码)***
    
    server {
        #由于我们的nginx需要配置多站点,所以在此就需要注释一些东西
        
         listen       80 default_server;
         listen       [::]:80 default_server;
        #保留监听的端口  
        # server_name  _;
        # root         /usr/share/nginx/php;

        # 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 {
        }
        # location ~ \.php$ {
        # root           /usr/share/php;
        # fastcgi_pass   127.0.0.1:9000;
        # fastcgi_index  index.php;
        # fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        # include        fastcgi_params;
        # }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # 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.conf可以直接复制了去使用!~好用了就给博主打个赏钱!谢谢!

配置完nginx之后我们该干啥、?当然是重启nginx呗

service nginx start      #启动nginx

service nginx stop       #停止nginx

service nginx restart    #重启nginx

sudo systemctl reload nginx     #或者执行这条

重启完毕,继续打开 http://外网IP地址 来确定Nginx是否已经启动。

此时,服务器启动的是nginx和apache。

而且php-fpm默认发送到apache。

所以咱们还得继续修改一下php-fpm。

配置 php-fpm

vi /etc/php-fpm.d/www.conf    

#编辑php-fpm配置文件

修改user和group (源代码为:user = apache group = apache)

user = nginx    
group = nginx

修改完了之后,还是老样子,重启php-fpm服务

service php-fpm start      #启动php-fpm

service php-fpm stop       #停止php-fpm

service php-fpm restart    #重启php-fpm

最后,咱们需要为nginx添加站点了

添加站点这我先给大家一个截图,以帮助大家迅速的了解是怎么回事

image1.png

大家应该看的很清楚了,猜都可以猜到,博主这一共配置了三个站点,这三个站点是怎么被nginx引入的呢?

我给大家贴出nginx的配置文件的里面应该有这么一句(注意图中的红框,上面的是地址)
include /etc/nginx/conf.d/*.conf;
#这句很重要,引入所有etc/nginx/conf.d/目录下的.conf文件
#***etc/nginx/conf.d/目录存放的就是分站点的文件(下面会给出实例代码)***

好的,大家应该能准确理解了,如果还是理解不了的话只能缺你回去喝点三鹿了!

下面我给大家贴出nginx站点配置文件的代码,修改修改就可以用

看代码的时候请注意看里面的路径,当然我也还是会给一定的中文注释

#这个文件是上面的qopmall.com.conf
server {

    server_name  qopmall.com www.qopmall.com;#这里就是你要绑定的域名了,空格分开
    location / {
            root   /usr/share/php/weixin; #这里是你站点存放的文件夹名称(也就是说,你当前这个站点的文件全部都丢在这个路径的weixin文件夹里面)
            index  index.php index.html index.htm; #这里照抄即可
        }
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /usr/share/php/weixin; #这里的配置等同于上面的那个root配置
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /usr/share/php/weixin/$fastcgi_script_name; #这里的配置也是和上面的root配置一样
        include        fastcgi_params;
    }
}

代码非常简单,我没注释到的不用修改就行。

上面的路径,比如/usr/share/php/weixin 这就是你站点的根目录,我给大家截图参考:
image2.png

各位童鞋,创建好站点了,先写个简单的php程序测试一下是否正常,比如info。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卟败灬筱龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值