zoukankan      html  css  js  c++  java
  • 在ubuntu上搭建LNMP服务器

    LNMP = Linux + Nginx + MySQL + PHP
    安装Nginx

    执行以下命令即可:

    apt-get install nginx

    不过源里的版本是0.7.65,不喜欢老旧的玩意,可以尝试编译安装。

    编译安装nginx.
    1.准备编译环境

    apt-get install libpcre3-dev build-essential libssl-dev

    在这里 http://nginx.org/en/download.html 能找到nginx的tar球,最新的官方稳定版是
    1.2.0.
    执行:

    cd /opt/
    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_module

    configure完成后会输出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,安装,执行:

    make
    make 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.sh
    mv 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
    下面是一个虚拟主机的配置实例:

    server {
        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_html
    mkdir -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,你可以访问上面的连接获取更多信息。个人更推荐一键傻瓜安装包:

  • 相关阅读:
    django 1.9.7 css控制模板样式
    python -- 判断给定的参数是否是地理位置的经度和纬度
    有恃无恐
    不知不觉
    vscode 编写Python走过的坑
    Python的从头再来
    【初赛】数学题错题总结
    【初赛】选择题总结
    20175317 《Java程序设计》第四周学习总结
    20175317 《Java程序设计》第三周学习总结
  • 原文地址:https://www.cnblogs.com/shihao/p/2519620.html
Copyright © 2011-2022 走看看