您的位置:首页 > 运维架构 > Linux

Linux 配置LNMP服务器 并配置虚拟主机

2016-11-09 10:51 435 查看

一、停止甚至删除系统上现有的web服务器软件

    为了防止出现意外情况,建议先卸载现有的所有web服务器资源,如apache、mysql、php

yum remove httpd


二、安装开发包和库文件

yum -y install ntp make openssl openssl-devel pcre pcre-devel libpng libpng-devel libjpeg-6b libjpeg-devel-6b freetype freetype-devel gd gd-devel zlib zlib-devel gcc gcc-c++ libXpm libXpm-devel ncurses ncurses-devel libmcrypt libmcrypt-devel libxml2 libxml2-devel imake autoconf automake screen sysstat compat-libstdc++-33 curl curl-devel


三、安装nginx

    因官方已经支持yum安装,所以采用yum安装(一是因为快,二是因为我懒,最重要的是编译安装好麻烦)

    1、添加官方的nginx资源库,我是centos,其它系统去:http://nginx.org/packages/自己找

rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

    2、此时可以查看nginx包信息  

yum info nginx

    3、安装nginx

yum install nginx

    如不出现错误,则安装成功,出错了就自己解决呗(一般不会出错的,请相信我)。

    配置nginx支持php,修改default.conf配置文件(我装的是nginx1.8.0版本。还有,现在还没装php呢,先配置好而已)

vi /etc/nginx/conf.d/default.conf

    修改为以下内容,将以下内容前面的“#”去掉,然后改一下fastcgi_param后面的目录即可,改为default.conf的web目录地址:/usr/share/nginx/html,或者是其它的其它目录也可以,但是一定要是root(默认)的目录

         location ~ \.php$ {
              root           html;
              fastcgi_pass   127.0.0.1:9000;
              fastcgi_index  index.php;
              fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;#这里写web服务器的目录地址
              include        fastcgi_params;
          }

   

    整个default.conf配置文件如下,本文只是让php运行,其它的配置没做任何修改

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;
    index index.php index.html index.htm;
    location / {
        root   /usr/share/nginx/html;
        #index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #把下面的location的所有“#”全部删除
    location ~ \.php$ {
        root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

   

    然后启动并设置为开机启动

   

systemctl start nginx        #启动nginx服务
systemctl enable nginx    #设置nginx为开机启动


四、安装mariadb

     因为centos7已用mariadb替换mysql,所以安装的是mariadb(反正mysql和mariadb没啥区别)

yum install  mariadb mariadb-server
<
d304
/pre>
    如不出错,则安装成功

   然后启动并设置为开机启动

systemctl start mariadb.service
systemctl enable mariadb.service

    然后配置mariadb

mysql_secure_installation

    除了让你输入新密码和确认新密码,其它一路回车即可,博主英文不好。

 

五、安装php

    因为nginx是用FastCGI模式运行php,php-fpm是一个FastCGI管理器,所以安装的时候要选择php-fpm模块

yum install php lighttpd-fastcgi php-cli php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-mssql php-snmp php-soap php-tidy php-common php-devel php-fpm

    安装完成后启动php-fpm,并且设置未开机启动,因为我这里是centos7,所以我就用7的命令操作了

systemctl start php-fpm          #启动php-fpm
systemctl enable php-fpm      #设置为开机启动


六、最重要当然是测试一下了

    默认nginx的web路径是/usr/share/nginx/html,可以改,可以不改

    在web目录下建一个php文件

vi /usr/share/nginx/html/index.php

    将以下内容写入文件

<?php
    phpinfo();
?>

   如果出现激动人心的phpinfo信息,则证明lnmp配置成功,恭喜恭喜,如果出现错误,就慢慢改呗。

七、配置虚拟主机

    这一步其实最简单了

    把上面的default.conf复制一份(其实不复制也行,重新在default.conf最下面写一个server,不过我喜欢将不同的东西彻彻底底的分开),改改server_name和root的地址,还有和他俩相关的选项即可:

server {
    listen       80;
    server_name  localtest.com;                  #把这里的域名改一下即可

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;
    index index.php index.html index.htm;
    location / {
        root   /var/www/html;                         #设置此虚拟主机的目录地址
        #index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;            #错误页面,改不改无所谓了,都用一样的呗(又暴露了我懒的本质了)
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html$fastcgi_script_name;             #把这里的目录改成和上面root一样的
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

    怎么样,是不是和default.conf没多大区别,只是改了三个地方而已。

    修改完后,重启一下nginx服务。

   

systemctl restart nginx

    然后配一下hosts文件,重启一下网络服务,
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: