Ubuntu14.04下搭建LANMP環境(Apache+Nginx+MySQL+PHP)

來源:互聯網
上載者:User
每回安裝環境都要去百度幾文章,今天索性自己寫一篇,以後省去百度的麻煩了,本文介紹的是在Ubuntu14.04下搭建LANMP環境(Apache+Nginx+MySQL+PHP),如果你只想安裝lamp或者lnmp都通用,直接拿去。
首先分兩步走:
每一、先更新系統然後安裝必要的依賴(這一步為以後的擴充提供鋪墊)
第二、安裝mysql apache2 php

sudo apt-get updatesudo apt-get install build-essential gcc g++ autoconf libiconv-hook-dev libmcrypt-dev libxml2-dev libmysqlclient-dev libcurl4-openssl-dev libjpeg8-dev libpng12-dev libfreetype6-dev snmp mcrypt sudo apt-get install mysql-server mysql-clientsudo apt-get install apache2sudo apt-get install php5 php5-common php5-mysql php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl php5-xcache php5-mcrypt

  重啟apache2

sudo service apache2 restart

  ubuntu下apache2虛擬機器主機配置

cd /etc/apache2/sites-availablels000-default.conf  default-ssl.confsudo cp 000-default.conf 000-default.conf.baksudo vim 000-default.conf

  檔案修改為以下內容

     ServerAdmin webmaster@localhost    DocumentRoot /data/www     ErrorLog ${APACHE_LOG_DIR}/error.log    CustomLog ${APACHE_LOG_DIR}/access.log combined             Options Indexes FollowSymLinks        AllowOverride None        Require all granted     

  重啟apache2測試效果

sudo service apache2 restart

  

cd chmod 777 /data/www vim info.php   

  在瀏覽器上http://localhost/info.php

ps aux | grep apache2root      3749  0.0  0.7 568604 28152 ?        Ss   10:05   0:00 /usr/sbin/apache2 -k startwww-data  3753  0.0  0.6 797128 24504 ?        S    10:05   0:00 /usr/sbin/apache2 -k startwww-data  3754  0.0  0.7 574664 30756 ?        S    10:05   0:00 /usr/sbin/apache2 -k startwww-data  3755  0.0  0.5 571040 22676 ?        S    10:05   0:00 /usr/sbin/apache2 -k startwww-data  3756  0.0  0.4 569892 18596 ?        S    10:05   0:00 /usr/sbin/apache2 -k startwww-data  3757  0.0  0.4 569884 18660 ?        S    10:05   0:00 /usr/sbin/apache2 -k startwww-data  3808  0.0  0.5 570500 21040 ?        S    10:05   0:00 /usr/sbin/apache2 -k startzzs       4187  0.0  0.0  15960   924 pts/0    S+   10:53   0:00 grep --color=auto apache2

  第三步、安裝Nginx

sudo apt-get install nginxsudo apt-get install php5-fpmcd /etc/nginx/sites-availablesudo cp default default.bak

  配置Nginx使其支援PHP

cd /etc/nginx/sites-available sudo vim default

  Nginx主機配置(連接埠改為8080,根目錄和剛才的apache2要目錄一樣/data/www)

server {    listen 8080 default_server; #修改連接埠    listen [::]:8080 default_server ipv6only=on; #修改連接埠     root /data/www; #修改網站根目錄    index index.php index.html index.htm; #添加index.php索引檔案     # Make site accessible from http://localhost/    server_name localhost;     location / {        # First attempt to serve request as file, then        # as directory, then fall back to displaying a 404.        try_files $uri $uri/ =404;        # Uncomment to enable naxsi on this location        # include /etc/nginx/naxsi.rules    }     location ~ \.php$ {        fastcgi_split_path_info ^(.+\.php)(/.+)$;        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini         # With php5-cgi alone:        #fastcgi_pass 127.0.0.1:9000;        # With php5-fpm:        fastcgi_pass unix:/var/run/php5-fpm.sock; #使用通訊端方式啟動        fastcgi_index index.php;        include fastcgi_params;    } }

  重啟服務,查看進程

sudo service nginx restartsudo service php5-fpm restartps aux | grep php-fpmroot      3803  0.0  0.6 522716 26760 ?        Ss   10:05   0:00 php-fpm: master process (/etc/php5/fpm/php-fpm.conf)                    www-data  3806  0.0  0.4 751104 16396 ?        S    10:05   0:00 php-fpm: pool www                                                       www-data  3807  0.0  0.2 522716  8432 ?        S    10:05   0:00 php-fpm: pool www                                                       zzs       4234  0.0  0.0  15956   924 pts/0    S+   11:06   0:00 grep --color=auto php-fpmps aux | grep nginxroot      3782  0.0  0.0  85868  1360 ?        Ss   10:05   0:00 nginx: master process /usr/sbin/nginxwww-data  3783  0.0  0.0  86164  2032 ?        S    10:05   0:00 nginx: worker processwww-data  3784  0.0  0.0  86164  2520 ?        S    10:05   0:00 nginx: worker processwww-data  3785  0.0  0.0  86164  2032 ?        S    10:05   0:00 nginx: worker processwww-data  3786  0.0  0.0  86164  1776 ?        S    10:05   0:00 nginx: worker processzzs       4236  0.0  0.0  15956   916 pts/0    S+   11:06   0:00 grep --color=auto

  

附apache2虛擬機器主機配置方法:

在Apache2中,有效網站資訊都存放在/etc/apache2/sites-available/檔案 裡面。 我們可以添加格式如下的資訊來增加一個有效虛擬空間,將default檔案複製一份改一下裡面的路徑就可以了,要啟用配置則則將此檔案做一個軟連結到 /etc/apache2/sites-enabled/ 目錄下
下面具個例子說明:

$ cd /etc/apache2/sites-available/$ sudo cp 000-default.conf test.conf$ mkdir /data/www/logs     #ServerAdmin webmaster@localhost    ServerName test.com    ServerAlias www.test.com    DocumentRoot /data/www/test     ErrorLog /data/www/logs/test-error.log    CustomLog /data/www/logs/test-access.log combined             Options Indexes FollowSymLinks        AllowOverride None        Require all granted     

  添加軟連結到/etc/apache2/sites-enabled目錄

$ sudo ln -s /etc/apache2/sites-available/test.conf /etc/apache2/sites-enabled/test.conf

  

$ cd /etc/apache2/sites-enabled $ ls -aldrwxr-xr-x 2 root root 4096 12月 14 11:48 ./drwxr-xr-x 8 root root 4096 12月 14 00:13 ../lrwxrwxrwx 1 root root   35 12月 14 00:00 000-default.conf -> ../sites-available/000-default.conflrwxrwxrwx 1 root root   38 12月 14 11:48 test.conf -> /etc/apache2/sites-available/test.conf

  

注意:

查看fpm進程是

ps aux | grep php-fpm

  重啟服務是

sudo service php5-fpm restart

  不知道為何兩者名稱不統一,最好用ps aux| grep fpm來查看

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.