linux centos8 搭建LNMP平台(linux+nginx+mysql+php)


LNMP環境和軟件版本
名稱 版本號 查詢命令
linux系統 CentOS Linux release 8.2.2004 (Core) cat /etc/redhat-release
Nginx nginx-1.14.1-9.module_el8.0.0+184+e34fea82.x86_64 rpm -qa | grep nginx
mariadb mariadb-server-10.3.17-1.module_el8.1.0+257+48736ea6.x86_64 rpm -qa | grep mariadb
php php-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64 rpm -qa | grep php
一、安裝Nginx
1、查看是否安裝過nginx。

rpm -qa | grep nginx

2、有就卸載nginx。

yum remove -y "nginx*"

3、重新安裝nginx。

yum install -y nginx

4、查看啟動狀態。

systemctl status nginx

5、啟動nginx。

systemctl start nginx

6、添加開機啟動。

systemctl enable nginx

7、設置防火牆開放tcp80端口。

firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload
firewall-cmd --query-port=80/tcp

8、使用瀏覽器訪問http://192.168.16.144/,顯示如下界面,說明安裝的nginx服務正常運行。

9、由上圖內容可知,默認主頁index.html位於默認目錄/usr/share/nginx/html中。若要發布自己的網站內容,替換index.html主頁內容即可。例:主頁內寫入內容 “This is a Nginx test.” 。使用瀏覽器訪問http://192.168.16.144,則顯示如下界面內容。若要更換主頁目錄位置,需在nginx配置文件/etc/nginx/nginx.conf中設置目錄路徑。
echo "This is a Nginx test." > /usr/share/nginx/html/index.html

二、安裝mysql數據庫
1、查看是否安裝過mariadb。

rpm -qa | grep mariadb

2、有就卸載mariadb。

yum remove -y "mariadb*"

3、重新安裝mariadb-server。

yum install -y mariadb-server

4、啟動mariadb。

systemctl start mariadb

5、查看啟動狀態。

systemctl status mariadb

6、添加開機啟動。

systemctl enable mariadb

7、設置mysql數據庫root賬號密碼。

mysqladmin -uroot password 'yourpassword'

mysql_secure_installation

8、root賬號登陸mysql。

mysql -uroot -p

9、登陸mysql后可以使用如下命令重新設置當前賬戶數據庫密碼。

MariaDB[(none)]> set password=password('123456');

10、創建一個新用戶mysql,密碼為123456,授權遠程計算機使用賬號mysql登陸數據庫,並立刻刷新權限。

MariaDB[(none)]>grant all on . to 'mysql'@'%' identified by '123456';
MariaDB[(none)]>flush privileges;

上述語句表示使用"mysql"賬戶,"123456“”密碼從任何主機連接到mysql服務器,並賦予所有的權限。

11、退出mysql數據庫。

MariaDB[(none)]> quit;

MariaDB[(none)]> exit;

12、設置防火牆開放tcp3306端口。

firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload
firewall-cmd --query-port=3306/tcp

13、遠程計算機連接服務器數據庫時使用如下命令,輸入密碼即可登錄mysql數據庫。

mysql -umysql -p -h 192.168.16.144 -P 3306

三、安裝PHP
1、查看是否安裝過php。

rpm -qa | grep php

2、有就卸載php。

yum remove -y "php*"

3、重新安裝php。

yum install -y php

4、創建文件/usr/share/nginx/html/index.php,寫入內容 “”。

touch /usr/share/nginx/html/index.php
echo "" > /usr/share/nginx/html/index.php

5、重啟nginx服務,使用瀏覽器訪問http://192.168.16.144,如果顯示如下,則說明php安裝成功。

systemctl restart nginx


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM