. Configure a local WordPress development on macOS from scratch Published: 2018.04.29 8 minutes read So you are a user and you want to configure a local environment to build a project.
Great choice, it is a fantastic piece of software! There are plenty of tools that let you set it up in no time at no cost — and are probably the best choices for beginners. Smashing Magazine just published an article called which is a great guide that takes you through the journey when using these kinds of tools. There is one disadvantage though — applications like these hide lots of important details from the user and come pre-bundled with lots of stuff that you just don’t need to run a WordPress website. My approach is a little bit more complicated but gives you enough knowledge about the environment to walk away confidently. An, and is all that we need and, believe it or not, your Apple computer comes with the majority of these elements baked in. Configure an Apache HTTP server and enable PHP The Apache HTTP Server and PHP language are already on your machine.
The Best Mac RSS Clients for News on Your Desktop The Best Mac RSS Clients for News on Your Desktop If you’re looking for a fast way to get news updates from specific sources, RSS is still the best way to go about it.
You can confirm they are installed by checking the current version for each of them in the command line. Apachectl -v php -v Before we run the server we need to make a tiny adjustment in its configuration file. To do so, feel free to use your favourite text editor such as nano, vim, subl or my beloved code. Superuser privileges ( sudo) will be needed. Sudo nano /etc/apache2/httpd.conf This is the main Apache configuration file that contains tons of helpful comments about all the available directives. We need to proceed with a few tweaks here:. Enable vhost.
Enable rewrites. Enable PHP.
Change the default location for our projects. Enable.htaccess Enable vhost (Virtual Host) There is a chance that you are going to work on multiple WordPress projects in the future and it would be cool to access them via custom domains (i.e. Somewebsite.localhost or anotherone.localhost).
Is a term that describes exactly this functionality. To enable it uncomment LoadModule vhostaliasmodule libexec/apache2/modvhostalias.so (line 167 in my case) and Include /private/etc/apache2/extra/httpd-vhosts.conf (line 516 in my case) inside your Apache configuration file. Old: #LoadModule vhostaliasmodule libexec/apache2/modvhostalias.so new: LoadModule vhostaliasmodule libexec/apache2/modvhostalias.so old: #Include /private/etc/apache2/extra/httpd-vhosts.conf new: Include /private/etc/apache2/extra/httpd-vhosts.conf Enable rewrites By default follows the filesystem path. For example the URL to a page about your company may end up being mycompany.com/about.php. In the case of WordPress we will more likely see something like mycompany.com/?p=1. Wouldn’t it be cool to simplify it to mycompany.com/about?
This is the reason why we need to explicitly enable it. Uncomment LoadModule rewritemodule libexec/apache2/modrewrite.so (line 175 in my case). Old: #LoadModule rewritemodule libexec/apache2/modrewrite.so new: LoadModule rewritemodule libexec/apache2/modrewrite.so Enable PHP WordPress is written in PHP. That being so our server surely needs to know how to deal with.php files. It is as easy as uncommenting LoadModule php7module libexec/apache2/libphp7.so (line 176 in my case).
Old: #LoadModule php7module libexec/apache2/libphp7.so new: LoadModule php7module libexec/apache2/libphp7.so Change the default location for our projects Personally I store the source files to all websites that I am working on inside a Sites folder in my home directory. It is not a requirement, just a convention. The default root directory for the Apache server is /Library/WebServer/Documents. We have to amend this path (line 244 and 255 in my case). Please, be sure to change the name of your username folder — the chances that your directory is called pawelgrzybek are slim!
Old: DocumentRoot '/Library/WebServer/Documents' old: new: DocumentRoot '/Users/pawelgrzybek/Sites/' new: Enable.htaccess To easily change the server configuration on a per-directory basis, Apache uses. The AllowOverride controls section (line 266 in my case) of the configuration file allows us to enable the use of.htaccess files. Edit the value of AllowOverride from None to All. We are done here! Old: AllowOverride None new: AllowOverride All Start, stop, restart and test apache server config I know it is a little bit daunting but I promise that we’ll never come back to this nasty lengthy configuration file again.
Four simple commands are everything that we need to remember from now on. Start, stop, restart and configuration test. Sudo apachectl start sudo apachectl stop sudo apachectl restart sudo apachectl configtest Hopefully the commands are self-explanatory. Please bare in mind that every single change of the configuration file requires rebooting of the server — sudo apachectl restart comes in very handy for this so do it now please. A good practice is to run a sanity check beforehand by executing sudo apachectl configtest. Now let’s create a test index.php file in the root directory that we specified in the Apache configuration file ( /Users/pawelgrzybek/Sites/index.php in my case).
DocumentRoot '/Users/pawelgrzybek/Sites/wp.localhost' ServerName wp.localhost ErrorLog '/var/log/apache2/wp.localhost-errorlog' The DocumentRoot tells Apache which directory the domain specified under the ServerName should be pointing to. The ErrorLog enables any error log files for this website (this may be helpful for debugging in the future). Time for a quick Apache restart and check if everything is working as expected. Sudo apachectl restart Helpful tip As a front end developer, the majority of the time I use some node-based servers for my local environment. I rarely build WordPress projects so there is no need for me to keep Apache and MySQL always running in a background. I created two quick bash aliases that enable / disable those tools for me in a blink of an eye.
If you like my approach, add it to your.bashprofile file.