阿里云 Ubuntu PHP7 Nginx Mysql 开发环境搭建

服务器

浏览数:242

2019-3-14

记录下在阿里云搭建 PHP7 开发环境

安装 nginx

sudo apt-get update 
sudo apt-get install nginx

安装完成后会自动开启,通过命令查看
# ps -aux |grep nginx
root      7921  0.0  0.0  14232   972 pts/0    S+   21:25   0:00 grep --color=au
to nginx
root     27770  0.0  0.0 117084  1444 ?        Ss   20:39   0:00 nginx: master p
rocess /usr/sbin/nginx -g daemon on; master_process on;
www-data 27771  0.0  0.0 117404  3084 ?        S    20:39   0:00 nginx: worker p
rocess
www-data 27772  0.0  0.1 117692  5120 ?        S    20:39   0:00 nginx: worker p
rocess

浏览器打开 http://x.x.x.x.com/ 可以看到 nginx 的欢迎页面

安装 PHP7

sudo apt-get install python-software-properties software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt-get install php7.0-fpm php7.0-cli php7.0-mcrypt php7.0-mysql php7.0-mbstring php7.0-xml php7.0-curl
开启OPcache
apt-get install php70-php-opcache.x86_64
sudo service php7.0-fpm restart

配置 nginx 解析 PHP

vi /etc/nginx/sites-available/default
server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    #root 为项目代码所在目录
    root /var/www/html;
    index index.php index.html index.htm;

    #填写购买的阿里云外网IP或者自己已经解析备案的域名
    server_name http://x.x.x.x/;

    #更改未被注释一行
    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
        # Uncomment to enable naxsi on this location
        # include /etc/nginx/naxsi.rules
    }
    #添加下面代码
        location ~ \.php$ {
               try_files $uri $uri/ =404;
               fastcgi_split_path_info ^(.+\.php)(/.+)$;
               fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
               fastcgi_index index.php;
               fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
               include fastcgi_params;
        }
}

sudo vi /etc/php/7.0/fpm/php.ini
cgi.fix_pathinfo=0

sudo phpenmod mcrypt
sudo service php7.0-fpm restart
sudo service nginx restart
vi test.php
<?php
phpinfo();

安装 MySQL

# 默认安装 MySQL 5.7
sudo apt-get install mysql-server mysql-client
#设置好密码后登陆
mysql -u root -p

安装 redis

sudo apt-get install git
git clone -b php7 https://github.com/phpredis/phpredis.git
mv phpredis/ /etc/
cd /etc/phpredis
sudo apt install php7.0-dev
#生成编译文件
phpize
sudo ./configure
make && make install
find / -name redis.so
/etc/phpredis/.libs/redis.so
/etc/phpredis/modules/redis.so
/usr/lib/php/20151012/redis.so

vim /etc/php/7.0/apache2/php.ini 
extension=/etc/phpredis/modules/redis.so
sudo service php7.0-fpm restart
ps -aux |grep redis
root      7981  0.0  0.0  14232  1024 pts/0    S+   21:40   0:00 grep --color=auto redis
redis    30952  0.0  0.1  40136  6716 ?        Ssl  20:59   0:02 /usr/bin/redis-server 127.0.0.1:6379
redis-cli
127.0.0.1:6379> info
# Server
redis_version:3.0.6
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:687a2a319020fa42
redis_mode:standalone
os:Linux 4.4.0-79-generic x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:5.3.1
process_id:30952
run_id:056f34665607470e822036784d93cbbe079080a7
tcp_port:6379
uptime_in_seconds:2792
uptime_in_days:0

vi test.php
<?php

//连接本地Redis服务  
$redis=new Redis();
$redis->connect('127.0.0.1','6379');

上传代码

scp -r . root@x.x.x.x:/var/www/html

参考
阿里云Ubuntu14+PHP7+Nginx+Mysql环境搭建
在阿里云上部署Laravel应用
阿里云ECS部署Laravel+PHP7+Nginx
Nginx配置https访问