Centos7下LNMP服務器搭建

LNMP環境搭建

1. nginx安裝

  • 安裝
yum install nginx
  • 啓動
systemctl start nginx
systemctl restart nginx

如果不能安裝,

2.安裝PHP及相關模塊:

  1. 安裝php-fpm:
yum install php-fpm
  1. 安裝完成後配置文件在/etc/php-fpm.conf,配置引用了/etc/php-fpm.d/*.conf,默認有一個www.conf,修改www.conf,找到用戶、用戶組設置:
user = nginx   39行
group = nginx  41行
  1. 修改/etc/php.ini文件:提高安全性能
;cgi.fix_pathinfo=1 修改爲 cgi.fix_pathinfo=0
  1. 啓動服務:
systemctl start php-fpm.service
  1. 配置nginx支持php網頁:vim /etc/nginx/nginx.conf.default找到以下內容:
location ~\.php$ {
           root           html;
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.php,phpinfo.php;
           #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
           fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
           include        fastcgi_params;
       }

將內容複製到nginx.conf中,效果如下:

第一個location是原先就有的,

location / {
           root   /usr/share/nginx/html;
           index  index.html index.htm index.php;
        }
location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

3.安裝mysql數據庫

  • 安裝mariadb:
yum install mariadb mariadb-server mariadb-libs mariadb-devel
  • 檢查是否安裝成功:
rpm -qa |grep maria
  • 開啓mariadb服務,並設置開啓啓動,檢查mysql狀態
systemctl start mariadb
systemctl enable mariadb
systemctl status mariadb
  • 數據庫安全設置
mysql_secure_installation
Enter current password for root (enter for none):  敲回車鍵
Set root password? [Y/n]  按Y鍵
New password: 123456(真實環境中設置複雜密碼)
Re-enter new password: 123456
Remove anonymous users? [Y/n]  n
Disallow root login remotely?[Y/n]  n
Remove test database and access to it? [Y/n] n
Reload privilege tables now? [Y/n] n
  • 數據庫基本操作

    • 登錄數據庫:mysql -u root -p
    MariaDB [(none)]>        mariadb的命令操作提示符
    MariaDB [(none)]> show databases;   查看有哪些數據庫
    [(none)]> use mysql;   選擇數據庫
    MariaDB [mysql]> show tables;  查看數據庫中的表
    MariaDB [mysql]> select * from  表名   查詢表中所有數據
    MariaDB [mysql]> select user,password from 表名  只查詢表中usr與password字段的內容。
    
    • 數據庫備份:
    MariaDB [mysql]> exit  退出
    [root@teacher mnt]# mysqldump couman --user=root --password=123456>db-01.mysql;   將couman數據庫備份到當前目錄下,叫db-01.mysql
    
    • 數據庫的恢復:
    首先創建一個空數據庫存
    MariaDB [(none)]> create database couman;
    Query OK, 1 row affected (0.00 sec)
    
    • 備份文件中恢復數據庫(導入數據庫):
    MariaDB [mysql]> exit  退出
    [root@teacher mnt]# mysql -u root -p couman<db-01.mysql
    Enter password:
    

4.域名發佈網站

  • 在/etc/hosts下添加域名,也可以配置DNS服務器
vim /etc/hosts
添加:
192.168.161.131 www.book.com
192.168.161.131 www.anxing.com
  • 將網站上傳到服務器/data(目錄可以自己制定)目錄下:
mkdir /data
  • 配置nginx.conf訪問多個網站:
vim /etc/nginx/nginx.conf
末尾"}"之前添加:
include vhost/*.conf
  • 添加配置文件:
mkdir /etc/nginx/vhost
cd /etc/nginx/vhost
  • www.book.com網站:
vim book.conf
server{
        listen 192.168.161.131:80;
        server_name www.book.com;
        location / {
                root  /data/book;
                Index  index.html index.php;
}

        location ~ \.php$ {
           root /data/book;
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
           include        fastcgi_params;
       }
}
  • www.anxing.com網站:
vim anxing.com
server{
        listen 192.168.161.131:80;
        server_name www.anxing.com;
        location / {
                root  /data/anxing;
                Index  index.html index.php;
}

        location ~ \.php$ {
           root /data/anxing;
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
           include        fastcgi_params;
       }
}
  • 重啓服務:
systemctl restart nginx
systemctl restart php-fpm
systemctl restart mariadb
  • 這裏關閉防火牆,selinux安全機制:
systemctl stop firewalld
setenforce 0
  • 域名訪問:www.book.com www.anxing.com

配置多個網站,不同域名訪問

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章