ubuntu安装lemp(lnmp)

Posted on Posted in linux, mysql, nginx, php
Tips: 本文创建于2014年11月19日,已超过 2 年,内容或图片可能已经失效!

lemp

本次说的是ubuntu安装lemp ubuntu安装lamp,非常简单,一条命令

sudo apt-get install lamp-server^   
#然后在安装过程中输入数据库密码即可  

下面安装lemp

1、安装nginx

卸载apache2,以免冲突

sudo apt-get purge apache2*
sudo apt-get autoremove -y

安装

sudo apt-get install nginx -y
sudo service nginx start

编辑nginx配置文件

sudo nano /etc/nginx/nginx.conf

设置 worker_processes(例如,你系统里CPU数目)。查看CPU数目,可以使用命令“lscpu”。在我这里是“1”。所以我把这个值设为1。设置完需要重启nginx。

worker_processes 1;

默认虚拟主机(服务器模块)定义在文件/etc/nginx/sites-available/default里。

sudo vim /etc/nginx/sites-available/default     #找到如下类似文件并修改  
server {  
        listen 80 default_server;   #监听ipv4 80端口  
        listen [::]:80 default_server ipv6only=on;  #监听ipv6 端口  
        root /usr/share/nginx/html;     #文件根目录  
        index index.html index.htm index.php;   #加入index.php  
        # Make site accessible from http://localhost/  
        server_name localhost;  #服务器FQDN  
        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$ {  
                try_files $uri = 404;   #增加这行:尝试找到所有文件,如果找不到,返回状态吗404。用于避免0day漏洞。  
                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;  
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;  
                include fastcgi_params;  
        }  

测试nginx配置

sudo nginx -t   #返回下面两行,验证无错
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重启nginx

sudo service nginx restart

nginx.png

2、安装mysql

sudo apt-get install mysql-server mysql-client

过程中按照提示输入密码即可 检查mysql状态

sudo service mysql status
mysql start/running, process 3433

3、安装php

sudo apt-get install php5 php5-fpm php5-mysql -y
sudo vim /etc/php5/fpm/php.ini
cgi.fix_pathinfo=0  #编辑文件并把这行参数,1改为0。与上面nginx配置呼应,避免漏洞。

重启php-fpm

sudo service php5-fpm restart
php5-fpm stop/waiting
php5-fpm start/running, process 4964

测试php文件

sudo vim /usr/share/nginx/html/info.php
<?php
phpinfo();
?>

访问http://server-ip-address/info.php PHP-FPM会默认监听套接字/var/run/php5-fpm.sock。如果你希望PHP-FPM使用TCP连接,打开文件/etc/php5/fpm/pool.d/www.conf

sudo vim /etc/php5/fpm/pool.d/www.conf
;listen = /var/run/php5-fpm.sock    #注释这行,添加下面这行
listen = 127.0.0.1:9000

重启php5-fpm

sudo service php5-fpm restart

编辑nginx

sudo vim /etc/nginx/sites-available/default
location ~ \.php$ {
                try_files $uri = 404;
                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_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi_params;
        }

重启nginx

sudo service nginx restart

4、安装phpMyAdmin

sudo apt-get install phpmyadmin

选择一个网页服务器。默认情况下,这里不会显示 nginx。所以,选择 apache 或者 lighttpd,然后我们再把 phpMyAdmin 和 nginx 连接起来工作。 选择‘是’通过dbconfig-common为phpMyAdmin配置数据库。

dbconfig-common.png

创建一个 phpMyAdmin 的软连接到网站根目录。这里我们的网站根文档目录是/usr/share/nginx/html/。

sudo ln -s /usr/share/phpmyadmin/ /usr/share/nginx/html

重启nginx

sudo service nginx restart
» 转载请注明来源:若我若鱼 » ubuntu安装lemp(lnmp)

Leave a Reply

Your email address will not be published. Required fields are marked *

18 + eighteen =