ubuntu14.04中项目如何配置apache并且正常运行

来源:互联网 发布:飞信网络技术开发公司 编辑:程序博客网 时间:2024/05/18 05:02

今天发表一篇使用ubuntu,怎么配置项目apache,并且在本地运行   (第一种是是前后端分离的,前端需要配置一个域名,后端也需要配置一个,后面我会补充不是前后端分离的配置方法)

首先下载的项目在某一个文件价里边,这个就不多说了,记住自己的项目的路径,后面的配置会有用到。

我做一个示范,我的项目创建在主文件下面的 demo 这个文件夹里边(demo是我自己创建的一个文件夹,方便自己管理)

项目文件夹叫WAPPHONE(git拉下来的公司项目文件夹名称)

(第一种:前后端分离的配置方法)

进入到apache里面开始配置 输入下面命令

  cd /etc/apache2/sites-available/

然后开始配置前端域名并且写入配置

sudo vim phonelocal.com.conf  

回车之后输入密码,进入到里面写配置

<VirtualHost *:80>


        ServerName phonelocal.com    //配置本地前端项目的域名是什么


        ServerAdmin webmaster@localhost
        DocumentRoot /home/flting/demo/WAPPHONE/public/angular   //项目路径,指向前端的部分
        DirectoryIndex index.html   //前端的开始文件


        <Directory /home/flting/demo/WAPPHONE/public/angular/>   //这里是一样的
        AllowOverride all
        require all granted
        </Directory>


        ErrorLog ${APACHE_LOG_DIR}/wapphone_error.log    //配置前端出错
        CustomLog ${APACHE_LOG_DIR}/wapphone_access.log combined 


</VirtualHost>

然后按esc,打冒好,在输入wq,回车(基本一步一步教学了)

然后是后端的配置

sudo vim phoneapilocal.com.conf (我们区分前后端的方法就是在后端的配置里面多加一个api,大家可以根据自己的项目要求进行配置)

回车之后输入密码,进入到里面写配置

<VirtualHost *:80>


        ServerName phoneapilocal.com   //配置后端的域名

        ServerAdmin webmaster@localhost
        DocumentRoot /home/flting/demo/WAPPHONE/public   //指向后端的项目位置
        DirectoryIndex index.php //后端的开始文件
        Header set Access-Control-Allow-Origin "http://phonelocal.com"    //指向前端的域名,跟前面配置的对应好
        Header set Access-Control-Allow-Credentials true


        <Directory /home/flting/demo/WAPPHONE/public>    //指向后端的位置
        AllowOverride all
        require all granted
        </Directory>


        ErrorLog ${APACHE_LOG_DIR}/wapphone_api_error.log    //配置后端出错
        CustomLog ${APACHE_LOG_DIR}/wapphone_api_access.log combined


</VirtualHost>


然后按esc,打冒好,在输入wq,回车

然后启用配置:sudo a2ensite phonelocal.com

    sudo a2ensite phonepailocal.com

重启 apache2:sudo service apache2 restart

这一步完成之后 进入到hosts文件

sudo vim /etc/hosts

进入里面写入

 127.0.0.1 phonelocal.com  //配置前端的域名 然后保存退出

在本机浏览器输入 phonelocal.com 就能看见自己的项目运行成功

(如果遇到浏览器不能正常运行的情况,是因为项目的权限问题)这时候输入以下代码

sudo chmod 755 /home/flting/demo
sudo chmod 755 -R /home/flting/demo/WAPPHONE


(第二种,前后端不分离的项目配置)

前面的步骤都一样,进入apache里面进行配置,改变的是配置里面的内容而已

配置域名

sudo vim phonelocaltest.com.conf  

配置内容

<VirtualHost *:80>
     ServerAlias phonelocaltest.com    //配置项目地址名称
    ServerAdmin webmaster@localhost
    DocumentRoot /home/flting/demo/WAPPHONE/public    //项目的路径位置

    <Directory /home/flting/demo/WAPPHONE/public>    //项目的路径位置
    AllowOverride all
    require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

启用配置:sudo a2ensite phonelocaltest.com

重启 apache2:sudo service apache2 restart

打开host文件:sudo vim /etc/hosts
添加一行:127.0.0.1 phonelocaltest.com

浏览器输入phonelocaltest.com,即可成功运行


阅读全文
0 0
原创粉丝点击