Docker安裝LNMP-ubuntu18.04+nginx1.14+mysql5.7+php7.3

Pumpkin發表於2021-04-02

拉取ubuntu18.04映象

docker pull ubuntu18.04

啟動ubuntu容器

docker run -it --name="php7.3" --privileged=true ubuntu:18.04 /bin/bash

ubuntu更換阿里雲源

備份源

mv /etc/apt/sources.list /etc/apt/sources.list.bak

更換源

    echo -e "deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe \n
    deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse \n
    deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse \n
    deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse \n
    deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse \n
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse \n
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse \n
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse \n
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse \n
    deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse" >> /etc/apt/sources.list

更新源

apt-get update

安裝常用的軟體

軟體安裝過程中遇到:Do you want to continue? [Y/n]統一輸入y然後回車

vim(必須,且必須會使用下面四個命令)

apt-get install -y vim

需要掌握的vim命令

開啟檔案:vim 檔名

輸入內容:開啟檔案後直接按i

退出編輯狀態:按esc

關閉並儲存檔案:先退出編輯狀態,然後按shift+:輸入wq然後回車

curl(可選)

apt-get install -y curl

安裝wget(可選)

apt-get install -y wget

安裝niginx

apt-get install -y nginx

啟動nginx

service nginx start

配置nginx

建立配置目錄

mkdir -p /data/nginx/conf

建立存放網站程式碼目錄

mkdir -p /data/nginx/code

給目錄新增許可權

chown :www-data -R /data/nginx

修改nginx配置,將建立的配置目錄載入到配置中

vim /etc/nginx/nginx.conf

在http配置中新增 include /data/nginx/conf/*;

在http配置中新增

測試新增一個網站

建立配置檔案

vim /data/nginx/conf/test.net

新增內容,儲存退出

server {
    listen 80;
    server_name test.net;
    index index.html index.htm;
    root /data/nginx/code/test.net;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ /\.ht {
        deny all;
    }
}

建立程式碼檔案

mkdir  /data/nginx/code/test.net
vim /data/nginx/code/test.net/index.html

新增內容,儲存退出

<html>
    <head>
        <title>test.com</title>
    </head>
    <body>
        test build web success!
    </body>
</html>

html檔案

重啟nginx

service nginx restart

修改本機hosts

vim /etc/hosts

在檔案末尾新增 127.0.0.1 test.net ,儲存退出

修改本機hosts

測試訪問

#此操作需要安裝curl
curl test.net

curl輸出結果

至此nginx安裝完成

新增PPA源支援

apt-get install software-properties-common

源新增php

add-apt-repository ppa:ondrej/php

更新源

apt-get update

安裝PHP

安裝php7.3

apt-get install php7.3-fpm

選擇時區步驟一:輸入6 + 回車

選擇時區步驟一

選擇時區步驟二:輸入70 + 回車

選擇時區步驟二

安裝php擴充套件

當前命令安裝了全部擴充套件,可以按需安裝

apt-get install php7.3-bz2 php7.3-curl php7.3-fileinfo php7.3-gd php7.3-gettext php7.3-gmp php7.3-intl php7.3-imap php7.3-interbase php7.3-ldap php7.3-mbstring php7.3-exif php7.3-mysqli php7.3-odbc php7.3-pgsql php7.3-shmop php7.3-snmp php7.3-soap php7.3-sockets php7.3-sqlite3 php7.3-tidy php7.3-xmlrpc php7.3-xsl php7.3-redis php7.3-pdo

驗證PHP是否安裝成功

php -v

php版本資訊

可以輸入php -m 檢視已安裝擴充套件

安裝mysql

apt-get install mysql-server-5.7

啟動mysql服務

service mysql start

檢視mysql賬戶密碼

cat /etc/mysql/debian.cnf

使用賬號密碼登陸

mysql -h localhost -u debian-sys-maint -p

修改root密碼

use mysql;
update user set authentication_string=PASSWORD("root") where user='root';
update user set plugin="mysql_native_password";
flush privileges;

修改資料庫編碼格式

vim /etc/mysql/mysql.conf.d/mysqld.cnf
#在檔案末尾新增
character_set_server=utf8
collation_server=utf8_general_ci
vim /etc/mysql/my.cnf
#在檔案末尾新增
[mysql]
default-character-set=utf8

重啟mysql服務

service mysql restart

開啟遠端訪問

#連線mysql
mysql -u root -p
#選擇資料庫
use mysql;
#開啟遠端授權
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
#重新整理許可權
FLUSH PRIVILEGES; 
#退出
exit

修改配置檔案

vim /etc/mysql/mysql.conf.d
#將bind-address註釋掉
#重啟mysql
service mysql restart

啟動報錯No directory, logging in with HOME=/

usermod -d /var/lib/mysql/ mysql

ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock

chown -R mysql:mysql /var/lib/mysql

解決時間戳格式報錯

vim /etc/mysql/mysql.conf.d/mysqld.cnf

在檔案末尾新增

sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

啟動php-fpm

service php7.3-fpm start

test.net站點的nginx配置修改

server {
    listen 80;
    server_name test.net;
    index index.php index.html index.htm;
    root /data/nginx/code/test.net;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #主要程式碼
        fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~ /\.ht {
        deny all;
    }
}

站點新增php檔案

vim /data/nginx/code/test.net/index.php

檔案內容

<?php
    phpinfo();
#下載安裝檔案
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

#驗證安裝檔案
php -r "if (hash_file('sha384', 'composer-setup.php') === '756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

#下載composer
php composer-setup.php --install-dir=/usr/local/bin --filename=composer

#刪除安裝檔案
php -r "unlink('/usr/local/bin/composer-setup.php');"

驗證composer:composer -v

apt-get install git
#...
curl -sL https://deb.nodesource.com/setup_14.x | bash -
#更新源
apt-get update
#安裝nodejs
apt-get install -y nodejs

安裝

#新增源
add-apt-repository ppa:redislabs/redis
#更新源
apt-get update
#安裝
apt-get install redis

啟動

service redis-server start

連線redis

redis-cli

redis開啟遠端訪問

vim /etc/redis/redis.conf

註釋掉bind行,在行頭新增#
protected-mode 修改為no

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章