Simple web server
I was making an web page at work recently, the main perpose is to integrate the API I wrote with a web page console. So I will need a web server for my site and do the test. After a quick search on google, I decided to use nginx and php-fpm to implement my server.
I installed it on an AWS-ec2 with Amazon Linux AMI instance.
最近工作上剛好在做一個網站,主要功能是要把我之前在寫的cloud messaging API用一個網頁主控台的方式接起來,讓不會使用command line
指令的使用者也可以來使用我們API的功能。經過一番隨意的搜尋研究之後決定使用 Nginx + PHP-FPM 來實現一個可以跑php的網站伺服器。使用的server是裝在AWS的ec2機器Amazon Linux AMI上面。
Install Nginx and PHP-FPM
1 | sudo yum install nginx php php-fpm |
Completed! the installation was pretty easy, but the configuration was annoying.
Put your index.php
and all web files to the default Nginx path:
把你的index.php
跟其他網站檔案放到 Nginx 預設的路徑底下:1
/usr/share/nginx/html/index.php
Configure Nginx
Nginx Configuration:
順利安裝完之後開始修改 Nginx Server 設定檔,如下:1
sudo vim /etc/nginx/nginx.conf
1 | user nginx; |
The only changes I made in nginx.conf after the installation were :
1 34: root /usr/share/nginx/html;# the default root path, you can change it to where you put your files.
# Nginx Server的預設路徑,你可以把它改成你自己放檔案的地方。
1 36: index index.php;# set index lookup file name
# 預設首頁檔名
1
2
3
4
5
6
7
8
9 53: location ~ \.php$ {
54: #root html;
55: #root /usr/share/nginx/html;
56: fastcgi_pass 127.0.0.1:9000;
57: fastcgi_index index.php;
58: include fastcgi_params;
59: fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
60: #include fastcgi_params;
61: }# PHP settings, notice that line:59
fastcgi_param
is different from the default
# PHP 設定,注意59行的fastcgi_param
跟預設的有點不同。
Start Nginx and PHP-FPM
start PHP-FPM:1
sudo service php-fpm start
start Nginx:1
sudo service nginx start
Congratulation!
Now the PHP and web server are all ready to go, open an browser and connect to your server throught your public ip, and you should be able to see the Nginx Welcome page as follow or the index.php made by yourself.
現在全部設定都弄好了,你只要透過AWS給你的public ip 連到你的Server,你就可以看到下面的Nginx預設歡迎畫面,或是你自定的首頁index.php啦!