LNMP环境搭建

工作流

在这里插入图片描述

  1. 用户访问域名网站,DNS解析成对应的IP
  2. IP:80 端口,访问到WebServer Nginx,如果是静态目录,Nginx直接将请求返回给用户端
  3. 如果是动态PHP文件,Nginx会将PHP文件转发给PHP-FPH PHP核心解析器,同时,解析器对PHP代码进行解析,如果涉及到数据库操作类的动作,会和数据库作交互。 最后将解析后的PHP代码返回给Nginx,Nginx最后将结果再呈现给用户。

环境部署

安装Nginx

#1)安装nginx
wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum -y install nginx
systemctl start nginx
systemctl enable nginx
systemctl status nginx.service

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qWBRrqjk-1643608857981)(images/image-20220131131030823.png)]

安装MySql

#2)安装MySql
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql57-community-release-el7-10.noarch.rpm
yum makecache
yum -y install mysql-community-server
systemctl start mysqld
systemctl enable mysqld
systemctl status mysqld.service
    # 执行以下命令,获取安装MySQL时自动设置的root用户密码。
    grep 'temporary password' /var/log/mysqld.log
	#回显如下类似信息。
#加固MySql并且设置安全策略
mysql_secure_installation

           
            [ host201:~ ]# mysql_secure_installation

            Securing the MySQL server deployment.

            Enter password for user root:

            The existing password for the user account root has expired. Please set a new password.

                        New password:

            Re-enter new password:
            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
            Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : 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
            Success.


            Normally, root should only be allowed to connect from
            'localhost'. This ensures that someone cannot guess at
            the root password from the network.

            Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n

             ... skipping.
            By default, MySQL comes with a database named 'test' that
            anyone can access. This is also intended only for testing,
            and should be removed before moving into a production
            environment.


            Remove test database and access to it? (Press y|Y for Yes, any other key for No) : n

             ... skipping.
            Reloading the privilege tables will ensure that all changes
            made so far will take effect immediately.

            Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
            Success.

            All done!
#数据库登录	
	 mysql -uroot -proot123#
	 mysq	-h 192.168.3.50 -P 3306 -uroot -proot

安装MySql

#3 安装PHP
rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum -y install php70w-tidy php70w-common php70w-devel php70w-pdo php70w-mysql php70w-gd php70w-ldap php70w-mbstring php70w-mcrypt php70w-fpm
php -v

systemctl start php-fpm
systemctl enable php-fpm

修改nginx 配置,让webserver nginx 能识别index.php文件,从而将php动态代码转发给PHP-FPM PHP解析器能

# 4 修改nginx 配置,让webserver nginx 能识别index.php文件,从而将php动态代码转发给PHP-FPM  PHP解析器能
vim /etc/nginx/nginx.conf

[ host201:/etc/nginx ]# cat nginx.conf

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


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

    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;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

[ host201:/etc/nginx/conf.d ]# cat nginx.conf  | grep -Ev '^$|#'
server {
    listen       80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    location ~ \.php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

在这里插入图片描述

编写PHP代码测试验证

在这里插入图片描述

在这里插入图片描述

FAQ

Nginx配置文件中,/etc/nginx/nginx.conf中有且只有一个http配置项目,一个http可以对应多个Server,server 文件可以配置在该文件中,也可以配置在/etc/nginx/conf.d/*.conf下面,文件以.conf结尾。

参考资料

手工搭建LNMP环境(CentOS7.2 PHP7.0)

linux运维项目-百万电商系统从0到1架构设计

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

yolo2016

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

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

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

打赏作者

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

抵扣说明:

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

余额充值