阿里云ECS环境配置-Ubuntu14.04+php7.0+nginx+mysql
1.安装nginx(版本:1.11.6)
1、自动安装最新版的nginx
wget -O - http://nginx.org/keys/nginx_signing.key | sudo apt-key add -
2、对当前sources.list文件做个备份
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
3、现在把以下软件源地址复制到这个目录
/etc/apt/sources.list
echo “deb http://nginx.org/packages/mainline/ubuntu/ trusty nginx” | sudo tee -a /etc/apt/sources.list
echo “deb-src http://nginx.org/packages/mainline/ubuntu/ trusty nginx” | sudo tee -a /etc/apt/sources.list
4、更新软件包,安装最新版的nginx
sudo apt-get update
sudo apt-get install nginx5、现在启动nginx,访问IP,会出现nginx欢迎页
sudo service nginx restart
2.安装php7(版本7.0.13)
1、添加PPA,添加过程中需要按一次回车(Enter)键
sudo apt-get install python-software-properties software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update2、安装PHP7以及所需的一些扩展
sudo apt-get install php7.0-fpm php7.0-mysql php7.0-common php7.0-curl php7.0-cli php7.0-mcrypt php7.0-mbstring php7.0-dom php7.0-gd
3、配置PHP7的配置文件
- 打开php.ini配置文件,设置cgi.fix_pathinfo=0:
sudo vim /etc/php/7.0/fpm/php.ini
找到cgi.fix_pathinfo选项,去掉注释;,然后将值设置为0 (这个操作是为了避免PHP7的一个漏洞,PS:vim使用“/”进入查找模式)
cgi.fix_pathinfo=0
- 打开php.ini配置文件,设置cgi.fix_pathinfo=0:
4、更新软件包,安装最新版的nginx
sudo apt-get update
sudo apt-get install nginx
3.安装mysql(5.5.3)
sudo apt-get install mysql-server-5.5 mysql-client-5.5
途中会提示设置MySQL的密码,安装好后:mysql -uroot -p
然后输入刚刚设置的密码,能成功进入即成功安装
添加nginx(版本1.11.6)配置文件,建立站点
1、用户属组的修改:nginx配置文件在
/etc/nginx/nginx.conf
中,默认的一行为
user root
,这个要和网站的用户属组相同。将user root
改成user www-data
,进入到网站目录,比如在/var/www/website
,进入到website,执行sudo chown -R www-data:www-data website
2、进入到conf.d目录,添加新的站点配置文件test.conf
cd /etc/nginx/conf.d
sudo vim test.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/website/;
index index.html index.htm index.php;
try_files $uri $uri/ /index.php?$query_string;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/website$fastcgi_script_name;
include fastcgi_params;
}
}
需要注意的是
- 1、
root /var/www/website/;
中填写网站所在目录 2、有两种方式调用fastcgi程序,依据是查看www.conf的listen方式,
执行命令
cat /etc/php/7.0/fpm/poll.d/www.conf,如果写的是
listen = /run/php/php7.0-fpm.sock那么nginx配置文件中写
fastcgi_pass unix:/run/php/php7.0-fpm.sock;`如果配置文件中写的是
listen=127.0.0.1:9000
,那么nginx配置文件中写fastcgi_pass 127.0.0.1:9000;
- 配置错误的话会出现502错误
- 3、
fastcgi_param SCRIPT_FILENAME /var/www/website$fastcgi_script_name;
中要填写网站所在目录,否则会出现file not found
的错误
- 1、
以上都配置成功后,
重启nginx: sudo service restart
重启php: sudo service php7.0-fpm restart
访问你的IP,即可访问到/var/www/website下的index.php