set up Yii2 with MAMP on macOS

 To set up Yii2 with MAMP on macOS, install MAMP and Composer, create your Yii2 project inside the MAMP htdocs directory, and configure your database.

1. Start MAMP and Check PHP
  • Open MAMP from your Applications folder and click Start Servers.
  • Ensure Apache and MySQL are running. MAMP normally runs Apache on port 8888 and MySQL on port 8889. [1, 2, 3]
  • Open your Terminal and verify that PHP is accessible and meets Yii2 requirements (PHP 5.4 or higher): [1, 2]
    bash
    php -v
    
    Use code with caution.
2. Install Composer (If Not Already Installed)
Yii2 uses Composer to manage its packages and dependencies. Install it globally by running these commands in your Terminal: [1, 2, 3]
bash
curl -sS https://getcomposer.org | php
sudo mv composer.phar /usr/local/bin/composer
Use code with caution.
3. Create the Yii2 Project
  • Navigate to your MAMP document root folder (htdocs):
    bash
    cd /Applications/MAMP/htdocs
    
    Use code with caution.
  • Run the Composer command to create a basic Yii2 project template (replace my-yii-app with your project name):
    bash
    composer create-project --prefer-dist yiisoft/yii2-app-basic my-yii-app
    
    Use code with caution.
  • Composer will download all files and dependencies. [1, 2, 3, 4, 5]
4. Configure the Database
  • Open phpMyAdmin via your MAMP interface or by visiting http://localhost:8888/phpMyAdmin/ (or your configured port/host). [1]
  • Create a new blank database, for example: yii2_db.
  • Open your project's database configuration file located at my-yii-app/config/db.php using a text editor.
  • Update the credentials to match MAMP's default MySQL settings: [1, 2]
    php
    return [
        'class' => 'yii\db\Connection',
        'dsn' => 'mysql:host=localhost;port=8889;dbname=yii2_db', // Note the MAMP port 8889
        'username' => 'root',
        'password' => 'root',
        'charset' => 'utf8',
    ];
    
    Use code with caution.
5. Run and Access Your Application
You can access your application in two ways:
  • Via MAMP Apache Server: Open your browser and go to:
    http://localhost:8888/my-yii-app/web/
    (Note: Pointing directly to the web subfolder is required for security in Yii2).
    [1, 2, 3]
  • Via Yii Built-in Development Server: Alternatively, navigate into your project folder via Terminal and run:
    bash
    cd /Applications/MAMP/htdocs/my-yii-app
    php yii serve
    
    Use code with caution.
    Then open http://localhost:8080 in your browser. [1, 2, 3, 4]
If you'd like, let me know:
  • Yii2 Basic or Advanced template?
  • Virtual Host configuration for clean URLs?
I can provide the next steps.

Post a Comment

0 Comments