Ubuntu 17.10 用 apt 搭建 lamp 環境、安裝 phpmyadmin、redis 及擴充、mysql 擴充、開啟錯誤提示、配置虛擬機器主機,

來源:互聯網
上載者:User

Ubuntu 17.10 用 apt 搭建 lamp 環境、安裝 phpmyadmin、redis 及擴充、mysql 擴充、開啟錯誤提示、配置虛擬機器主機,
最終環境:

Ubuntu17.10、Apache2.4.27、MySQL5.7.20、PHP7.1

 

1. 安裝 apache

官方源有,直接安裝:

sudo apt-get install apache2

 

2. 安裝 mysql

官方源有,直接安裝:

sudo apt-get install mysql-server

安裝期間會提示設定 MySQL administrator 的密碼

 

============================================================

PS:需要什麼軟體或包,直接用 apt-cache 搜尋

sudo apt-cache search <關鍵詞>

確認包名後,直接用 apt-get install 安裝。

 

3. 安裝 php

官方源有 php7.1,直接安裝:

sudo apt-get install php7.1 php7.1-dev

php7.1 是主程式,php7.1-dev 是 7.1 版的工具包(有 phpize、php-config 等等,phpize 可以為 已編譯好的 php 建立擴充模組,php-config 可以獲得 php 的詳細配置)。

 

============================================================

如果要裝 php5.6 的話,推薦這個 PPA 源(就算不裝 5.6,也推薦):ppa:ondrej/php這個源有 php5.6 和 php7.x 以及絕大多數的 php 擴充,包括 redis、memcache、mongodb 等等。

添加 ppa:ondrej/php 源:

sudo add-apt-repository ppa:ondrej/phpsudo apt-get update

安裝 php5.6:

sudo apt-get install php5.6

 

4. 重啟 apache
sudo /etc/init.d/apache2 restart

更多選項:

Usage: apache2 {start|stop|graceful-stop|restart|reload|force-reload}

 

5. 檢查 apache

訪問 http://localhost/。這是 apache 伺服器的預設頁在 /var/www/html 下,裡面還介紹了 apche 的相關設定檔。

 

6. 檢查 mysql

終端輸入 mysql,跟著打兩個 Tab,看到所有有關 mysql 的命令包:

mingc@mingc-GE60-2PL:~/Downloads/mysql-fae9884$ mysqlmysql                      mysql_install_dbmysqladmin                 mysqloptimizemysqlanalyze               mysql_pluginmysqlbinlog                mysqlpumpmysqlcheck                 mysqlrepairmysql_config_editor        mysqlreportmysqld                     mysql_secure_installationmysqld_multi               mysqlshowmysqld_safe                mysqlslapmysqldump                  mysql_ssl_rsa_setupmysqldumpslow              mysql_tzinfo_to_sqlmysql_embedded             mysql_upgrademysqlimport                

輸入mysql -u<你的帳號> -p,斷行符號,輸入密碼,進入 mysql:

mingc@mingc-GE60-2PL:~/Downloads/mysql-fae9884$ mysql -uroot -pEnter password: Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 73Server version: 5.7.20-0ubuntu0.17.10.1 (Ubuntu)Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> 

 

7. 檢查  php
php -v

不出意外的話,應該顯示 php 的版本

 

8. 建立 php 探針

sudo vim /var/www/html/info.php

添加如下內容:

<?phpphpinfo();

改變所有者:

sudo chown www-data:www-data /var/www/html/info.php

訪問 http://localhost/info.php,得到有關 PHP 的詳細頁面。

 

9. 安裝 phpmyadmin
sudo apt-get install phpmyadmin

安裝期間,

詢問要已連線的服務器,選擇 apache2;

詢問建立 phpmyadmin 的資料庫,選擇“是”;

詢問設定登入 phpmyadmin 的使用者和密碼。

然後瀏覽器訪問:http://localhost/phpmyadmin

 

 

10. 安裝 redis 服務和 redis 擴充

官方源有,直接安裝:

sudo apt-get install redis-server

期間,一些需要的工具包也會安裝。安裝好後,預設自啟動、後台運行,設定檔在 /etc/redis 目錄下。

利用 apt 安裝的好處就是,服務和擴充一併裝上了。

重啟 apche, sudo /etc/init.d/apache2 restart 

訪問 http://localhost/info.php,確認 redis 擴充:

測試一下:

終端輸入 redis-,跟著打兩個 Tab 鍵,看到有關 redis 的命令和工具:

mingc@mingc-GE60-2PL:/etc/redis$ redis-redis-benchmark  redis-check-rdb  redis-server     redis-check-aof  redis-cli

redis-cli 是 訪問 redis-server 的用戶端介面,執行 redis-cli 即可登入到 redis 伺服器:

mingc@mingc-GE60-2PL:~/Downloads/mysql-fae9884$ redis-cli 127.0.0.1:6379> 

更多 redis-cli 的命令選項,使用 redis-cli --help 查看。

再寫一段 php 指令碼測試一下, vim /var/www/html/test_redis.php 

內容如下:

<?php$redis = new Redis();$redis->connect('127.0.0.1', 6379);echo "Connection to server sucessfully";echo "Server is running: " . $redis->ping(); 

 

11. 安裝 mysql 擴充

在上面的 http://localhost/info.php 裡可以看到,沒有 mysql 擴充。雖說 php7 裡棄用了 mysql 擴充,但有些舊項目還是需要的。裝一下 mysql 擴充。

這裡選擇編譯安裝(通過 apt 安裝的 php-mysql 擴充,如果是 php7.x,那就是裝 pdo_mysql 擴充)

(1)直接從 PECL 官方站 搜尋 mysql,找到 MySQL 擴充的 頁面 ,點擊頁面的 [ Browse Source ],選擇最新的 commit、下載 tar.gz 包。我下載後的包名為 mysql-fae9884.tar.gz

(2)編譯安裝:

tar -xf mysql-fae9884.tar.gzcd mysql-fae9884phpize./configure --with-php-config=/usr/bin/php-configsudo make && sudo make install

前面裝 php7.1 的時候,也裝了 php7.1-dev,這裡面就有 phpize 和 php-config。而 --with-php-config 是 php-config 指令碼(命令)的位置,可以使用 whereis php-config 查看。

編譯安裝成功後,會有成功提示 和 mysql 擴充模組(.so 檔案)的路徑:

Build complete.Don't forget to run 'make test'.Installing shared extensions:     /usr/lib/php/20160303/  

然後,編輯 php 用於 apache 的設定檔 /etc/php/7.1apache2/php.ini,在最後添加一行:

extension=mysql.so

重啟 apche, sudo /etc/init.d/apache2 restart 

訪問 http://localhost/info.php,確認下有了 mysql 擴充。

 

寫一段 php 指令碼測試:  vim /etc/www/html/test_mysql.php 

內容如下:

<?php$mysql = mysql_connect('127.0.0.1', 'root', 'root');if(!$mysql) {die(mysql_error($mysql));}echo 'Ok' . "\r\n";

 

12. 開啟 php 和 apache 的錯誤提示

預設不顯示 php 錯誤,下面開啟。

(1)修改 php 的設定檔,在 /etc/php/7.1/apache2 下,開啟 php.ini。

(2)搜尋 display_errors = Off,修改為 On

(3)搜尋 error_reporting =  E_ALL & ~E_NOTICE,修改為 E_ALL | E_STRICT(搜不到的畫搜短一點:“error_reporting =”)。

(4)修改 apache 的設定檔,在 /etc/apache2 下,開啟 apache.conf。

(5)檔案最後添加兩行:

php_flag display_errors        on
php_value error_reporting       2039

(6)重啟 apache:

sudo /etc/init.d/apache2 restart

 

13. 建立虛擬機器主機

建立虛擬機器主機自然用 apache 配置,瞭解一下 apache 的配置目錄:

/etc/apache2├── apache2.conf      # 主設定檔,其他的一些設定檔通過 Include 指令包含進來├── conf-available    # 所有可用的設定檔(裡面的 *.conf 檔案內容幾乎都被預設注釋了)├── conf-enabled      # 可用的設定檔中,啟用了哪些,一般都是符號連結、指向上面的 conf-available 目錄裡每個 *.conf 檔案├── envvars           # 環境變數├── magic├── mods-available    # 所有可用的模組├── mods-enabled      # 哪些模組被啟用了├── ports.conf        # 定義連接埠監聽├── sites-available   # 重點來了:所有可用的網站└── sites-enabled     # 哪些網站被啟用了

步驟簡單,就是在 sites-available 目錄定義網站設定檔,然後在 sites-enabled 裡建立指向這個檔案的符號串連。

(1)建配置

cd /etc/apache2/sites-available/sudo cp 000-default.conf my.site.confvim my.site.conf

修改如下(注意別把注釋與指令行放在一行,會有語法錯誤):

<VirtualHost *:80># The ServerName directive sets the request scheme, hostname and port that# the server uses to identify itself. This is used when creating# redirection URLs. In the context of virtual hosts, the ServerName# specifies what hostname must appear in the request's Host: header to# match this virtual host. For the default virtual host (this file) this# value is not decisive as it is used as a last resort host regardless.# However, you must set it for any further virtual host explicitly.# 網域名稱ServerName my.site# 網域名稱別名,可以設定多個,空格隔開ServerAlias my.siteServerAdmin webmaster@localhostDocumentRoot /var/www/my.site<Directory "/var/www/my.site/">    # 啟用符號連結    Options FollowSymLinks        DirectoryIndex index.php index.html index.htm        # 注意這個配置,會影響本地目錄下的 .htaccess 的啟用    AllowOverride All        Order deny,allow    Allow from All        # 限制訪問目錄,多個目錄用空格隔開    # php_admin_value open_basedir "/var/www/my.site/:/tmp:/usr/lib/php/"</Directory># Available loglevels: trace8, ..., trace1, debug, info, notice, warn,# error, crit, alert, emerg.# It is also possible to configure the loglevel for particular# modules, e.g.#LogLevel info ssl:warnErrorLog ${APACHE_LOG_DIR}/error.logCustomLog ${APACHE_LOG_DIR}/access.log combined# For most configuration files from conf-available/, which are# enabled or disabled at a global level, it is possible to# include a line for only one particular virtual host. For example the# following line enables the CGI configuration for this host only# after it has been globally disabled with "a2disconf".#Include conf-available/serve-cgi-bin.conf</VirtualHost># vim: syntax=apache ts=4 sw=4 sts=4 sr noet

精簡一下就是這樣的:

<VirtualHost *:80>ServerName my.siteServerAlias my.siteServerAdmin webmaster@localhostDocumentRoot /var/www/my.site<Directory "/var/www/my.site/">    Options FollowSymLinks    DirectoryIndex index.php index.html index.htm    AllowOverride All    Order deny,allow    Allow from All    # php_admin_value open_basedir "/var/www/my.site/:/tmp:/usr/lib/php/"</Directory>ErrorLog ${APACHE_LOG_DIR}/error.logCustomLog ${APACHE_LOG_DIR}/access.log combined</VirtualHost>

(2)建符號連結

cd /etc/apache2/sudo ln -s ./sites-available/my.site.conf ./sites-enabled/my.site.conf

(3)檢查文法

apachectl configtest

(4)添加 hosts 解析

vim /etc/hosts,添加一行:

127.0.0.1   my.site

(4)重啟apache

sudo /etc/init.d/apache2 restart

然後,按照配置的網站目錄 /etc/www/my.site,建立目錄和測試檔案:

sudo makedir -p /etc/www/my.sitecd /etc/www/my.site/
sudo echo "<h2>Hello, Welcome to my.site!</h2>" > index.html

瀏覽器訪問 my.site,

OK,完成~~

 

參考連結:

  • http://howtoubuntu.org/how-to-install-lamp-on-ubuntu
  • Ubuntu Server 16.04下配置LAMP環境
  • Ubuntu 16.04 搭建 LAMP
  • 開啟PHP和Apache的錯誤提示
  • Ubuntu16.04安裝redis和php的redis擴充
  • Ubuntu Apache 虛擬機器主機配置

  • httpd-vhosts設定網域名稱別名
  • php錯誤提示 open_basedir restriction in effect 解決

相關連結:

  • PHP 及擴充的 PPA 源:https://launchpad.net/~ondrej/+archive/ubuntu/php/+index?batch=75&memo=75&start=75

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.