1. 程式人生 > >一步一步教你用PHP+MySql搭建網站 No.0 準備工作

一步一步教你用PHP+MySql搭建網站 No.0 準備工作

新開一個系列教程吧,這次是講如何用PHP+MySQL搭建網站。

之前一直有想過搭建自己的個人網站,然後上週通過阿里雲申請的域名和空間都通過稽核了,於是就開始研究如何用PHP+MYSQL搭建網站,研究了差不多兩週,總算搞定了一個小型的blog類的網站。當然,整個過程是通過學習《PHP and MySQL web development》下來的,這篇教程中的例子是在原書中的Chapter 28: Building a Content Manager System章節的基礎上,修改完成的。所有的原始碼可以在這裡找到,本來想買中文版的書來看,但是看評論說翻譯的不行,然後就直接看的原版第3版的英文PDF,有想要的同學可以自行google或者給我留言。

我是從0開始學習PHP+MySQL的,基本上網站搭建下來花了不到兩週;當然,這裡需要你有一定的HTML和SQL的基礎,很基本的就行了。PHP其實就是個指令碼語言,在原來的HTML中,把需要動態計算的部分用PHP完成,其實最後輸出出來的還是HTML的格式。

所以不用覺得很困難,當然我在學習的過程中還是經歷的很多痛苦的,因為書本資料比較久遠,而且阿里的雲主機也不知道怎麼連資料庫,所以很有必要記錄下來。

按照慣例,我們來看下需要完成哪些準備工作吧。

1. 首先你需要有一個自己的域名和主機。

這個我是通過阿里雲註冊的(www.net.cn)裡面有域名申請,我申請了一個.cn的,一年29塊錢。域名申請很簡單,掏錢就可以了。主要是之後還要申請主機,這樣可以把你的域名解析成你的主機ip地址,然後使用者訪問你的域名以後就能跳轉到你的主機上去,找到相應的網頁檔案了。

整個主機申請的過程可以參照網站上面的介紹,裡面有特別詳細的過程,包括填寫資料,上傳省份證、到指定地點拍照、郵寄資料,然後就等待稽核吧。我之前的所有步驟,就是拍照、郵寄資料這些的都是兩天就搞定了,但是最後資料稽核特別慢,基本上兩週才稽核通過。你也可以申請國外的主機,好像不用稽核,但是需要收費。國內阿里會免費提供你一個主機,但是就是需要稽核。


這個是我在阿里的主機介面,在下面的新手必讀裡面,寫的比較清楚,按照這個步驟來,你的主機申請是沒啥問題的。

2. 資料庫操作

這個步驟我研究了好久才弄明白在哪裡操作。本來按照上圖裡面的3步驟來,本地下了個Mysql然後無論如何也連線不到遠端去。後來無意間發現可以在網頁上管理資料庫。

就是上圖裡面的,在你的主機目錄後面,點選“管理”,然後就跳轉到這個頁面:


然後點選頂端的“資料庫資訊


點選右邊的“管理


然後就到了登入介面,登入以後,在命令列頁面就可以操作資料庫了,建表、插入、刪除等等操作,寫完了以後 ctrl+enter就執行了 


然後就可以執行bookmarks.sql裡面的響應的sql語句,建好我們這個程式需要用到的幾個表

程式碼如下:


然後我們需要插入一些測試資料,直接用教材裡面的吧:

insert into writers (username, password, full_name)
             values ('bob', SHA1('password'), 'Robert Bobbins');

insert into writers (username, password, full_name)
             values ('bill', SHA1('password'), 'William Billings');


insert into pages (code, description)
           values ('news', 'The Top News Stories From Around the World');

insert into pages (code, description)
           values ('sport', 'Sports Latest - All The Winners and Losers');

insert into pages (code, description)
           values ('weather', 'Up To The Minute Weather Reports and Forecasts');


insert into writer_permissions (writer, page) values ('bob', 'news');
insert into writer_permissions (writer, page) values ('bob', 'sport');

insert into writer_permissions (writer, page) values ('bill', 'news');
insert into writer_permissions (writer, page) values ('bill', 'weather');


insert into stories 
    (id, writer, page, headline, created, modified, published,
     story_text, picture)
values (1, 'bill', 'news', 'Man gives birth', 976573221, 976580154, 976570230,
   'A man today gave birth in a hospital on Staten Island, NY.  The baby boy weighed in at just over eight pounds and is doing well.  The parents were naturally overjoyed at the birth of their first son, and have have said they hope to have a large family.  <br /><br />Father Ted, 34, conceived using a new method known as paternatility whereby the fertilised embryo is transferred to the father\'s body at an early stage.  It is believed that this method reduces many of the risks of childbirth.', 'images/1.jpg');

insert into stories 
    (id, writer, page, headline, created, modified, published,
     story_text, picture)
values (2, 'bill', 'news', 'Fire!', 976562355, 976572203, 976570230,
    'Breaking news:  Reports are coming in of a fire in a barn somewhere in Arizona.  Our sources say the barn is very likely to burn to the ground and will not be economically viable to repair.<br /><br />A bystander is reputed to have said "There was quite a lot of smoke"', 'images/2.jpg');

insert into stories 
    (id, writer, page, headline, created, modified, published,
     story_text, picture)
values (3, 'bill', 'news', 'SFON Launch Party report', 976542355, 976542503, 976555650,
    'Yesterday has already gone down in history as the day the best news site on the web first hit the Internet.  Just to prove the point, there was a star-studded party last night at a secret location in Seattle.<br /><br />Joining our team for a boogie were several A-list celebs who wish to remain anonymous.', 'images/3.jpg');

insert into stories 
    (id, writer, page, headline, created, modified, published,
     story_text, picture)
values (4, 'bob', 'sport', 'World Crossword Championship Preview', 976531355, 976532503, 976533320,
    'It\'s now just three days to the start of the prestigious annual World Crossword Championship to be held this year for the first time live on the Internet.  The new media format will allow many more competitors than ever before to take part from the comfort of their own home, or from one of 126 regional centers.<br /><br />Last year\' champion is not keen on the new format.  She said "Crosswords should be done on paper, not online".', 'images/4.jpg');

insert into stories 
    (id, writer, page, headline, created, modified, published,
     story_text, picture)
values (5, 'bob', 'sport', 'Basketball is bad for you', 976542355, 976542503, 976555650,
    'Scientists believe that basketball can be bad for you.  Research has suggested that both watching and playing the game can have detrimental effects on your health.  The scientific evidence supporting this claim is currently being verified by our expert team and we will bring you updates as soon as we can.<br /><br />An NBA spokesperson said "That is complete rubbish".', 'images/5.jpg');

insert into stories 
    (id, writer, page, headline, created, modified, published,
     story_text, picture)
values (6, 'bill', 'weather', 'Storms to come', 976542355, 976542503,
976555650, 'It never rains but it pours.  When the rain comes in November there\'s gonna be a storm.<br /><br /> Meterologists predict rain, thunder, lightening and all the usual displeasures that you get during a period of inclement weather.  No word on a hurricane yet, but don\'t be surprised if we get one!', 'images/6.jpg');

insert into stories 
    (id, writer, page, headline, created, modified, published,
     story_text, picture)
values (7, 'bill', 'weather', 'Sun is shining, weather is sweet', 976451129, 976458734, 976458754,
    'The forecast for this weekend is good, with long spells of sunshine predicted in most areas.  The high temperature will be 96F and no rain is expected until November', '');

3. 好像基本也沒啥了,然後推薦幾個比較好用的HTML編輯器吧。

我用的兩個感覺還不錯 。

首選  Komodo edit,該有的功能都有了,左邊可以看到目錄,然後語法支援的也比較好,寫起程式碼來很舒服


還可以用Sublime Text2 這個也還不錯,我之前用了好久。

4. 最後提醒下,和上一系列教程一樣,這次我依舊用截圖的方式來講解,如果想要原始碼的同學可以直接到這裡下載,但是我還是強烈建議大家親自把程式碼敲上去,這樣感受下php程式碼的寫法,比你直接複製貼上效果來的好!

然後我做好的這個示例網站的地址是  www.jiyuanhuida.cn/28  大家可以先去體驗一下。

好了,馬上正式開始我們的教程!