ubuntu16.04服务器lamp搭建以及常见问题 # 背景 好多朋友问我,从阿里云买了台ubuntu16.04的服务器,如何搭建起一个PHP的环境。针对这种问题,我基本是google一篇文章扔给对方,因为我也是这么干的。但是对方照着文章做,总是遇到各种各样的问题,估计是只会骑自行车,不知道自行车如何运转吧。现在把一些常见问题汇总下: ##### 1. 如果apt-get installphp7.0的库不存在怎么办? ubutun默认的源没有php7.0,可以用阿里云源。 sudo vim /etc/apt/source.list 修改前备份。 ```html # deb cdrom:[Ubuntu 16.04 LTS _Xenial Xerus_ - Release amd64 (20160420.1)]/ xenial main restricted deb-src http://archive.ubuntu.com/ubuntu xenial main restricted #Added by software-properties deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted multiverse universe #Added by software-properties deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted multiverse universe #Added by software-properties deb http://mirrors.aliyun.com/ubuntu/ xenial universe deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-updates multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse #Added by software-properties deb http://archive.canonical.com/ubuntu xenial partner deb-src http://archive.canonical.com/ubuntu xenial partner deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted multiverse universe #Added by software-properties deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe deb http://mirrors.aliyun.com/ubuntu/ xenial-security multiverse ``` 之后就有php7.0了 安装后顺便安装一些程序可能用到的库 ```html apt-get install php7.0 apt-get install libapache2-mod-php7.0 //安装apache与7.0的库 apt-get install php7.0-gd //不安装不能生成二维码 apt-get install php-mbstring //操作字符串 apt-get install php7.0-curl //模拟post请求 apt-get install php7.0-openssl ``` 看到眼熟的感觉可能要用的一些拓展也顺便安装好了,省的以后程序莫名其妙报错。 ##### 2. 如何开启apache的rewrite.so sudo a2enmod rewrite sudo service apache2 restart ##### 3. 开放一个mysql账号给外网连接。 mysql -u root -p use mysql ; seletc host,user form user; grant all privileges on *.* to 您的账号@"%" identified by '您的密码' with grant option; ##### 4. 短信无法发送。 //请注意是否php有curl服务。 sudo apt-get install php7.0-curl sudo service apache2 restart ##### 5. 修改mysql不强制检测非null字段 在/var/mysql/my.cnf文件里新增 # eidt by chenls sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION" 重启mysql ##### 6. apache如何设置多域名转发 很多时间一台服务器可能需要跑很多个站点,那么怎么在一台机子上根据不同的域名访问不同的程序目录呢? 这里以ubuntu apache2为例子 进入apache的目录 cd /etc/apache2/sites-enabled/ sudo mkdir XX站点.conf//如chenls.me.conf ```php # The ServerName directive sets the request scheme, hostname and port that # the server uses to identify itself. This is used when creating # redirection URLs. In the context of virtual hosts, the ServerName # specifies what hostname must appear in the request's Host: header to # match this virtual host. For the default virtual host (this file) this # value is not decisive as it is used as a last resort host regardless. # However, you must set it for any further virtual host explicitly. #ServerName www.example.com ServerAdmin webmaster@localhost DocumentRoot /var/www/xxxxx/trunk/public//您的项目入口 ServerName www.chenls.me//主域名 ServerAlias chenls.me//从域名 # Available loglevels: trace8, ..., trace1, debug, info, notice, warn, # error, crit, alert, emerg. # It is also possible to configure the loglevel for particular # modules, e.g. #LogLevel info ssl:warn ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined Options Indexes FollowSymLinks MultiViews AllowOverride all Order allow,deny allow from all # For most configuration files from conf-available/, which are # enabled or disabled at a global level, it is possible to # include a line for only one particular virtual host. For example the # following line enables the CGI configuration for this host only # after it has been globally disabled with "a2disconf". #Include conf-available/serve-cgi-bin.conf ``` 配置后重启apache2即可