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

安装 nginx

  1. sudo apt-get update

  2. sudo apt-get install nginx

  3.  

  4. 安装完成后会自动开启,通过命令查看

  5. # ps -aux |grep nginx

  6. root      7921  0.0  0.0  14232   972 pts/0    S+   21:25   0:00 grep --color=au

  7. to nginx

  8. root     27770  0.0  0.0 117084  1444 ?        Ss   20:39   0:00 nginx: master p

  9. rocess /usr/sbin/nginx -g daemon on; master_process on;

  10. www-data 27771  0.0  0.0 117404  3084 ?        S    20:39   0:00 nginx: worker p

  11. rocess

  12. www-data 27772  0.0  0.1 117692  5120 ?        S    20:39   0:00 nginx: worker p

  13. rocess

  14.  

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

安装 PHP7

  1. sudo apt-get install python-software-properties software-properties-common

  2. sudo add-apt-repository ppa:ondrej/php

  3. 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

配置 nginx 解析 PHP

  1. vi /etc/nginx/sites-available/default

  2. server {

  3.    listen 80 default_server;

  4.    listen [::]:80 default_server ipv6only=on;

  5.  

  6.    #root 为项目代码所在目录

  7.    root /var/www/html;

  8.    index index.php index.html index.htm;

  9.  

  10.    #填写购买的阿里云外网IP或者自己已经解析备案的域名

  11.    server_name http://x.x.x.x/;

  12.  

  13.    #更改未被注释一行

  14.    location / {

  15.        # First attempt to serve request as file, then

  16.        # as directory, then fall back to displaying a 404.

  17.        try_files $uri $uri/ =404;

  18.        # Uncomment to enable naxsi on this location

  19.        # include /etc/nginx/naxsi.rules

  20.    }

  21.    #添加下面代码

  22.        location ~ \.php$ {

  23.               try_files $uri $uri/ =404;

  24.               fastcgi_split_path_info ^(.+\.php)(/.+)$;

  25.               fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;

  26.               fastcgi_index index.php;

  27.               fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

  28.               include fastcgi_params;

  29.        }

  30. }

  31.  

  32. sudo vi /etc/php/7.0/fpm/php.ini

  33. cgi.fix_pathinfo=0

  34.  

  35. sudo phpenmod mcrypt

  36. sudo service php7.0-fpm restart

  37. sudo service nginx restart

  38. vi test.php

  39. <?php

  40. phpinfo();

安装 MySQL

  1. # 默认安装 MySQL 5.7

  2. sudo apt-get install mysql-server mysql-client

  3. #设置好密码后登陆

  4. mysql -u root -p

安装 redis

  1. sudo apt-get install git

  2. git clone -b php7 https://github.com/phpredis/phpredis.git

  3. mv phpredis/ /etc/

  4. cd /etc/phpredis

  5. sudo apt install php7.0-dev

  6. #生成编译文件

  7. phpize

  8. sudo ./configure

  9. make && make install

  10. find / -name redis.so

  11. /etc/phpredis/.libs/redis.so

  12. /etc/phpredis/modules/redis.so

  13. /usr/lib/php/20151012/redis.so

  14.  

  15. vim /etc/php/7.0/apache2/php.ini

  16. extension=/etc/phpredis/modules/redis.so

  17. sudo service php7.0-fpm restart

  18. ps -aux |grep redis

  19. root      7981  0.0  0.0  14232  1024 pts/0    S+   21:40   0:00 grep --color=auto redis

  20. redis    30952  0.0  0.1  40136  6716 ?        Ssl  20:59   0:02 /usr/bin/redis-server 127.0.0.1:6379

  21. redis-cli

  22. 127.0.0.1:6379> info

  23. # Server

  24. redis_version:3.0.6

  25. redis_git_sha1:00000000

  26. redis_git_dirty:0

  27. redis_build_id:687a2a319020fa42

  28. redis_mode:standalone

  29. os:Linux 4.4.0-79-generic x86_64

  30. arch_bits:64

  31. multiplexing_api:epoll

  32. gcc_version:5.3.1

  33. process_id:30952

  34. run_id:056f34665607470e822036784d93cbbe079080a7

  35. tcp_port:6379

  36. uptime_in_seconds:2792

  37. uptime_in_days:0

  38.  

  39. vi test.php

  40. <?php

  41.  

  42. //连接本地Redis服务  

  43. $redis=new Redis();

  44. $redis->connect('127.0.0.1','6379');

上传代码

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