使用 Yum 安裝必要軟件
注:CentOS 使用Yum 安裝軟件
1.登錄云服務(wù)器后,默認(rèn)已獲取 root 權(quán)限。在 root 權(quán)限下,通過以下命令,先將必要軟件一起安裝 (Nginx、MySQL、PHP):
yum install nginx php php-fpm php-mysql mysql-server -y
安裝完成,PuTTY 窗口會提示“Complete!”。同時可以上滑滾動條查看當(dāng)前安裝包版本
更多詳細(xì)操作,可參考 CentOS 環(huán)境下通過 Yum 安裝軟件。
軟件配置
將 Nginx、MySQL、PHP 等各軟件安裝好之后,還需要對各軟件分別進(jìn)行配置。以下是詳細(xì)步驟:
2.3.1 配置 Nginx
- 請使用 Vim 命令打開
default.conf
文件,取消對 IPv6 地址的監(jiān)聽同時配置 Nginx,實現(xiàn)與 PHP 的聯(lián)動。vim /etc/nginx/conf.d/default.conf
- 按字母“I”鍵或 “Insert” 鍵切換至編輯模式,將已有內(nèi)容全部清除,復(fù)制并粘貼以下內(nèi)容到
default.conf
文件。
server {
listen 80;
root /usr/share/nginx/html;
server_name localhost; #地址
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
index index.php index.html index.htm; ##啟動頁
}
#error_page 404 /404.html;
#redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
#pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
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;
}
}
修改完成后,按 “Esc” 鍵,輸入 “:wq”,保存文件并返回。
- 啟動 Nginx。
service nginx start
-
測試 Nginx 服務(wù)是否正常運行
在瀏覽器中,訪問 CentOS 云主機(jī)公網(wǎng) IP,查看 Nginx 服務(wù)是否正常運行。
顯示如下,則說明 Nginx 安裝配置成功:
測試Nginx2
2.3.2 配置 MySQL
-
啟動 MySQL 服務(wù)器。
service mysqld start
-
設(shè)置 MySQL 服務(wù)器 root 用戶的密碼,本教程設(shè)置為 “123456”,后續(xù)步驟中需要用到此用戶名和密碼。
/usr/bin/mysqladmin -u root password "123456" #123456 是初始密碼
2.3.3 配置 PHP
-
啟動 PHP-FPM 服務(wù)。
service php-fpm start
-
配置 PHP Session 的存儲路徑。
打開/etc/php.ini
文件。vim /etc/php.ini
進(jìn)入后直接輸入以下內(nèi)容,回車定位到 “session.save_path” 的位置:
/session.save_path
按字母“I”鍵或 “Insert” 鍵切換至編輯模式,將其改為 :
session.save_path = "/var/lib/php/session"
配置php更改
/var/lib/php/session
目錄下所有文件的屬組都改成 nginx 和 nginx。chown -R nginx:nginx /var/lib/php/session
2.3.4 驗證環(huán)境配置
-
請使用以下命令在 Web 目錄下創(chuàng)建
index.php
文件:vim /usr/share/nginx/html/index.php
-
按字母“I”鍵或 “Insert” 鍵切換至編輯模式,寫入如下內(nèi)容:
<?php echo "<title>Test Page</title>"; echo "Hello World!"; ?>
輸入完成后,按“Esc”鍵,輸入 “:wq”,保存文件并返回。
-
在瀏覽器中,訪問該
index.php
文件,查看環(huán)境配置是否成功:http://云主機(jī)的公網(wǎng) IP/index.php
頁面顯示 “Hello World!”,則說明 LNMP 環(huán)境配置成功。
驗證環(huán)境
CentOS 設(shè)置mysql的遠(yuǎn)程訪問
首先進(jìn)入數(shù)據(jù)庫,使用系統(tǒng)數(shù)據(jù)庫mysql。
mysql -u root -p mysql #回車,然后輸入則使用了系統(tǒng)數(shù)據(jù)庫
設(shè)置系統(tǒng)數(shù)據(jù)庫的root賬戶設(shè)置遠(yuǎn)程訪問的密碼,與本地的root訪問密碼并不沖突。
mysql> grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
#123456為你需要設(shè)置的密碼
防火墻設(shè)置一下,不然3306端口還是無法訪問。
iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
設(shè)置完之后,查看一下是否能通過。
iptables -L -n
如果想要限制訪問。
iptables -D INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
修改數(shù)據(jù)庫密碼
修改數(shù)據(jù)庫密碼
mysql> update user set password=password('qwe123') where user='root'; #qwe123為密碼
刷新權(quán)限表
mysql> flush privileges;
退出mysql,對mysql進(jìn)行重啟
service mysqld restart
上傳服務(wù)器文件
Linux 機(jī)器可通過以下命令向 Linux 云服務(wù)器上傳文件:
scp 本地文件地址 云服務(wù)器登錄名@云服務(wù)器公網(wǎng)IP/域名:云服務(wù)器文件地址
例如,將本地文件/home/lnmp0.4.tar.gz上傳到IP為129.20.0.2的 CentOS 系統(tǒng)云服務(wù)器對應(yīng)目錄下,應(yīng)執(zhí)行以下命令:
scp /home/Inmp0.4.tar.gz root@129.20.0.2:/home/Inmp0.4.tar.gz
Apache (如果Nginx 不能滿足需求)
安裝httpd
yum install -y httpd
編輯配置文件/etc/httpd/conf/httpd.conf
vim /etc/httpd/conf/httpd.conf
apache 文稿存放文件目錄 /var/www/html
啟動服務(wù)
service httpd start
查看CentOS 版本
cat /etc/issue
更換 SSH RSA key
ssh-keygen -R IP #IP 你的服務(wù)器IP