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


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

    为了防止出现意外状况,建议先卸载现有的全部web服务器资源,如apache、mysql、phpphp

yum remove httpd

2、安装开发包和库文件

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

3、安装nginx

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

    一、添加官方的nginx资源库,我是centos,其它系统去:http://nginx.org/packages/本身找mysql

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

    二、此时能够查看nginx包信息  nginx

yum info nginx

    三、安装nginxc++

yum install nginx

    如不出现错误,则安装成功,出错了就本身解决呗(通常不会出错的,请相信我)。web

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

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

    修改成如下内容,将如下内容前面的“#”去掉,而后改一下fastcgi_param后面的目录便可,改成default.conf的web目录地址:/usr/share/nginx/html,或者是其它的其它目录也能够,可是必定要是root(默认)的目录shell

         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运行,其它的配置没作任何修改
apache

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;
    #}
}

   

    而后启动并设置为开机启动centos

   

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

4、安装mariadb

     由于centos7已用mariadb替换mysql,因此安装的是mariadb(反正mysql和mariadb没啥区别)

yum install  mariadb mariadb-server

    如不出错,则安装成功

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

systemctl start mariadb.service
systemctl enable mariadb.service

    而后配置mariadb

mysql_secure_installation

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

 

5、安装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      #设置为开机启动

6、最重要固然是测试一下了

    默认nginx的web路径是/usr/share/nginx/html,能够改,能够不改

    在web目录下建一个php文件

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

    将如下内容写入文件

<?php
    phpinfo();
?>

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

7、配置虚拟主机

    这一步其实最简单了

    把上面的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.";
?>

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

相关文章
相关标签/搜索