Ubuntu web server configuration

Video loading...

  • 0:01
    [♪ music ♪] Drupalize Me
  • 0:07
    [♪ music ♪]
  • 0:13
    In a previous video, we looked at how to install a web server onto an Ubuntu desktop.
  • 0:20
    And in this video what we want to do is take it a little further and look at how we can go in and tweak the configuration for our server
  • 0:25
    so that we can fine tune it to meet our needs.
  • 0:28
    Now, before we tweak things, let's find out what versions we're working with.
  • 0:32
    So, in my local host root, web server is showing my version of Apache.
  • 0:36
    So, that's sort of convenient and handy but you won't necessarily always have that,
  • 0:39
    and we have other things.
  • 0:42
    So, I'm going to use the command line here and Apache 2 is—is Apache and if I do dash v
  • 0:47
    I can see what version I'm running.
  • 0:50
    This is a common thing with a lot of different kinds of applications.
  • 0:53
    I can do the same thing with PHP. So, PHP dash v—
  • 0:57
    and I can see that MySQL is not quite the same. It's not—you can't do a dash V.
  • 1:03
    I can't do MySQL dash v, but what I can do is actually when I log in to MySQL
  • 1:09
    if I just log in—and I'll just log in as the root user here
  • 1:12
    On the long in screen, once I'm in, it shows me the server version up here at the top, actually.
  • 1:19
    So that's quick and easy ways just from command line. Just so you get yourself oriented.
  • 1:26
    Another thing that we can look at, at least for Apache and MySQL is we can go over here into phpMyAdmin.
  • 1:31
    Which is installed on this web server.
  • 1:34
    So, if you use it, over here on the right hand side there's information about your MySQL version.
  • 1:39
    And then underneath it, actually, also shows which Apache version you're using as well.
  • 1:46
    So, if you have phpMyAdmin installed, that's a quick and easy way to see your,
  • 1:50
    you know, Apache and MySQL versions, at least.
  • 1:56
    All right, so I'm going to kind of go back to where we were here.
  • 2:00
    But what I want to look at now is where the configuration files are and then we can go in and start playing with them.
  • 2:05
    And we're going to do most of this from command line, but I want to show it in the file browser first real quick.
  • 2:10
    Just to orient folks.
  • 2:14
    So, we'll go into the top level file system, like my root, in the file system. The /etc folder; /etc.
  • 2:19
    And then you'll notice there's like—so there's an Apache 2 folder, there's also PHP, and a MySQL folder.
  • 2:25
    And that's where our stuff is. This is all like the land of sudo and you need extra permissions to play with these.
  • 2:31
    So, we're going to do this from the command line, because it ends up sort of being the most efficient and sort of comfortable.
  • 2:37
    But, you can do it either way.
  • 2:39
    But, we're going to show this on command line.
  • 2:40
    So, I'm going to cd into the /etc directory. /etc.
  • 2:45
    And again, so here's an ls of all the kinds—all kinds of configuration in here.
  • 2:51
    Like, there's MySQL and the other ones we're concerned with, of course, are Apache and PHP.
  • 2:57
    And so we're going to start off looking at Apache.
  • 2:59
    And before I actually make any configuration changes, I want to have a way to test—you know, something to see where my changes are taking effect.
  • 3:06
    You normally don't tweak your server unless you're doing it for a reason.
  • 3:09
    So, I'm going to go into my web server here. And I have a Drupal site installed.
  • 3:13
    It could be any CMS. This is a common issue for many CMS'.
  • 3:17
    You want to have clean URL's, so you need to rewrite the URL's.
  • 3:21
    And we'll go in here and—on this sample site I have there's a clean URL test.
  • 3:27
    It's not able to do them right now. And when I run this test, it's failing.
  • 3:31
    So, this server is not set up to allow me to have clean URL's on my site right now
  • 3:36
    And that's what I want to fix by going in to change my Apache configuration.
  • 3:39
    So, we'll go back in here to /etc where all my configuration junk is.
  • 3:45
    And we need to get the Apache configuration. So, I'm going to cd into apache2 folder.
  • 3:53
    And then we'll list the files that we have here.
  • 3:57
    And once we—once we get in here you're going to see—
  • 4:00
    The way Ubuntu breaks things down is it has files broken out to handle the most common configuration tasks
  • 4:05
    so it's not all on just one file.
  • 4:08
    Which—a system you need to get used to.
  • 4:11
    So, there's this mods-available and there's sites-available.
  • 4:15
    So, that's for modules and sites, which would be vhosts.
  • 4:19
    http.conf is the classic configuration file,
  • 4:23
    but Ubuntu uses apache2.conf as the main configuration file.
  • 4:29
    But the others, like anything in conf.d mods and sites,
  • 4:34
    it's all sort of read into that main file.
  • 4:36
    So, you generally would not actually edit apache2.conf.
  • 4:40
    There's other places to edit things to keep it more organized and separated.
  • 4:47
    So, the configuration change we need to make is to enable the mod rewrite module
  • 4:51
    So, we're going to go into cd mods-available and list this.
  • 4:57
    And is this all of the different modules that are available that we can load into our Apache server.
  • 5:02
    And you can see that rewrite is listed here.
  • 5:06
    It's very commonly needed, but it's not enabled by default in the Ubuntu installation.
  • 5:12
    So, now if I cd back over to mods-enabled and we get that list, you'll see it's a much shorter list
  • 5:19
    And rewrite is not listed. It's not enabled.
  • 5:21
    So, it's available but it's not enabled.
  • 5:23
    And so we need to actually just go ahead and enable that rewrite module.
  • 5:28
    In order to do that, I'm going to go back over to mods-available.
  • 5:34
    So, I have my available list. I"m know I"m—make sure I'm typing things in correctly.
  • 5:40
    So I get back in. We'll do this list again so I can see exactly what I need to call it.
  • 5:47
    It's rewrite. And so the command to enable is—
  • 5:50
    I need to do this as sudo, and if I don't do it as sudo it's not going to work.
  • 5:57
    And we can—actually let's look at it and see. You can see what it is.
  • 6:00
    So a2enmmod—Apache 2 enable module.
  • 6:03
    The name of the module without the dot load at the end.
  • 6:06
    Just the name of the module. No dot stuff afterwards.
  • 6:10
    And, since I didn't do this as sudo, you can see it's telling me permissions are denied.
  • 6:16
    So, I'm getting an error. I often forget the sudo part when I get in to do this.
  • 6:20
    So, when you see that, you'll need to do sudo a2enmod. The name of the module spelled correctly.
  • 6:28
    Let's try rewrite. And then I'll hit enter.
  • 6:35
    My sudo password. Tada
  • 6:38
    So, it says it's enabled it, but I—also it tells me I need to run this command in order to restart the server.
  • 6:44
    Every time you make configuration changes, you have to restart that service.
  • 6:49
    So, I'm just going to copy what they told me to write here.
  • 6:54
    So, it's service apache2 restart
  • 6:56
    We'll just take her down and restart it.
  • 6:59
    Again, I have to do this as sudo
  • 7:04
    And now the—it's restarted and the new configuration is in effect.
  • 7:08
    Okay, and just so we can see that change, I'm going to go ahead and cd back over into mods-enabled.
  • 7:16
    And looked at the list that we have in here, and just verify that I can now see the rewrite is here.
  • 7:21
    So rewrite has been enabled and—so you don't ever do anything directly in the enabled folder,
  • 7:27
    always work in the available folder. And then Ubuntu will make the connections for you.
  • 7:32
    So, I'm going to go back over and see if this is working now.
  • 7:35
    And I'm going to run this test and it still isn't working.
  • 7:40
    And that's because when dealing with rewrite—and this is—the rewrites work off of an .htaccess file.
  • 7:46
    And so you need to have the .htaccess file being read properly.
  • 7:52
    Which is not happening yet on this server.
  • 7:55
    So, I'm going to need to go back to my Apache configuration.
  • 8:00
    And this time we're going to go look at the sites-available, sites-enabled. bit.
  • 8:06
    Because this has to do with our sites and the directives that are being sent
  • 8:10
    for actual websites, vhosts.
  • 8:14
    So, we just dealt with the mods, and you can see there is the sites
  • 8:18
    And again, we only want to work in sites-available., not in sites-enabled, directly.
  • 8:26
    And when we get into sites-available you'll see that there is a default, and that's all we need.
  • 8:30
    I"m not going to get into creating other vhosts.
  • 8:34
    So I want to modify this default site so I'm going to sudo vi
  • 8:39
    and use whatever editor you want. I just need to open this up and edit it, and I need to do it as the root user.
  • 8:45
    And this here is the directive that's causing us issues.
  • 8:51
    In the /var/www directory, which is our web root
  • 8:57
    right now we are not allowing any overrides to happen with the .htaccess system.
  • 9:03
    And so I need to change that. So I'm going to delete that and I'm going to change it from none to all.
  • 9:07
    Because I want my websites to be able to use .htaccess period.
  • 9:11
    I'm going to save that file. And now that I've changed that directive
  • 9:17
    so my default configuration for my webserver has that—
  • 9:20
    again I need to restart the server.
  • 9:23
    Every time you make configuration changes you need to restart the service.
  • 9:27
    And I used the same command before.
  • 9:29
    Now, when I run the clean URL test, bing, it's working.
  • 9:34
    So, that's how I could—and I'll go ahead and enable this.
  • 9:38
    So, that's basically how to get in and change some stuff in Apache.
  • 9:41
    And we can see that it's working because my website is actually working properly now.
  • 9:46
    Okay, so now we're going to come back out. That's Apache
  • 9:49
    Let's come back to our main /etc directory.
  • 9:52
    And we're going to look at MySQL and do some tweaks in there.
  • 9:57
    So, let's go into the MySQL folder and take a look.—
  • 10:01
    You have a couple of things, but really the main thing that we're going to be looking at—
  • 10:05
    the only thing we care about right now is my.cnf or my dot cnf
  • 10:08
    This is the main configuration file for MySQL
  • 10:13
    And again, like we do with Apache, let's set ourselves up
  • 10:18
    so we can actually see what—what we're changing, so we can see when it changes
  • 10:23
    to know that we did the configuration properly and it's taking effect.
  • 10:26
    Nothing more frustrating than making a bunch of configuration changes and it not taking effect because you did it wrong,
  • 10:32
    or the wrong file or something and not knowing that.
  • 10:35
    So we'll look at this, just two ways.
  • 10:38
    Basically, I want to run a query in my MySQL to give me some information.
  • 10:42
    So, now I can do this in phpMyAdmin and again this is if you're comfortable or not
  • 10:47
    with doing SQL on the command line
  • 10:50
    But, there's an SQL tab here, and I could type SQL commands directly
  • 10:55
    into the database and get information that I need.
  • 10:59
    Since we're kind of focused on command line stuff anyway within this
  • 11:03
    I'm going to do—I'm going to do this from command line, but it's exactly the same thing you would type into this SQL tab
  • 11:08
    on phpMyAdmin if you wanted to use that instead.
  • 11:14
    So, I—first thing I need to do to get to MySQL command line
  • 11:19
    for typing that in is to actually login to my SQL
  • 11:21
    So, I'm going to go ahead and go in, and I'll just go in as my root user here.
  • 11:25
    And now that I'm here—
  • 11:28
    what I want to do is I want to find what the setting currently is for the max allowed packet.
  • 11:35
    So, the command is show variables like, and then the actual variable name is
  • 11:41
    `max_allowed_packet. With—whoops—quote there.
  • 11:48
    Okay. And this gives me the value that's stored in MySQL right now.
  • 11:53
    This is the current setting. And that is 16 megs.
  • 11:57
    I know it's a lot of numbers but that's 16 megabytes.
  • 12:02
    So, that's the number I want to change.
  • 12:05
    I've had to do this before when I've had huge chunks of stuff in the database
  • 12:09
    and I can't import a database because I have big chunks of stuff that don't fit.
  • 12:14
    So, let me clear this screen out. I logged out of MySQL
  • 12:19
    Because we need to go back into the my.cnf file.
  • 12:22
    So, I'm going to go ahead and edit this.
  • 12:25
    And again, just remember, you have to do this as sudo.
  • 12:30
    Okay, so I have my file open and now the thing I need to find here is
  • 12:35
    the max allowed packet configuration line.
  • 12:37
    So, I'm just going to search for that in vi here.
  • 12:41
    With that—so max allowed packet. And you can see that it is set to 16 meg
  • 12:46
    which is what I got back when I did my query.
  • 12:50
    So, I'm just going to go in and I'm going to change it.
  • 12:53
    And we'll make it 32 megs.
  • 12:56
    And then I"ll go ahead and just save this file.
  • 13:00
    You might want to have saved this original as a backup and then—these are very simple changes.
  • 13:05
    I"m not too worried about my default configuration file.
  • 13:09
    Now, again, I need to restart the service. So, this applies to MySQL just as much as Apache.
  • 13:16
    So, I need to sudo service MySQL restart
  • 13:19
    to make my configuration changes actually take effect.
  • 13:24
    It's probably the number one problem. You make a bunch of changes but you don't restart the server, and you don't understand why it doesn't work.
  • 13:29
    We're going to go back into MySQL and I'm going to run the query again.
  • 13:35
    And see if it really did actually update.
  • 13:39
    So, once I'm at my prompt I just copy that in again
  • 13:42
    or just, you know, arrow up to get to the previous command I ran.
  • 13:47
    And now you can see that the value has changed and we're now at 32 megs.
  • 13:51
    Or 33 gamillion bytes, right?
  • 13:54
    So, I can actually see that the change took effect.
  • 14:00
    And it's that simple to go in, modify the conf file—my.cnf file—
  • 14:05
    It's knowing where it is and restarting the service once you've made your changes.
  • 14:09
    Okay, so last one that we have here is talking about PHP.
  • 14:16
    And you'll notice if I try to cd to PHP nothing happens.
  • 14:19
    Like Apache, I need to put the version number, PHP5
  • 14:22
    rather than say PHP4, which would be ancient.
  • 14:24
    And you'll notice that we have a couple directories in here.
  • 14:29
    And we want the one for Apache 2, because we're dealing with PHP as it is installed through Apache.
  • 14:35
    For our web server. Not as a command line interface just for PHP
  • 14:39
    if I just want to do straight PHP on command line.
  • 14:43
    So, the Apache folder is where the configuration I'm looking for right now for my web server is located.
  • 14:49
    And like we did previously, I want to have a way to see what's going on.
  • 14:52
    So, I'm going to go back to my web root /var/www.
  • 14:56
    And I"m going to create a file that will give me more information about PHP.
  • 15:00
    So, I'm going to just—vi and I'm going to create a new file.
  • 15:03
    And I'm calling it info.php. Call it whatever you want dot php.
  • 15:06
    And there's going to be one line in here, which is a snippet of PHP code.
  • 15:12
    Open php php.info and then end the php.
  • 15:17
    And save that file. That's it. That's the entire file, it ends with dot php.
  • 15:23
    And it's in my root here. So when I go back to local host—
  • 15:28
    Alright, so if I—here we go.
  • 15:30
    So, here's my web root and I stuck it in there. Just so it would be easy for me to get to.
  • 15:35
    And now I can click on this, and this pulls up a whole bunch of information.
  • 15:39
    So, here's another way to see the version of PHP.
  • 15:43
    If I have an info file hanging around.
  • 15:47
    And it just gives you a lot of information. It's really a wonderful little file to have handy.
  • 15:52
    Including, like, information about where my configuration file is, right.
  • 15:58
    So this is show that path that we're working from; /etc/php5/apache2.
  • 16:03
    But the thing that I want to change—what I'm looking for in this one is memory limit.
  • 16:08
    So, I'm just going to find on this browser page here memory limit.
  • 16:12
    Memory limit is also something that sometimes you just need to change.
  • 16:17
    By default, this is coming with 128 megs, which is a pretty good amount.
  • 16:22
    But you can definitely be writing or using applications—content management systems that need more memory
  • 16:28
    than that to function.
  • 16:30
    So that is the thing that we can see the initial setting, and I want to change.
  • 16:37
    So, let's go back to /etc/php5, where our configuration files are.
  • 16:43
    As I said, we want to go into /apache2, and then we have a php to ini file.
  • 16:48
    Again, I can copy this to make a backup. I'm just going to edit the ini file.
  • 16:54
    It's a very simple edit here. And again, just like the other,
  • 16:59
    we need to make the edit, restart the service, and then check.
  • 17:03
    So, I'm going to sudo vi the file.
  • 17:10
    And again, I'll just search through this file for the keyword I'm looking for here.
  • 17:14
    Which is, memory_limit. And this is my memory limit.
  • 17:21
    And I'll just go over here and change this.
  • 17:24
    I'll just up it to 256, say. We'll just double the memory on this.
  • 17:29
    And once again I'll save those changes to the file.
  • 17:32
    And once I've done that, of course, I also again need to restart the service.
  • 17:36
    Now, the thing is, that PHP does not have it's own service.
  • 17:39
    So, there's no sudo service php
  • 17:43
    PHP is running as part of Apache, so we need to restart the Apache service—Apache 2.
  • 17:48
    Restart and then once that goes—everything started up well—
  • 17:56
    What we want to do is check out changes.
  • 17:59
    All right, so I'm just going to go back to my info file.
  • 18:01
    I'll reload that info file page, and if we come down and look at this,
  • 18:05
    we can see that the memory limit is now at 256 megs. Success.
  • 18:10
    So, a couple of easy tweaks for your server.
  • 18:15
    The main thing to remember is, you know, where the files are located.
  • 18:18
    Make sure you're editing the correct file, which would be in our /etc directory.
  • 18:27
    And once you have the correct file, and you've made sure you changed it,
  • 18:30
    make sure you restart the service that control that configuration.
  • 18:34
    So, you need to restart Apache for Apache and PHP configuration.
  • 18:38
    And you need to restart MySQL for MySQL changes.
  • 18:44
    Hopefully that gives you enough to get going and fine tune that server just the way you want it.

Ubuntu Web Server Configuration


In this followup video to Installing a web server on Ubuntu we learn how to configure the various pieces of our local development web server. We'll get clean URLs working by enabling the Apache rewrite module, and increase various settings for MySQL and PHP.

Additional resources: 

More information on Development Environments