Ubuntu18.04安装LAMP搭建laravel5.8环境

2019年5月11日 0 条评论 25 次阅读 0 人点赞

一、首先要更改ubuntu的源,国内推荐使用阿里云的,要更换首先要先备份,命令如下:

cp /etc/apt/sources.list /etc/apt/sources.list.bak

备份完成后打开apt的源文件,命令如下:

vim /etc/apt/sources.list

冒号进入vim命令行模式,输入以下命令删除所有该文件的内容:

:%d

删除后把下面的所有代码复制进去,保存退出,代码如下:

## Note, this file is written by cloud-init on first boot of an instance
## modifications made here will not survive a re-bundle.
## if you wish to make changes you can:
## a.) add 'apt_preserve_sources_list: true' to /etc/cloud/cloud.cfg
##     or do the same in user-data
## b.) add sources in /etc/apt/sources.list.d
## c.) make changes to template file /etc/cloud/templates/sources.list.tmpl

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://mirrors.aliyun.com/ubuntu/ bionic main
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main

## Major bug fix updates produced after the final release of the
## distribution.
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://mirrors.aliyun.com/ubuntu/ bionic universe
deb-src http://mirrors.aliyun.com/ubuntu/ bionic universe
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates universe
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu 
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in 
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
# deb http://mirrors.aliyun.com/ubuntu/ bionic multiverse
# deb-src http://mirrors.aliyun.com/ubuntu/ bionic multiverse
# deb http://mirrors.aliyun.com/ubuntu/ bionic-updates multiverse
# deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates multiverse

## Uncomment the following two lines to add software from the 'backports'
## repository.
## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
# deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
# deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu bionic partner
# deb-src http://archive.canonical.com/ubuntu bionic partner

deb http://mirrors.aliyun.com/ubuntu bionic-security main
deb-src http://mirrors.aliyun.com/ubuntu bionic-security main
deb http://mirrors.aliyun.com/ubuntu bionic-security universe
deb-src http://mirrors.aliyun.com/ubuntu bionic-security universe
# deb http://mirrors.aliyun.com/ubuntu bionic-security multiverse
# deb-src http://mirrors.aliyun.com/ubuntu bionic-security multiverse

因为网上的源很多不靠谱,这是我从阿里云的服务器上copy下来的,亲测没问题,保存后更新一下源,修复一下之前可能缺少的依赖,然后把系统里面的软件也更新下,代码如下:

apt-get update
apt-get -f install
apt-get upgrade

二、安装Apache服务器

代码如下:

apt-get install -y apache2

将Apache中的默认页面修改为PHP优先,命令如下:

vim /etc/apache2/mods-enabled/dir.conf

然后再文件中把index.php放在最前边,代码如下:

<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

保存并退出,开启rewrite重写,命令如下:

a2enmod rewrite

完成后重启Apache,命令如下:

 /etc/init.d/apache2 restart

至于想要配置虚拟主机,多站点共存,请参考: Centos7.4开启虚拟主机与端口站点 ,如果实在不明白的话,可以在文章下边留言,我下次会更新虚拟主机的配法。

三、安装MySQL数据库服务

代码如下:

apt-get install -y mysql-server mysql-client

如果之前更改过源的话,这里默认安装的版本应该是5.7版本的,安装完后,登录进入mysql,命令如下:

mysql -u root -p

直接回车即可,因为ubuntu默认安装的mysql5.7好像没有设置密码,回车后进入mysql的命令行,更改一下mysql的密码,并且让其必须使用密码登录,命令如下:

//使用mysql表

use mysql;

//更改mysql5.7的root密码

update user set authentication_string=PASSWORD("root")where user='root';

//更新mysql密码表

update user set plugin="mysql_native_password";

//刷新权限

flush privileges;

然后退出mysql命令行,再次登录mysql,不输入密码看其还能否登录,不能登录就输入正确的密码进行登录,登录成功后退出进行下一步。

四、安装PHP7.2

ubuntu18.04 默认安装PHP的版本都是php 7.2,所以我们直接安装php让系统选择当前默认版本,命令如下:

apt-get install -y php

可以看到安装界面安装的php版本都是PHP7.2的,等待安装完毕后我们安装一些php7.2的扩展,使PHP更加好用,命令如下:

apt-get install -y php7.2-bz2 php7.2-curl php7.2-gd php7.2-mbstring php7.2-mysql php7.2-mysqli php7.2-calendar php7.2-ctype php7.2-exif php7.2-fileinfo php7.2-gettext php7.2-posix php7.2-shmop php7.2-sockets php7.2-xml php7.2-zip

扩展也安装完毕了,我们修改一下PHP的配置文件,配置文件打开费方法如下:

vim /etc/php/7.2/apache2/php.ini

增加一些功能,功能如下:

开启PHP的错误显示:

display_errors = On

开启错误日志文件[文件位置:域名/php_errors.log ; (重启apache后,如果出现错误则会自动创建该文件) ]

error_log = php_errors.log

到此PHP就安装完成了,别忘了重启一下Apache,让他加载到我们的php设置

五、安装composer

命令如下:

apt-get install composer -y

本来是想自己去下载composer编译安装的,但是考虑到可能会缺少某些依赖,所以就使用apt去安装了,没什么区别。

更改中国源,命令如下:

composer config -g repo.packagist composer https://packagist.phpcomposer.com

六、安装git

命令如下:

apt-get install git -y

注意:安装composer貌似把git也安装了,但所这一步还是要做,万一没安装呢

七、安装svn版本控制

命令如下:

apt-get install subversion -y

八、安装laravel的安装器

使用composer全局安装,命令如下:

 composer global require laravel/installer 

然后进入你的网站根目录,新建一个laravel项目,命令如下:

 laravel new 项目名称 

还有很多的新建项目方法,可以指定版本等等,如果想了解的话,可以去参考我的这篇文章:Composer的基本使用 ,最后别忘了给自己的项目可访问权限,命令如下:

chmod -R 777 项目目录

然后配置好虚拟主机,laravel的目录一定要指到public目录下。这样你就可以愉快的去访问你的laravel项目了。

结语:这个周末打的字实在太多了,更新博客没有你们想象的这么简单,上班就已经很累了,周末写博客算是总结一下这周学习的东西吧,对模糊的知识点进行巩固,希望这篇文章能对你们有所帮助。

梁桂锋

来自西京学院大三的小开发者。

文章评论(0)