{"id":1358,"date":"2013-09-27T10:15:24","date_gmt":"2013-09-27T08:15:24","guid":{"rendered":"http:\/\/www.extradrm.com\/?p=1358"},"modified":"2013-09-27T10:30:25","modified_gmt":"2013-09-27T08:30:25","slug":"install-and-setup-an-apache-server-on-ubuntu","status":"publish","type":"post","link":"https:\/\/www.extradrm.com\/?p=1358","title":{"rendered":"Install and setup an apache web server with mysql on Ubuntu"},"content":{"rendered":"<h2>Install Lamp server from iso distrib Ubuntu 12.04<\/h2>\n<p>By default the address of your local webserver is localhost or 127.0.0.1 if you want to use an IP, you can type it as the address and reach your document root. If you have created a dir under the DR called test (as in \/var\/www\/test\/), navigate to the content of this folder simply by going to http:\/\/localhost\/test\/ or http:\/\/127.0.0.1\/test\/ in your web browser. In a nutshell every web address is relative to the DR.<br \/>\nType the following command :<\/p>\n<pre># sudo tasksel install lamp-server\r\n\r\n<\/pre>\n<hr\/>\n<h2>Apache2 Performance parameters<\/h2>\n<p>Open file http.conf file as shown under \/etc\/apache2\/ below<\/p>\n<p><a href=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/09\/apache1.jpg\"><img loading=\"lazy\" alt=\"apache1\" src=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/09\/apache1.jpg\" width=\"596\" height=\"256\" \/><\/a><\/p>\n<p><a href=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/09\/apache2.jpg\"><img loading=\"lazy\" alt=\"apache2\" src=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/09\/apache2.jpg\" width=\"451\" height=\"254\" \/><\/a><\/p>\n<pre><\/pre>\n<hr \/>\n<h2>Apache2 Permissions<\/h2>\n<p>www-data is a user\/group set created specifically for web servers. It should be listed in \/etc\/passwd as a user, and can be configured to run as another user in \/etc\/apache2\/apache2.conf.<br \/>\nBasically, it\u2019s just a user with stripped permissions so if someone managed to find a security hole in one of your web applications they wouldn\u2019t be able to do much. Without a lower-user like www-data set, apache2 would run as root, which would be a Bad Thing\u00ae since it would be able to do anything and everything to your system.<\/p>\n<p><strong>www-data or daemon :<\/strong><br \/>\nWhat version of ubuntu are you using?<br \/>\nhttpd.conf shouldn\u2019t be in use. It should be \/etc\/apache2\/envvars<\/p>\n<pre>Code:\r\n# envvars - default environment variables for apache2ctl\r\n\r\n# Since there is no sane way to get the parsed apache2 config in scripts, some\r\n# settings are defined via environment variables and then used in apache2ctl,\r\n# \/etc\/init.d\/apache2, \/etc\/logrotate.d\/apache2, etc.\r\nexport APACHE_RUN_USER=www-data\r\nexport APACHE_RUN_GROUP=www-data\r\nexport APACHE_PID_FILE=\/var\/run\/apache2.pid<\/pre>\n<p>And yes, www-data is system user, created by apache.<br \/>\nHow are you checking users? are you just issuing \u2018users\u2019 from the command line?<br \/>\n<strong>If you are you will only see real users there, not www-data because apache is a daemon running under the user www-data<\/strong><br \/>\nyou can<\/p>\n<pre> #cat \/etc\/shadow<\/pre>\n<p>and that is a grimmy way if listing all valid users.<\/p>\n<hr \/>\n<h2>Apache2 \/var\/www\/site1 Permissions<\/h2>\n<p><strong>Method 1<\/strong><\/p>\n<pre>sudo chown -R yourname:www-data site1<\/pre>\n<p>then<\/p>\n<pre>sudo chmod -R g+s site1<\/pre>\n<p>First command changes owner and group Second command adds s attribute which will keep new files and directories within <strong>site1<\/strong> having the same group permissions<\/p>\n<hr \/>\n<p><strong>Method 2<\/strong><br \/>\nIf you are the only developer, why not just make the owner of \/var\/www\/ you?<\/p>\n<pre>$ sudo chown -R mike \/var\/www\/<\/pre>\n<p>and set permissions to 755 for directories and 644 for files.<br \/>\nAfter this you would want to do this to set the right permissions for your files and directories.<br \/>\nto change all the directories to 755:<\/p>\n<pre>find \/var\/www -type d -exec chmod 755 {} \\;<\/pre>\n<p>to change all the files to 644:<\/p>\n<pre>find \/var\/www -type f -exec chmod 644 {} \\;<\/pre>\n<hr \/>\n<p><strong>Method 3<\/strong><\/p>\n<pre>you@yourbox:~$ sudo -i\r\n[sudo] password for you: [password]\r\nroot@yourbox:~# cd \/var\/www\r\nroot@yourbox: \/var\/www# mkdir site1\r\nroot@yourbox: \/var\/www# chown you:www-data site1\r\nroot@yourbox: \/var\/www# chmod 0750 site1\r\nroot@yourbox: \/var\/www# logout\r\nyou@yourbox:~$ ln -s \/var\/www\/site1 \/home\/you\/site1\r\nyou@yourbox:~$ exit<\/pre>\n<hr \/>\n<p><strong>Method 4<\/strong><\/p>\n<ul>\n<li><em>Create a new group (www-pub) and add the users to that group<\/em><\/li>\n<li><code>groupadd www-pub<\/code><code>usermod -a -G www-pub usera<\/code> <em>## must use -a to append to existing group<\/em><\/li>\n<li><em>s<\/em><code>usermod -a -G www-pub userb<\/code><code>groups usera<\/code> <em>## display groups for user<\/em><\/li>\n<li><em>Change the ownership of everything under \/var\/www to root:www-pub<\/em><\/li>\n<li><code>chown -R root:www-pub \/var\/www<\/code> <em>## -R for recursive<\/em><\/li>\n<li><em>Change the permissions of all the folders to 2775<\/em><\/li>\n<li><code>chmod 2775 \/var\/www<\/code> <em>## 2=set group id, 7=rwx for owner (root), 7=rwx for group (www-pub), 5=rx for world (including apache www-data user) <\/em>Set group ID (SETGID) bit (2) causes the group (www-pub) to be copied to all new files\/folders created in that folder. Other options are SETUID (4) to copy the user id, and STICKY (1) which I think lets only the owner delete files.There\u2019s a -R recursive option, but that won\u2019t discriminate between files and folders, so you have to <strong>use find<\/strong>, like so:<\/li>\n<li><code>find \/var\/www -type d -exec chmod 2775 {} +<\/code><\/li>\n<li><em>Change all the files to 0664<\/em><\/li>\n<li><code>find \/var\/www -type f -exec chmod 0664 {} +<\/code><\/li>\n<li><em>Change the umask for your users to 0002 <\/em>The umask controls the default file creation permissions, 0002 means files will have 664 and directories 775. Setting this (by editing the <code>umask<\/code> line at the bottom of <code>\/etc\/profile<\/code> in my case) means files created by one user will be writable by other users in the www-group without needing to <code>chmod<\/code> them.<\/li>\n<li>Possible addition \u2013 set cache\/upload dirs that need to be written to by the webserver to www-data:www-data and 775<\/li>\n<\/ul>\n<p>Test all this by creating a file and directory and verifying the owner, group and permissions with <strong><code>ls -l<\/code><\/strong><\/p>\n<hr \/>\n<h3>\u00a0Create a new user who will be a webmaster<\/h3>\n<pre>sudo adduser &lt;login&gt;<\/pre>\n<h3>Create a webadmin group<\/h3>\n<pre>sudo addgroup www-admin<\/pre>\n<h3>Then Add users to the group<\/h3>\n<pre>sudo adduser &lt;login&gt; www-admin<\/pre>\n<h3>Permissions on existing files<\/h3>\n<pre>sudo chown -R root:www-admin \/var\/www\r\nsudo chmod -R 0664 \/var\/www<\/pre>\n<h3>Permissions on existing folders<\/h3>\n<pre>cd \/var\/www\r\nsudo find . -type d -exec chmod 2775 {} \\;<\/pre>\n<h3>Admin web Umask for same site<\/h3>\n<p>Change file of concerned users (Admins)<\/p>\n<pre>nano \/home\/&lt;login&gt;\/.profile<\/pre>\n<pre>umask 002<\/pre>\n<h3>Special permissions for folders where web server must write :<\/h3>\n<p>\u2026for example <strong>\/wp-content\/uploads<\/strong> for WordPress<\/p>\n<pre>sudo chown -R www-data:www-admin \/var\/www\/wp-content\/uploads\r\nsudo chmod -R 775 \/var\/www\/wp-content\/uploads<\/pre>\n<hr \/>\n<h2>Method 6 : Set Web Directory User and Permissions<\/h2>\n<p>Now that you have everything installed and configured you\u2019ll need to set up some file permissions to allow Apache to read from, and you to write to, the web directory. This will be accomplished by changing the owner of the web directory, adding the Apache user and your user to this group and setting the guid bit forcing all new files\/folders to have the same group permissions.<\/p>\n<h3>Create a new group:<\/h3>\n<pre>$ sudo addgroup webdev<\/pre>\n<h3>Change the group of your web directory:<\/h3>\n<pre>$ sudo chgrp -R webdev \/var\/www\/\r\n$ sudo chmod -R g+rw \/var\/www\/<\/pre>\n<h3>Set the guid bit on all folders in your web directory:<\/h3>\n<pre>$ sudo find \/var\/www -type d -exec chmod +s {} \\;<\/pre>\n<h3>Add Apache to the webdev group:<\/h3>\n<pre>$ sudo usermod -a -G webdev www-data<\/pre>\n<h3>Add your user to the webdev group:<\/h3>\n<pre>$ sudo usermod -a -G webdev &lt;user_name&gt;<\/pre>\n<hr \/>\n<h2>Usual WordPress Permissions for LAMP (Ubuntu)<\/h2>\n<pre>find -type d -exec chmod 755 {} \\;\r\nfind -type f -exec chmod 664 {} \\;\r\nchmod 770 -R wp-content\/uploads\r\nchmod 770 -R w-content\/upgrade\r\nchmod 750 wp-content\/plugins<\/pre>\n<hr \/>\n<h2>Running several name-based web sites on a single IP address<\/h2>\n<p>In apache you have to create a new file under \u201c\/etc\/apache2\/sites-available\u201d. There you can create a new vhost file. Give them a name you want.<br \/>\n<a href=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/09\/apache9.jpg\"><img loading=\"lazy\" alt=\"apache9\" src=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/09\/apache9.jpg\" width=\"485\" height=\"274\" \/><\/a><\/p>\n<p>Add ip to etc\/hosts file<\/p>\n<p><a href=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/09\/apache8.jpg\"><img loading=\"lazy\" alt=\"apache8\" src=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/09\/apache8.jpg\" width=\"335\" height=\"222\" \/><\/a><\/p>\n<p>and copy\/uddate web files to \/var\/www\/<\/p>\n<p><a href=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/09\/apache6.jpg\"><img loading=\"lazy\" alt=\"apache6\" src=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/09\/apache6.jpg\" width=\"345\" height=\"164\" \/><\/a><\/p>\n<p>Put you vhost information in this file something like this:<br \/>\n<a href=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/09\/apache4.jpg\"><img loading=\"lazy\" alt=\"apache4\" src=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/09\/apache4.jpg\" width=\"397\" height=\"268\" \/><\/a><\/p>\n<p>After that you have to enable the vhost.<\/p>\n<pre>a2ensite example.com<\/pre>\n<p>With this command you create a symlink from \u201csites-available\u201d to \u201csites-enabled\u201d<br \/>\nthen reload you webserver<\/p>\n<pre>\/etc\/init.d\/apache2 reload<\/pre>\n<p>and it should work.<\/p>\n<hr \/>\n<p><strong>How to check which apache modules are enabled\/installed ?<\/strong><br \/>\nOn Ubuntu use:<\/p>\n<pre>apache2ctl -M<\/pre>\n<p><strong>Enabling sites and apache mods<\/strong><\/p>\n<p>Virtual hosts are added a little differently in Ubuntu with Apache2.<br \/>\n\u201csites-enabled\u201d and \u201csites-available\u201d will be the keywords if you need to search out that information.<\/p>\n<pre># a2ensite monsite.com<\/pre>\n<pre># a2enmode zzz<\/pre>\n<p><strong>disabling sites and apache mods<\/strong><\/p>\n<pre># a2dissite monsite.com<\/pre>\n<pre># a2dismode zzz<\/pre>\n<hr \/>\n<h2>Create the WordPress Database and User<\/h2>\n<hr \/>\n<p>After we unzip the wordpress files, they will be in a directory called wordpress in the home directory.<\/p>\n<p>Now we need to switch gears for a moment and create a new MySQL directory for wordpress.<\/p>\n<p>Go ahead and log into the MySQL Shell:<\/p>\n<pre>mysql -u root -p<\/pre>\n<p>Login using your MySQL root password, and then we need to create a wordpress database, a user in that database, and give that user a new password. Keep in mind that all MySQL commands must end with semi-colon.<\/p>\n<p>First, let\u2019s make the database (I\u2019m calling mine wordpress for simplicity\u2019s sake; feel free to give it whatever name you choose):<\/p>\n<pre>CREATE DATABASE wordpress;\r\nQuery OK, 1 row affected (0.00 sec)<\/pre>\n<p>Then we need to create the new user. You can replace the database, name, and password, with whatever you prefer:<\/p>\n<pre>CREATE USER wordpressuser@localhost;\r\nQuery OK, 0 rows affected (0.00 sec)<\/pre>\n<p>Set the password for your new user:<\/p>\n<pre>SET PASSWORD FOR wordpressuser@localhost= PASSWORD(\"password\");\r\nQuery OK, 0 rows affected (0.00 sec)<\/pre>\n<p>Finish up by granting all privileges to the new user. Without this command, the wordpress installer will not be able to start up:<\/p>\n<pre>GRANT ALL PRIVILEGES ON wordpress.* TO wordpressuser@localhost IDENTIFIED BY 'password';\r\nQuery OK, 0 rows affected (0.00 sec)<\/pre>\n<p>Then refresh MySQL:<\/p>\n<pre>FLUSH PRIVILEGES;\r\nQuery OK, 0 rows affected (0.00 sec)<\/pre>\n<p>Exit out of the MySQL shell:<\/p>\n<pre>exit<\/pre>\n<hr \/>\n<p>Cheers,<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Install Lamp server from iso distrib Ubuntu 12.04 By default the address of your local webserver is localhost or 127.0.0.1 if you want to use an IP, you can type it as the address&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":2843,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,209,14],"tags":[214,231,232],"youtube_video":null,"_links":{"self":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts\/1358"}],"collection":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1358"}],"version-history":[{"count":0,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts\/1358\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/media\/2843"}],"wp:attachment":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}