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


一、停止甚至删除系统上现有的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

    如不出错,则安装成功

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

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文件,重启一下网络服务,如果是在本地虚拟机或局域网安装测试的,别忘了改一下你当前物理机的hosts.

systemctl restart network

    在 /var/www/html文件下建一个index.php文件,随便写点东西(懒人同胞们请复制下面的php代码),在浏览器里面输入你的设置的域名访问一下吧,哈哈,是不是出现了久违的“hello world.”,恭喜你,配置成功。

   

<?php
    echo "hello world.";
?>

    好了,整篇教程到这里就结束了,如果不会,请私信我,我看到会回复的。

转载于:https://my.oschina.net/u/873934/blog/597319

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值