在ubuntu上搭建LNMP服务器
LNMP = Linux + Nginx + MySQL + PHP
安裝Nginx
執行以下命令即可:
apt-get install nginx不過源里的版本是0.7.65,不喜歡老舊的玩意,可以嘗試編譯安裝。
編譯安裝nginx.
1.準備編譯環境
在這里 http://nginx.org/en/download.html 能找到nginx的tar球,最新的官方穩定版是
1.2.0.
執行:
wget http://nginx.org/download/nginx-1.2.0.tar.gz
tar -zxvf nginx-1.2.0.tar.gz
cd /opt/nginx-1.2.0/
configure:
./configure --prefix=/opt/nginx --user=nginx --group=nginx --with-http_ssl_moduleconfigure完成后會輸出nginx的一些相關信息,
nginx path prefix: “/opt/nginx”
nginx binary file: “/opt/nginx/sbin/nginx”
nginx configuration prefix: “/opt/nginx/conf”
nginx configuration file: “/opt/nginx/conf/nginx.conf”
nginx pid file: “/opt/nginx/logs/nginx.pid”
nginx error log file: “/opt/nginx/logs/error.log”
nginx http access log file: “/opt/nginx/logs/access.log”
nginx http client request body temporary files: “client_body_temp”
nginx http proxy temporary files: “proxy_temp”
nginx http fastcgi temporary files: “fastcgi_temp”
make,安裝,執行:
makemake install
給nginx進程添加用戶nginx:
adduser --system --no-create-home --disabled-login --disabled-password --group nginx下載并安裝啟動腳本:
wget -O init-deb.sh http://library.linode.com/assets/552-init-deb.shmv init-deb.sh /etc/init.d/nginx
chmod +x /etc/init.d/nginx
/usr/sbin/update-rc.d -f nginx defaults
啟動一下試試:
/etc/init.d/nginx start如果輸出正常,那nginx就安裝成功了。
配置虛擬主機:
如果從源里安裝的nginx,那nginx配置文件位于/etc/nginx/sites-enabled
下面是一個虛擬主機的配置實例:
? ? listen ? 80;
? ? server_name www.example.com example.com;
? ? access_log /srv/www/example.com/logs/access.log;
? ? error_log /srv/www/example.com/logs/error.log;
? ? location / {
? ? ? ? root ? /srv/www/example.com/public_html;
? ? ? ? index ?index.html index.htm;
? ? }
}
注意,配置文件中出現的目錄必須存在,nginx并不會自動創建他們,所以,還需要執行:
mkdir -p /srv/www/example.com/public_htmlmkdir -p /srv/www/example.com/logs
如果是編譯安裝的版本,相關路徑見上文configure部分,配置文件位于/opt/nginx/conf,可以直接在nginx.conf添加 server段,為了便于管理,還是把虛擬主機獨立出來好,修改nginx.conf, 如下在http段添加include部分。
http {# [...]
include /opt/etc/nginx/sites-enabled/*;
# [...]
}
添加完虛擬主機后,別忘了重啟以下nginx服務。
/etc/init.d/nginx restart刪除虛擬主機只需要刪除對應的配置文件并重新啟動一下下nginx就好。
安裝php with fastcgi
執行:
apt-get install php5-cli php5-cgi psmisc spawn-fcgi執行下面的命令會下載并安裝fastcgi的控制腳本:
cd /opt/wget -O php-fastcgi-deb.sh http://library.linode.com/assets/554-php-fastcgi-deb.sh
mv /opt/php-fastcgi-deb.sh /usr/bin/php-fastcgi
chmod +x /usr/bin/php-fastcgi
wget -O init-php-fastcgi-deb.sh http://library.linode.com/assets/553-init-php-fastcgi-deb.sh
mv /opt/init-php-fastcgi-deb.sh /etc/init.d/php-fastcgi
chmod +x /etc/init.d/php-fastcgi
/etc/init.d/php-fastcgi start
update-rc.d php-fastcgi defaults
然后在虛擬主機的配置文件里面添加php支持,示例如下:
server {? ? server_name www.example.com example.com;
? ? access_log /srv/www/example.com/logs/access.log;
? ? error_log /srv/www/example.com/logs/error.log;
? ? root /srv/www/example.com/public_html;
? ? location / {
? ? ? ? index index.html index.htm index.php;
? ? }
? ? location ~ \.php$ {
? ? ? ? include /etc/nginx/fastcgi_params;
? ? ? ? fastcgi_pass ?127.0.0.1:9000;
? ? ? ? fastcgi_index index.php;
? ? ? ? fastcgi_param SCRIPT_FILENAME /srv/www/example.com/public_html$fastcgi_script_name;
? ? }
}
最后重啟一下nginx服務:
/etc/init.d/nginx restart安裝MySQL:
執行:
apt-get install mysql-server php5-mysql安裝過程中會要求輸入數據庫的初始密碼,
還可以執行一下:
mysql_secure_installation禁用root的遠程登錄即可。
如果需要重新設置MySQL的密碼,執行:
dpkg-reconfigure mysql-server-5.0最后重啟一下fastcgi:
/etc/init.d/php-fastcgi restart至此LNMP安裝完成。
本文翻譯自Linode的Library文章:http://library.linode.com/lemp-guides/ubuntu-10.04-lucid#sph_id7
文中的操作系統是Ubuntu 10.04,你可以訪問上面的連接獲取更多信息。個人更推薦一鍵傻瓜安裝包:
總結
以上是生活随笔為你收集整理的在ubuntu上搭建LNMP服务器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: nginx配置文件
- 下一篇: C函数的实现(strcpy,atoi,a