Centos 搭建LNMP環境

1、防火牆:php

開啓80端口、3306端口html

2、安裝nginxnode

yum install nginx      #安裝nginx,根據提示,輸入Y安裝便可成功安裝 
    systemctl start nginx  #啓動
    systemctl enable nginx #設爲開啓啓動
    rm -rf /usr/share/nginx/html/*  #刪除ngin默認測試頁、
複製代碼

3、安裝MySQLmysql

#查找存儲庫下載連接 (yum版本,對應紅帽家族) dev.mysql.com/downloads/r…nginx

#添加存儲庫
    wget https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm
    #安裝下載的發行包
    yum install mysql57-community-release-el7-11.noarch.rpm

    ###啓用和禁用 版本(可忽略)###
    yum repolist all | grep mysql
    yum-config-manager --disable mysql57-community
    yum-config-manager --enable mysql56-community
    #查看 啓用版本
    yum repolist enabled | grep mysql
    #############################

    #安裝mysql
    yum install mysql-community-server
    #啓動
    systemctl start mysqld.service
    #檢查狀態
    systemctl status mysqld.service

    #查看初始密碼
    grep "password" /var/log/mysqld.log
    mysql -uroot -p
    #修改密碼(必須包含數字、字母大小寫、特殊符號,且大於8位)
    ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPass1!';
    #刪除MySQL的Repository
    yum -y remove mysql57-community-release-el7-11.noarch.rpm
    #開機啓動
    systemctl enable mysqld.service
複製代碼

4、安裝PHPsql

#安裝php
    yum install php
    #安裝PHP組件,使PHP支持 MySQL、PHP支持FastCGI模式
    yum install php-mysql php-gd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt libmcrypt-devel php-fpm
    #啓動php-fpm
    systemctl start php-fpm
    #設置開機啓動
    systemctl enable php-fpm
複製代碼

5、配置Nginx支持PHP:數據庫

Nginx配置文件vim

vim /etc/nginx/nginx.conf 
複製代碼
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;
    
        server {
            listen       80 default_server;
            listen       [::]:80 default_server;
            server_name  _;
            root         /home/html/;
            index        index.php index.html index.htm;
    
            # Load configuration files for the default server block.
            include /etc/nginx/default.d/*.conf;
    
            location / {
            }
    
            #支持PHP的配置
            location ~ .*\.php(\/.*)*$ {
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
                include        fastcgi_params;
            }
    
            error_page 404 /404.html;
                location = /40x.html {
            }
    
            error_page 500 502 503 504 /50x.html;
                location = /50x.html {
            }
        }
    }
複製代碼

6、測試篇bash

#新建網站首頁
cd /home/html;
vim index.php;
複製代碼
<?php
    #測試數據庫鏈接
    $con = mysql_connect("localhost","root","NewPass1!");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }else{
            echo "Mysql connected!";
    }
    #打印php信息
    phpinfo();
    ?>
複製代碼
相關文章
相關標籤/搜索