- Learning PHP 7
- Antonio Lopez
- 686字
- 2021-07-09 20:17:38
Setting up the environment on Ubuntu
Setting up your environment on Ubuntu is the easiest of the three platforms. In fact, you could take the provisioner.sh
script from the Setting up the environment with Vagrant section and execute it on your laptop. That should do the trick. However, just in case you already have some of the tools installed or you want to have a sense of control on what is going on, we will detail each step.
Installing PHP
The only thing to consider in this section is to remove any previous PHP versions on your system. To do so, you can run the following command:
$ sudo apt-get -y purge php.*
The next step is to add the necessary repositories in order to fetch the correct PHP version. The commands to add and update them are:
$ sudo apt-get install python-software-properties $ sudo LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php -y $ sudo apt-get update
Finally, we need to install PHP 7 together with the driver for MySQL. For this, just execute the following three commands:
$ sudo apt-get install php7.0 php7.0-fpm php7.0-mysql -y $ sudo apt-get --purge autoremove -y $ sudo service php7.0-fpm start
Installing MySQL
Installing MySQL manually can be slightly different than with the Vagrant script. As we can interact with the console, we do not have to specify the root password previously; instead, we can force MySQL to prompt for it. Run the following command and keep in mind that the installer will ask you for the password:
$ sudo apt-get -y install mysql-server mysql-client
Once done, if you need to start the MySQL server, you can do it with the following command:
$ sudo service mysql start
Installing Nginx
The first thing that you need to know is that you can only have one web server listening on the same port. As port 80
is the default one for web applications, if you are running Apache on your Ubuntu machine, you will not be able to start an Nginx web server listening on the same port 80
. To fix this, you can either change the ports for Nginx or Apache, stop Apache, or uninstall it. Either way, the installation command for Nginx is as follows:
$ sudo apt-get install nginx –y
Now, you will need to enable a site with Nginx. The sites are files under /etc/nginx/sites-available
. There is already one file there, default
, which you can safely replace with the following content:
server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /var/www/html; index index.php index.html index.htm; server_name server_domain_or_IP; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { try_files $uri /index.php =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; } }
This configuration basically points the root directory of your web application to the /var/www/html
directory. You can choose the one that you prefer, but make sure that it has the right permissions. It also listens on the port 80
, which you can change with the one you prefer; just keep this in mind that when you try to access your application via a browser. Finally, to apply all the changes, run the following command:
$ sudo service nginx restart
Tip
Downloading the example code
You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
You can download the code files by following these steps:
- Log in or register to our website using your e-mail address and password.
- Hover the mouse pointer on the SUPPORT tab at the top.
- Click on Code Downloads & Errata.
- Enter the name of the book in the Search box.
- Select the book for which you're looking to download the code files.
- Choose from the drop-down menu where you purchased this book from.
- Click on Code Download.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
- WinRAR / 7-Zip for Windows
- Zipeg / iZip / UnRarX for Mac
- 7-Zip / PeaZip for Linux
- 計算思維與算法入門
- Python自然語言處理實戰(zhàn):核心技術(shù)與算法
- Python Network Programming Cookbook(Second Edition)
- HDInsight Essentials(Second Edition)
- 零基礎(chǔ)學(xué)單片機C語言程序設(shè)計
- HTML5從入門到精通(第4版)
- 區(qū)塊鏈技術(shù)進(jìn)階與實戰(zhàn)(第2版)
- Kivy Cookbook
- SQL Server 入門很輕松(微課超值版)
- Python Machine Learning Blueprints:Intuitive data projects you can relate to
- Unity 2017 Game AI Programming(Third Edition)
- Java高手是怎樣煉成的:原理、方法與實踐
- HTML5與CSS3權(quán)威指南
- XML程序設(shè)計(第二版)
- FORTRAN程序設(shè)計權(quán)威指南