Ubuntu installs the PHP and PHP Nginx configuration methods

  • 2020-06-12 11:40:35
  • OfStack

Recently, I took over an PHP project without having done PHP before, so I started to learn from building PHP environment and wrote a tutorial about installing PHP for Ubuntu.

1. Delete the legacy PHP packages


sudo apt-get purge `dpkg -l | grep php| awk '{print $2}' |tr "\n" " "`
sudo apt autoremove

2. Add PPA


sudo apt-get install -y language-pack-en-base
sudo LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php
sudo apt-get update

3. Install PHP

PHP version 5.6


sudo apt-get install php5.6 php5.6-cli
sudo apt-get install php5.6-dev php5.6-fpm php5.6-cgi
sudo apt-get install php5.6-mbstring php5.6-mcrypt php5.6-mysql php5.6-gd php5.6-curl php5.6-memcache

PHP version 7.1


sudo apt-get install php7.1 php7.1-cli
sudo apt-get install php7.1-dev php7.1-fpm php7.1-cgi
sudo apt-get install php7.1-mysql php7.1-gd php7.1-curl php7.1-memcache

4. Nginx configuration


# Pass all .php files onto a php-fpm/php-fcgi server.
location ~ [^/]\.php(/|$) {
  fastcgi_split_path_info ^(.+?\.php)(/.*)$;
  if (!-f $document_root$fastcgi_script_name) {
    return 404;
  }
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  # fastcgi_pass unix:/run/php/php5.6-fpm.sock;
  fastcgi_pass unix:/run/php/php7.1-fpm.sock;
  fastcgi_index index.php;
  include fastcgi_params;
}

Related articles: