本篇文章給大家?guī)淼膬热菔顷P于如何在linux實例上搭建magento電子商務網(wǎng)站,有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。
在linux實例上搭建Magento電子商務網(wǎng)站
Magento是一款開源電商網(wǎng)站框架,其豐富的模塊化架構體系及拓展功能可為大中型站點提供解決方案。它使用PHP開發(fā),支持版本范圍從PHP 5.6到PHP 7.1,并使用MySQL存儲數(shù)據(jù)。本文主要說明如何在阿里云ECS實例上搭建Magento電子商務網(wǎng)站,使用的操作系統(tǒng)為Linux CentOS 7.2 64位。
適用對象
適用于熟悉ECS,熟悉Linux系統(tǒng),剛開始使用阿里云進行建站的用戶。
資源
本文描述的操作涉及的Linux ECS實例配置包括:2 vCPU、4 GiB內存、Cent OS 7.2 64位操作系統(tǒng)、VPC網(wǎng)絡、分配的公網(wǎng)IP地址。
說明:用于搭建Magento 2的服務器,內存不能小于2 GiB。
根據(jù)本文搭建的Magento電子商務網(wǎng)站,使用的軟件版本信息如下:
MySQL 5.7
PHP 7.0
Magento 2.1
前提條件
您已經(jīng)創(chuàng)建了一臺VPC網(wǎng)絡類型的Linux ECS實例,詳細操作,請參見 創(chuàng)建ECS實例。配置包括:2 vCPU、4 GiB內存、Cent OS 7.2 64位操作系統(tǒng)、VPC網(wǎng)絡、分配公網(wǎng)IP地址。
ECS實例所在安全組中已經(jīng)添加了如下表所示的安全組規(guī)則。詳細操作,請參見 創(chuàng)建ECS實例 和 添加安全組規(guī)則。
操作步驟
使用云服務器ECS搭建Magento網(wǎng)站的操作步驟如下:
步驟1. 安裝配置LAMP平臺
步驟2. 創(chuàng)建數(shù)據(jù)庫
步驟3. 安裝配置Composer
步驟4. 安裝配置Magento
步驟5. 添加cron作業(yè)
步驟1. 安裝配置LAMP平臺
本部分內容說明如何手動安裝LAMP平臺。您也可以在 云市場 購買LAMP鏡像直接啟動ECS實例,以便快速建站。
依次運行以下命令更新包和存儲庫,并安裝Apache Web服務器和MySQL服務器。
#?yum?-y?update #?yum?-y?install?httpd #?rpm?-Uvh?http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm #?yum?-y?install?mysql-community-server
啟動HTTP和MySQL服務并設置開機自啟動。
#?systemctl?start?httpd #?systemctl?enable?httpd #?systemctl?start?mysqld #?systemctl?enable?mysqld
編輯Apache配置文件:
運行命令vim /etc/httpd/conf/httpd.conf。
按 i 鍵進入編輯模式。
做以下修改:
在 Include conf.modules.d/*.conf 之后添加 LoadModule rewrite_module modules/mod_rewrite.so。
將以下內容的 AllowOverride None 改為 AllowOverride all。
Options Indexes FollowSymLinks
#?AllowOverride?controls?what?directives?may?be?placed?in?.htaccess?files. #?It?can?be?"All",?"None",?or?any?combination?of?the?keywords: #?Options?FileInfo?AuthConfig?Limit # AllowOverride?None
按 Esc 鍵退出編輯,并輸入 :wq 保存并退出。
查看/var/log/mysqld.log文件,獲取安裝MySQL時自動設置的root用戶密碼。
#?grep?'temporary?password'?/var/log/mysqld.log 2016-12-13T14:57:47.535748Z?1?[Note]?A?temporary?password?is?generated?for?root@localhost:?p0/G28g>lsHD
運行下面的命令可以從如下4個方面提高MySQL的安全性:
設置root賬號密碼
禁止root賬號遠程登錄
刪除匿名用戶賬號
刪除test庫以及對test庫的訪問權限
詳細說明可參見 官方文檔。
#?mysql_secure_installation Securing?the?MySQL?server?deployment. Enter?password?for?user?root:?#輸入第4步中獲取的root用戶密碼 The?'validate_password'?plugin?is?installed?on?the?server. The?subsequent?steps?will?run?with?the?existing?configuration?of?the?plugin. Using?existing?password?for?root. Estimated?strength?of?the?password:?100? Change?the?password?for?root???((Press?y|Y?for?Yes,?any?other?key?for?No)?:?Y?#是否更改root用戶密碼,輸入Y New?password:?#輸入密碼 Re-enter?new?password:?#再次輸入密碼 Estimated?strength?of?the?password:?100? Do?you?wish?to?continue?with?the?password?provided?(Press?y|Y?for?Yes,?any?other?key?for?No)?:?Y By?default,?a?MySQL?installation?has?an?anonymous?user,?allowing?anyone?to?log?into?MySQL?without?having?to?have?a?user?account?created?for?them.?This?is?intended?only?for?testing,?and?to?make?the?installation?go?a?bit?smoother.?You?should?remove?them?before?moving?into?a?production?environment. Remove?anonymous?users??(Press?y|Y?for?Yes,?any?other?key?for?No)?:?Y?#是否刪除匿名用戶,輸入Y Success. Normally,?root?should?only?be?allowed?to?connect?from?'localhost'.? This?ensures?that?someone?cannot?guess?at?the?root?password?from?the?network. Disallow?root?login?remotely??(Press?y|Y?for?Yes,?any?other?key?for?No)?:?Y?#禁止root遠程登錄,輸入Y Success. By?default,?MySQL?comes?with?a?database?named?'test'?that?anyone?can?access.? This?is?also?intended?only?for?testing,?and?should?be?removed?before?moving?into?a?production environment. Remove?test?database?and?access?to?it??(Press?y|Y?for?Yes,?any?other?key?for?No)?:?Y?#是否刪除test庫和對它的訪問權限,輸入Y -?Dropping?test?database... Success. -?Removing?privileges?on?test?database... Success. Reloading?the?privilege?tables?will?ensure?that?all?changes made?so?far?will?take?effect?immediately. Reload?privilege?tables?now??(Press?y|Y?for?Yes,?any?other?key?for?No)?:?Y?#是否重新加載授權表,輸入Y Success. All?done!
依次運行以下命令,安裝PHP 7和一些所需的額外PHP擴展。
#?yum?install?-y?http://dl.iuscommunity.org/pub/ius/stable/CentOS/7/x86_64/ius-release-1.0-14.ius.centos7.noarch.rpm #?yum?-y?update #?yum?-y?install?php70u?php70u-pdo?php70u-mysqlnd?php70u-opcache?php70u-xml?php70u-gd?php70u-mcrypt?php70u-devel?php70u-intl?php70u-mbstring?php70u-bcmath?php70u-json?php70u-iconv
查看PHP版本,以驗證PHP是否已經(jīng)成功安裝。
#?php?-v PHP?7.0.13?(cli)?(built:?Nov?10?2016?08:44:18)?(?NTS?) Copyright?(c)?1997-2016?The?PHP?Group Zend?Engine?v3.0.0,?Copyright?(c)?1998-2016?Zend?Technologies ?with?Zend?OPcache?v7.0.13,?Copyright?(c)?1999-2016,?by?Zend?Technologies
編輯配置文件/etc/php.ini:
運行命令 vim /etc/php.ini。
按 i 進入編輯模式。
在文件最后添加以下配置:
memory_limit = 128M #根據(jù)實際情況增加內存限制
date.timezone = Asia/Shanghai #設置時區(qū)為上海。
重啟Web服務進程。
#?systemctl?restart?httpd
步驟2. 創(chuàng)建數(shù)據(jù)庫
按以下步驟創(chuàng)建數(shù)據(jù)庫。
創(chuàng)建數(shù)據(jù)庫及用戶:為Magento數(shù)據(jù)創(chuàng)建一個數(shù)據(jù)庫和一個數(shù)據(jù)庫用戶,數(shù)據(jù)庫和用戶名可根據(jù)實際情況修改。
#?mysql?-u?root?-p Enter?password:? mysql>?CREATE?DATABASE?magento;?#根據(jù)實例情況替換magento Query?OK,?1?row?affected?(0.00?sec) mysql>?GRANT?ALL?ON?magento.*?TO?YourUser@localhost?IDENTIFIED?BY?'YourPass';?#根據(jù)實際情況替換YourUser和YourPass Query?OK,?0?rows?affected,?1?warning?(0.00?sec) mysql>?FLUSH?PRIVILEGES; Query?OK,?0?rows?affected?(0.00?sec)
運行 exit 退出MySQL。
(可選)驗證新建的Magento數(shù)據(jù)庫和用戶是否可用。
#?mysql?-u?YourUser?-p mysql>?show?databases; +--------------------+ |?Database???????????| +--------------------+ |?information_schema?| |?magento????????????| +--------------------+ 2?rows?in?set?(0.00?sec) mysql>?exit
步驟3. 安裝配置Composer
Composer是PHP一個包管理和包依賴管理的工具。按以下步驟安裝配置Composer。
安裝Composer。
#?curl?-sS?https://getcomposer.org/installer?|?php All?settings?correct?for?using?Composer Downloading?1.2.4... Composer?successfully?installed?to:?/root/composer.phar Use?it:?php?composer.phar
配置Composer全局使用。
#?mv?/root/composer.phar?/usr/bin/composer
測試命令是否可用。
#?composer?-v ______ /?____/___??____?___??____??____??________??_____ /?/???/?__?/?__?`__?/?__?/?__?/?___/?_?/?___/ /?/___/?/_/?/?/?/?/?/?/?/_/?/?/_/?(__??)??__/?/ ____/____/_/?/_/?/_/?.___/____/____/___/_/ ?????????????????/_/ Composer?version?1.2.4?2016-12-06?22:00:51
步驟4. 安裝配置Magento
您可以使用不同的方法安裝Magento,也可以選擇是否安裝示例數(shù)據(jù)。如果安裝Magento僅用于測試,您可以選擇安裝示例數(shù)據(jù)。如果是在生產(chǎn)環(huán)境中安裝Magento,建議您安裝全新的Magento,從頭開始配置。
本部分介紹如何使用git下載Magento,然后使用Composer安裝Magento。
依次運行以下命令,通過 git clone 下載Magento。
#?yum?-y?install?git #?cd?/var/www/html/ #?git?clone?https://github.com/magento/magento2.git
(可選)將Magento切換到穩(wěn)定版本。
默認情況git下載安裝Magento是一個最新的開發(fā)版本。如果您在生產(chǎn)環(huán)境中使用,建議切換到穩(wěn)定版本,否則未來將無法升級安裝。
#?cd?magento2?&&??git?checkout?tags/2.1.0?-b?2.1.0 Switched?to?a?new?branch?'2.1.0'
將安裝文件移到Web服務器根目錄下。否則,您只能通過 http://[ECS實例公網(wǎng)IP地址]/magento2 訪問您的Magento站點。
#?shopt?-s?dotglob?nullglob?&&?mv?/var/www/html/magento2/*?/var/www/html/?&&?cd?..
設置Magento文件適當?shù)臋嘞蕖?/p>
#?chown?-R?:apache?/var/www/html #?find?/var/www/html?-type?f?-print0?|?xargs?-r0?chmod?640 #?find?/var/www/html?-type?d?-print0?|?xargs?-r0?chmod?750 #?chmod?-R?g+w?/var/www/html/{pub,var} #?chmod?-R?g+w?/var/www/html/{app/etc,vendor} #?chmod?750?/var/www/html/bin/magento
運行 composer install 安裝Magento。
測試:在瀏覽器中訪問 http://[ECS實例公網(wǎng)IP地址],如果出現(xiàn)以下頁面,說明Magento安裝成功。
單擊 Agree and Setup Magento 開始配置Magento:按實際情況填寫連接數(shù)據(jù)庫信息、Web訪問設置、定制商店、創(chuàng)建管理員賬號。出現(xiàn)如下圖所示的界面時,說明Magento配置完成
步驟5. 添加cron作業(yè)
運行 crontab -u apache -e 設置cron運行調度工作。
添加以下內容。
*/10?*?*?*?*?php?-c?/etc?/var/www/html/bin/magento?cron:run */10?*?*?*?*?php?-c?/etc?/var/www/html/update/cron.php */10?*?*?*?*?php?-c?/etc?/var/www/html/bin/magento?setup:cron:run