此教程適用於初學LNMP的菜鳥...
運行環境
- VmWare16虛擬機
- centos7 最小安裝版本
- 具體環境安裝包,見百度雲盤 鏈接:https://pan.baidu.com/s/1y0ueGT6Fh_1gSmsSbKAgfg
- 提取碼:iabw
Linux虛擬機配置*注意事項
1.安裝centos鏡像時虛擬機網絡配置為橋接
2.安裝完成后 配置網絡 (ifcfg-ens33 可能為其他名字 不一定為ens33)
vi /etc/sysconfig/network-scripts/ifcfg-ens33
3.修改 ONBOOT=yes 表示啟用該配置
ONBOOT=yes
4.啟動網絡
service network start
5.使用ip addr 查看當前ip地址
安裝NGINX
1.安裝EPEL和lsof插件
yum -y install epel-release lsof
2.安裝Nginx
yum -y install nginx
3.啟動nginx
service nginx start
4.關閉防火牆
systemctl stop firewalld
5.開機關閉防火牆,開機自啟動nginx
systemctl enable nginx
systemctl disable firewalld
現在使用ip addr 查看自己的ip地址
可以看到內網的ip地址是:192.168.13.157
瀏覽器中打開 http://192.168.13.157 可以發現Nginx的歡迎頁面,說明nginx服務器已經部署成功了!
安裝php7.4
1.升級yum rpm 包
yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
2.查看 yum源中的php相關包(依賴組件---非必要)
yum search php74
3.安裝 php7.4 及一些必要依賴
yum -y install php74-php-gd php74-php-pdo php74-php-mbstring php74-php-cli php74-php-fpm php74-php-mysqlnd
4.啟動 php-fpm 服務 (用於開啟fastapi接口服務)
service php74-php-fpm start
5.設置開機啟動 php-fpm 服務
systemctl enable php74-php-fpm
整合Nginx 和 PHP 使nginx可以解析php文件
1.修改nginx 配置文件:
vi /etc/nginx/nginx.conf
將其中的 server 修改為以下信息
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
index index.php index.html index.htm;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
location ~ .php$ {
try_files $uri =404;
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.conf;
}
}
2.在網站目錄下創建info.php
vi /usr/share/nginx/html/info.php
在info.php中添加內容
<?php phpinfo(); ?>
3.重啟Nginx服務器 使配置生效
service nginx restart
4.訪問 http://192.168.13.157/info.php 你會發現nginx可以解析php文件了!
mysql5.6 安裝
1.更新 yum 源
yum install http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
2.安裝mysql
yum -y install mysql-community-server
3.啟動mysql 設置開機啟動
systemctl start mysqld
systemctl enable mysqld
4.進入mysql 設置密碼(剛安裝的mysql 是沒有密碼的)
# mysql -uroot
# set password for 'root'@'localhost' = password('root');
# exit;
測試php-MySQL 連通性
1.在網站目錄下創建 mysql.php
vi /usr/share/nginx/html/mysql.php
2.在文件中輸入測試語句
<?php
$link = mysqli_connect('localhost', 'root', 'root');
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($link);
?>
3.訪問 http://192.168.13.157/mysql.php
-
如果看到 Connected successfully 說明mysql已經鏈接成功!
-
如果看到 Could not connect: xxxx 說明mysql 出現錯誤需要繼續調試