<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>permissions Archives - Alexandros Georgiou</title>
	<atom:link href="https://www.alexgeorgiou.gr/tag/permissions/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.alexgeorgiou.gr/tag/permissions/</link>
	<description>Balancing brackets for a living</description>
	<lastBuildDate>Wed, 20 Dec 2023 10:34:06 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://www.alexgeorgiou.gr/wp-content/uploads/2021/07/cropped-alexgeorgiou-icon-32x32.png</url>
	<title>permissions Archives - Alexandros Georgiou</title>
	<link>https://www.alexgeorgiou.gr/tag/permissions/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>💻 wp-cli executed by www-data user to avoid Linux permission issues</title>
		<link>https://www.alexgeorgiou.gr/wp-cli-www-data-user-permissions-linux/</link>
					<comments>https://www.alexgeorgiou.gr/wp-cli-www-data-user-permissions-linux/#comments</comments>
		
		<dc:creator><![CDATA[alexg]]></dc:creator>
		<pubDate>Wed, 26 Oct 2016 14:02:19 +0000</pubDate>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[permissions]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[wp-cli]]></category>
		<guid isPermaLink="false">http://www.alexgeorgiou.gr/?p=124</guid>

					<description><![CDATA[<p>I'm going to share with you a trick that I use to run wp-cli as the www-data user on my Linux development environment.</p>
<p>The post <a href="https://www.alexgeorgiou.gr/wp-cli-www-data-user-permissions-linux/">💻 wp-cli executed by www-data user to avoid Linux permission issues</a> appeared first on <a href="https://www.alexgeorgiou.gr">Alexandros Georgiou</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In this article I&#8217;m going to share with you a trick that I use to run <em>wp-cli</em> as the <code>www-data</code> user on my Linux development environment.</p>
<h2>wp-cli and file owner issues</h2>
<p>I can&#8217;t imagine doing any serious WordPress development without <em>wp-cli</em>. It lets you automate the <em>WordPress</em> installation and configuration, lets you install and manage themes and plugins, post articles, and even post file-based media such as images. Whether you run it from <em>grunt</em>, from shell scripts, <a href="/wordpress-testing-with-docker/">from inside docker containers</a>, or just from the plain old shell, <em>wp-cli</em> rocks!</p>
<p>One issue I often face is that when I run <em>wp-cli</em> as my default Linux user, naturally all the file operations write files owned by the current user. Sometimes this is an issue, as my web files are normally owned by user <code>www-data</code> (in the <code>www-data</code> group).</p>
<p>In the past that meant that I had to do this a lot, in order to avoid various permission issues:</p>
<pre>sudo chown -R www-data:www-data /var/www/wordpress</pre>
<p>It quickly becomes tedious. There&#8217;s a better way.</p>
<h2>finding your wp-cli installation</h2>
<p>If you&#8217;ve followed the installation instructions, then you probably have <code>wp-cli.phar</code> installed as <code>wp</code> at <code>/usr/local/bin/wp</code>. In any case, you can do</p>
<pre>which wp</pre>
<p>or</p>
<pre>whereis wp</pre>
<p>to find its location.</p>
<h2>running as www-data</h2>
<p>Now on to put a <a href="https://en.wikipedia.org/wiki/Setuid" target="_blank" rel="noopener noreferrer">setuid</a> flag on that file and change its owner to that of your web server (usually <code>www-data</code>), right?</p>
<p>Not so fast! The astute Linux aficionado will notice that <em>wp-cli</em> is not a binary, even though it&#8217;s placed under a <code>bin</code> dir. Actually it&#8217;s a PHP CLI script:</p>
<pre>$ head -n 1 `which wp`
#!/usr/bin/env php
$</pre>
<p>Here&#8217;s what you do instead. First, run <em>visudo</em> with elevated privileges:</p>
<pre>sudo visudo</pre>
<p>This will let you edit your sudoers file. You want to allow your current user to run your script as <code>www-data</code>. So, add the following line: (here I&#8217;m logged in as alex, the local user I use for development)</p>
<pre>alex ALL=(www-data) NOPASSWD: /usr/local/bin/wp</pre>
<p>Save the file and exit. Now you can do stuff like this:</p>
<pre>mkdir /tmp/t
cd /tmp/t
sudo -u www-data wp core download
ls -l</pre>
<p>You should see a WordPress installation owned by the <code>www-data</code> user, without ever having to enter a password for <code>www-data</code> (by default there shouldn&#8217;t be any password for that user anyway).</p>
<p>Of course, having to type <code>sudo -u www-data wp</code> every time is also tedious. So add this to your <code>~/.bashrc</code>, or better yet to your <code>~/.bash_aliases</code> if you have one.</p>
<pre>alias wp="sudo -u www-data wp"</pre>
<p>That should do it, assuming <em>bash</em> is your shell. For other shells, YMMV.</p>
<h2>wp-cli cache permission issue</h2>
<p>But now you might notice some warnings of the form:</p>
<pre>Warning: copy(/home/alex/.wp-cli/cache/plugin/XXXXXXXX.zip): failed to open stream: Permission denied in phar:///usr/local/bin/wp/php/WP_CLI/FileCache.php on line 164</pre>
<p>This just means that the script, which now runs as the <code>www-data</code> user, cannot write to its cache directory, because that directory is owned by you. You can fix this with a simple:</p>
<pre>sudo chown -R www-data:www-data ~/.wp-cli/</pre>
<p>Now you can use commands such as <code>wp plugin install foo.zip</code> <code>--activate </code>and <code>wp plugin uninstall foo --deactivate</code> in your dev-testing lifecycle without ever worrying about your web server choking on permission issues.</p>
<p>The post <a href="https://www.alexgeorgiou.gr/wp-cli-www-data-user-permissions-linux/">💻 wp-cli executed by www-data user to avoid Linux permission issues</a> appeared first on <a href="https://www.alexgeorgiou.gr">Alexandros Georgiou</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.alexgeorgiou.gr/wp-cli-www-data-user-permissions-linux/feed/</wfw:commentRss>
			<slash:comments>12</slash:comments>
		
		
			</item>
	</channel>
</rss>
