LAMP Stack 安裝教學
LAMP 的意思就是:Linux(作業系統)+ Apache(網頁伺服器)+ MySQL(資料庫)+ PHP(語言引擎),是目前新手架站最常使用的作法,因為討論資源豐富,所以出問題相對方便找到人幫忙解決! 效能不怎麼樣就是…
主機設定
設定 Hostname
先用 cat 讀一下設定檔
➜ ~ cat /etc/hostname MyWebHost ➜ ~ cat /etc/hosts 127.0.0.1 localhost 127.0.1.1 MyWebHost # The following lines are desirable for IPv6 capable hosts ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters
沒有的話就補一下
➜ ~ sudo vim /etc/hostname - 原本的 + MyWebHost
➜ ~ sudo vim /etc/hosts + 127.0.1.1 MyWebHost
改完記得重新開機!
用 hostname
指令檢查一下
➜ ~ hostname MyWebHost
安裝 Apache
一行搞定!
sudo apt install apache2
如果沒有要設定 Virtual Host 的話,請記得設定 ServerName
,加在最後一行
➜ ~ sudo vim /etc/apache2/apache2.conf + ServerName server_domain_or_IP
跑一下測試工具
➜ ~ sudo apache2ctl configtest Syntax OK
設定 UFW
總之先列出所有的 App 設定檔來看看
➜ ~ sudo ufw app list Available applications: Apache Apache Full Apache Secure CUPS OpenSSH
在設定下去之前先看一下自己設定了什麼,這很重要!
➜ ~ sudo ufw app info "Apache Full" Profile: Apache Full Title: Web Server (HTTP,HTTPS) Description: Apache v2 is the next generation of the omnipresent Apache web server. Ports: 80,443/tcp
允許 HTTP / HTTPS 通過防火牆
➜ ~ sudo ufw allow "Apache Full" Rule added Rule added (v6)
總之還是確認一下設定
➜ ~ sudo ufw status Status: active To Action From -- ------ ---- 22 ALLOW Anywhere Apache Full ALLOW Anywhere 22 (v6) ALLOW Anywhere (v6) Apache Full (v6) ALLOW Anywhere (v6)
試著瀏覽一下…
http://ip_位址
如果不知道主機的 IP 位址,可以使用
ifconfig
或ip addr
指令查一下,不然的話在分享器的已連線裝置列表
應該也找得到
安裝 MariaDB
就目前的情況看來,Oracle 入主 MySQL 之後,仍然在繼續提供更新,不管是安全性更新,還是小幅度的功能改變(Release Note),但是有鑑於 Oracle 旗下仍有其他競爭產品,而且對開放原始碼這件事情並不特別熱衷,我個人目前仍推薦轉移到 MariaDB 陣營。
官方 Repository(暫不支援 ARM 架構)
基本上照著上面的說明裝就好了,如果遇到什麼困難,可以參考我們的 這篇文章。
ARM Port (for Debian/Ubuntu)
基本上映像檔的作者應該都有幫你打包了,用以下指令檢查一下就好了
apt search mariadb-server
如果不幸找不到結果,你可以考慮自己在 /etc/apt/sources.list
加入以下的來源:
Debian(Raspbian):
deb http://archive.raspbian.org/raspbian wheezy main contrib non-free deb-src http://archive.raspbian.org/raspbian wheezy main contrib non-free
Ubuntu / Armbian:
deb http://ports.ubuntu.com/ xenial main restricted universe multiverse deb-src http://ports.ubuntu.com/ xenial main restricted universe multiverse deb http://ports.ubuntu.com/ xenial-updates main restricted universe multiverse deb-src http://ports.ubuntu.com/ xenial-updates main restricted universe multiverse deb http://ports.ubuntu.com/ xenial-security main restricted universe multiverse deb-src http://ports.ubuntu.com/ xenial-security main restricted universe multiverse deb http://ports.ubuntu.com/ xenial-backports main restricted universe multiverse deb-src http://ports.ubuntu.com/ xenial-backports main restricted universe multiverse
安裝指令只要一行就夠了
sudo apt install mariadb-server
安全設定工具
執行之後會跳出一個提示,提醒你這個程式的功能是關閉一些常見的,可能影響安全的設定。
➜ ~ sudo mysql_secure_installation NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
如果你在安裝 DB 時有設定 root 密碼,請記得輸入,否則直接按下 Enter 鍵即可( MariaDB 10.x 的話,安裝過程中沒有要你設定是正常的)
In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here. Enter current password for root (enter for none): OK, successfully used password, moving on...
趁這個機會把 root 密碼改一改
Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation. Set root password? [Y/n] Y New password: Re-enter new password: Password updated successfully! Reloading privilege tables.. ... Success!
要不要刪掉匿名(anonymous)使用者(建議選是)
By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB 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? [Y/n] Y ... Success!
是否停用 root 帳戶的遠端登入(建議選是)
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? [Y/n] Y ... Success!
是否刪除預設的測試用資料庫(test)
By default, MariaDB 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? [Y/n] 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? [Y/n] Y ... Success!
清理暫存檔並完成
Cleaning up... All done! If you've completed all of the above steps, your MariaDB installation should now be secure. Thanks for using MariaDB!
試著用剛才設定的 root 密碼登入看看
➜ ~ mysql -u root -p Enter password: ERROR 1698 (28000): Access denied for user 'root'@'localhost'
這時候你應該會發現:
咦?root 帳戶怎麼完全登入不進去了,難道我的設定搞錯了?
其實並不是這樣的,你可以在 systemctl
的紀錄中看出端倪:
170927 18:22:41 [Warning] 'user' entry 'root@localhost' has both a password and an authentication plugin specified. The password will be ignored
總之,用 sudo
就可以登入了…
如果你不喜歡這樣,你可以參考[這篇文章][MariaDB Warning root – StackOverflow],或新增一個同等功能的帳戶!
➜ ~ sudo mysql -u root -p Enter password: MariaDB [(none)]> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ 3 rows in set (0.00 sec) MariaDB [(none)]> CREATE USER 'myroot'@'localhost' IDENTIFIED BY 'new_password'; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> GRANT ALL PRIVILEGES ON * . * TO 'myroot'@'localhost' WITH GRANT OPTION; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> exit; Bye
試著登入一下
➜ ~ mysql -u myroot -p Enter password: ERROR 1045 (28000): Access denied for user 'myroot'@'localhost' (using password: YES) ➜ ~ mysql -u myroot -p Enter password: MariaDB [(none)]> exit; Bye
安裝 PHP 7
先搜尋一下有哪些東西可裝
➜ ~ apt search ^php7 Sorting... Done Full Text Search... Done php7.0/xenial-updates,xenial-security 7.0.22-0ubuntu0.16.04.1 all server-side, HTML-embedded scripting language (metapackage) php7.0-bcmath/xenial-updates,xenial-security 7.0.22-0ubuntu0.16.04.1 armhf Bcmath module for PHP php7.0-bz2/xenial-updates,xenial-security 7.0.22-0ubuntu0.16.04.1 armhf bzip2 module for PHP php7.0-cgi/xenial-updates,xenial-security 7.0.22-0ubuntu0.16.04.1 armhf server-side, HTML-embedded scripting language (CGI binary) ...(略)
安裝 php 套件(請依自己的需求增減)
sudo apt-get install php7.0 libapache2-mod-php7.0 php7.0-cli php7.0-curl php7.0-gd php7.0-mbstring php7.0-mcrypt php7.0-mysql php7.0-xml php7.0-xmlrpc
重新啟動 Apache,套用新的模組,然後 status
檢查一下
➜ ~ sudo systemctl restart apache2 ➜ ~ sudo systemctl status apache2 ● apache2.service - LSB: Apache2 web server Loaded: loaded (/etc/init.d/apache2; bad; vendor preset: enabled) Drop-In: /lib/systemd/system/apache2.service.d └─apache2-systemd.conf Active: active (running) since 四 2017-09-28 15:40:39 UTC; 4s ago Docs: man:systemd-sysv-generator(8) Process: 10315 ExecStop=/etc/init.d/apache2 stop (code=exited, status=0/SUCCESS) Process: 8751 ExecReload=/etc/init.d/apache2 reload (code=exited, status=0/SUCCESS) Process: 10339 ExecStart=/etc/init.d/apache2 start (code=exited, status=0/SUCCESS) CGroup: /system.slice/apache2.service ├─10357 /usr/sbin/apache2 -k start ├─10360 /usr/sbin/apache2 -k start └─10361 /usr/sbin/apache2 -k start ...(略)
建立新測試用的頁面,顯示 php 的狀態(確認完記得刪掉)
sudo nano /etc/apache2/mods-enabled/dir.conf
<span class="token operator"><</span>IfModule mod_dir<span class="token punctuation">.</span>c<span class="token operator">></span> DirectoryIndex index<span class="token punctuation">.</span>html index<span class="token punctuation">.</span>cgi index<span class="token punctuation">.</span>pl index<span class="token punctuation">.</span>php index<span class="token punctuation">.</span>xhtml index<span class="token punctuation">.</span>htm <span class="token operator"><</span><span class="token operator">/</span>IfModule<span class="token operator">></span>
用瀏覽器開一下
http://ip_位址/info.php
安裝 phpMyAdmin(非必要)
好消息是,只要一行指令就能完成
sudo apt install phpmyadmin
後記
最近剛好有機會,稍微研究了一下 LAMP 的裝法,想說放上來跟大家分享一下,下次更新我也不知道會是什麼時候…
總之,我會努力的!!