search
HomeBackend DevelopmentPHP TutorialUbuntu14.04下搭建LANMP环境(Apache+Nginx+MySQL+PHP)

每回安装环境都要去百度几文章,今天索性自己写一篇,以后省去百度的麻烦了,本文介绍的是在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

  文件修改为以下内容

<VirtualHost *:80>     ServerAdmin webmaster@localhost    DocumentRoot /data/www     ErrorLog ${APACHE_LOG_DIR}/error.log    CustomLog ${APACHE_LOG_DIR}/access.log combined     <Directory />        Options Indexes FollowSymLinks        AllowOverride None        Require all granted    </Directory> </VirtualHost>

  重启apache2测试效果

sudo service apache2 restart

  

cd chmod 777 /data/www vim info.php <?phpphpinfo();

  在浏览器上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<VirtualHost *:80>     #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     <Directory />        Options Indexes FollowSymLinks        AllowOverride None        Require all granted    </Directory> </VirtualHost>

  添加软链接到/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来查看

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is the difference between unset() and session_destroy()?What is the difference between unset() and session_destroy()?May 04, 2025 am 12:19 AM

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

What is sticky sessions (session affinity) in the context of load balancing?What is sticky sessions (session affinity) in the context of load balancing?May 04, 2025 am 12:16 AM

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

What are the different session save handlers available in PHP?What are the different session save handlers available in PHP?May 04, 2025 am 12:14 AM

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

What is a session in PHP, and why are they used?What is a session in PHP, and why are they used?May 04, 2025 am 12:12 AM

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

Explain the lifecycle of a PHP session.Explain the lifecycle of a PHP session.May 04, 2025 am 12:04 AM

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

What is the difference between absolute and idle session timeouts?What is the difference between absolute and idle session timeouts?May 03, 2025 am 12:21 AM

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

What steps would you take if sessions aren't working on your server?What steps would you take if sessions aren't working on your server?May 03, 2025 am 12:19 AM

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

What is the significance of the session_start() function?What is the significance of the session_start() function?May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment