How to extend CentOS partition

標準

1) Identify the device name, which is by default /dev/sda, and confirm the new size by running the command:

# fdisk -l

2) Get list of partitions for /dev/sda device:

# ls -al /dev/sda*

brw-rw---- 1 root disk 8, 0 Dec 29 15:32 /dev/sda
brw-rw---- 1 root disk 8, 1 Dec 29 15:32 /dev/sda1
brw-rw---- 1 root disk 8, 2 Dec 29 15:32 /dev/sda2

3) Create new primary partition

# fdisk /dev/sda

Then type:
  n (enter) [create new partition]
  p (enter) [primary partition]
  3 (enter) [next available number from listed /dev/sda partitions in 2)
  (enter)   [start cylinder]
  (enter to use all available physical space) or specify size in +cylinders, +size{K,M,G}
  t (enter) [change partition type]
  3 (enter) [selecting /dev/sda3 partition]
  8e (enter) [this sets partition type to Linux LVM or type L then enter to see list of types] 
  w (enter)

  The partition table has been altered!

4) Reboot Centos 6.X then log back in with root privileges

# reboot

5) Check the new partition is ready and type ‘8e’:

# fdisk -l

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        2089    16264192   8e  Linux LVM
/dev/sda3            2089        3916    14678054   8e  Linux LVM

6) Create physical volume:

# pvcreate /dev/sda3

Physical volume "/dev/sda3" successfully created

7) Find out volume group name:

# vgdisplay

--- Volume group ---
VG Name               vg_app1
...

8) Extend the physical volume:

# vgextend vg_app1 /dev/sda3

Volume group "vg_app1" successfully extended

9) Extend the existing volume group to the new physical volume (+100%FREE can be altered to desired size). Since we are extending root partition hence pointing to lv_root in vg_app1 volume group.

# lvextend -l +100%FREE /dev/vg_app1/lv_root

Size of logical volume vg_app1/lv_root changed from 11.63 GiB (2978 extents) to 25.63 GiB (6561 extents).
Logical volume lv_root successfully resized

10) Resize logical root volume:

# resize2fs /dev/vg_app1/lv_root

resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg_app1/lv_root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/vg_app1/lv_root to 6718464 (4k) blocks.
The filesystem on /dev/vg_app1/lv_root is now 6718464 blocks long.

Note: Use ext2online instead of resize2fs if it is a Red Hat virtual machine.

11) Check available space:

# df -h

Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_app1-lv_root
                      26G   10G   14G  42% /
tmpfs                 9.8G     0  9.8G   0% /dev/shm
/dev/sda1             477M   88M  364M  20% /boot

Install Linux, Nginx, MySQL, PHP (LEMP) On CentOS 7

標準

Step One — Install Nginx

In order to display web pages to our site visitors, we are going to employ Nginx, a modern, efficient web server.

To add the CentOS 7 EPEL repository, open terminal and use the following command:

  • sudo yum install epel-release

Since we are using a sudo command, these operations get executed with root privileges. It will ask you for your regular user’s password to verify that you have permission to run commands with root privileges.

Now that the Nginx repository is installed on your server, install Nginx using the following yum command:

  • sudo yum install nginx

Afterwards, your web server is installed.

Once it is installed, you can start Nginx on your VPS:

  • sudo systemctl start nginx

You can do a spot check right away to verify that everything went as planned by visiting your server’s public IP address in your web browser (see the note under the next heading to find out what your public IP address is if you do not have this information already):

Open in a web browser:
http://server_domain_name_or_IP/

You will see the default CentOS 7 Nginx web page, which is there for informational and testing purposes.

Before continuing, you will want to do is enable Nginx to start on boot. Use the following command to do so:

  • sudo systemctl enable nginx

How To Find Your Server’s Public IP Address

If you do not know what your server’s public IP address is, there are a number of ways you can find it. Usually, this is the address you use to connect to your server through SSH.

From the command line, you can find this a few ways. First, you can use the iproute2 tools to get your address by typing this:

  • ip addr show eth0 | grep inet | awk ‘{ print $2; }’ | sed ‘s/\/.*$//’

This will give you one or two lines back. They are both correct addresses, but your computer may only be able to use one of them, so feel free to try each one.

Step Two — Install MySQL (MariaDB)

Now that we have our web server up and running, it is time to install MariaDB, a MySQL drop-in replacement. MariaDB is a community-developed fork of the MySQL relational database management system. Basically, it will organize and provide access to databases where our site can store information.

Again, we can use yum to acquire and install our software. This time, we’ll also install some other “helper" packages that will assist us in getting our components to communicate with each other:

  • sudo yum install mariadb-server mariadb

When the installation is complete, we need to start MariaDB with the following command:

  • sudo systemctl start mariadb

Now that our MySQL database is running, we want to run a simple security script that will remove some dangerous defaults and lock down access to our database system a little bit. Start the interactive script by running:

  • sudo mysql_secure_installation

The prompt will ask you for your current root password. Since you just installed MySQL, you most likely won’t have one, so leave it blank by pressing enter. Then the prompt will ask you if you want to set a root password. Go ahead and enter Y, and follow the instuctions:

mysql_secure_installation prompts:
Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

New password: password
Re-enter new password: password
Password updated successfully!
Reloading privilege tables..
 ... Success!

For the rest of the questions, you should simply hit the “ENTER" key through each prompt to accept the default values. This will remove some sample users and databases, disable remote root logins, and load these new rules so that MySQL immediately respects the changes we have made.

The last thing you will want to do is enable MariaDB to start on boot. Use the following command to do so:

  • sudo systemctl enable mariadb

At this point, your database system is now set up and we can move on.

 

Step Three — Install PHP

Install PHP 5.6:

wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm

wget http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

rpm -Uvh remi-release-7*.rpm epel-release-7*.rpm

If you already have EPEL installed:

wget http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

rpm -Uvh remi-release-7*.rpm

Enabling the Repo

Now we need to make sure the repo is enabled and select which version you want to install. We need to head over to /etc/yum.repos.d you should inside see a file called remi.repo.

Open the file in your favourite editor (Nano, Pico, Vi etc), you’ll see a number of sections. We need to make sure that the first section [remi] is enabled:

[remi]

name=Les RPM de remi pour Enterprise Linux 6 – $basearch

#baseurl=http://rpms.famillecollet.com/enterprise/6/remi/$basearch/

mirrorlist=http://rpms.famillecollet.com/enterprise/6/remi/mirror

enabled=1

gpgcheck=1

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

Note the line enabled=1 make sure this is set! Now technically you can actually go ahead and install PHP, but you will only get PHP 5.4.*. Which might be want to you want is so skip ahead to the next section!

If we want PHP 5.5 or PHP 5.6 we need to do a bit more work, further down in the repo.repo file you will see two additional sections [remi-php55] and [remi-php56], decide which PHP version you want to install and then enable the correct. So for PHP 5.6 we would change to:

[remi-php56]

name=Les RPM de remi de PHP 5.6 pour Enterprise Linux 6 – $basearch

#baseurl=http://rpms.famillecollet.com/enterprise/6/php56/$basearch/

mirrorlist=http://rpms.famillecollet.com/enterprise/6/php56/mirror

# WARNING: If you enable this repository, you must also enable “remi"

enabled=1

gpgcheck=1

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

Once you made your changes save your modified file and quit your editor.

Installing PHP

sudo yum install php php-gd php-mysql php-mcrypt php-fpm

As you can see PHP is installing version 5.5.20-2.el6.remi from the remi-php55 repo! Once you have hit Y to confirm the install restart apache and magical unicorns you have a better version of PHP!

You can also change your mind in the future by going back into the remi.repo file and enable a different PHP version and then run yum update and if you have moved from 5.5 to 5.6 it will upgrade PHP for you. If you want to downgrade for any reason you will need to remove PHP (sudo yum remove php*) and then reinstall the PHP modules you want.

Configure the PHP Processor

We now have our PHP components installed, but we need to make a slight configuration change to make our setup more secure.

Open the main php-fpm configuration file with root privileges:

  • sudo vi /etc/php.ini

What we are looking for in this file is the parameter that sets cgi.fix_pathinfo. This will be commented out with a semi-colon (;) and set to “1″ by default.

This is an extremely insecure setting because it tells PHP to attempt to execute the closest file it can find if a PHP file does not match exactly. This basically would allow users to craft PHP requests in a way that would allow them to execute scripts that they shouldn’t be allowed to execute.

We will change both of these conditions by uncommenting the line and setting it to “0″ like this:

/etc/php.ini excerpt
cgi.fix_pathinfo=0

Save and close the file when you are finished.

Next, open the php-fpm configuration file http://www.conf:

  • sudo vi /etc/php-fpm.d/www.conf

Find the line that specifies the listen parameter, and change it so it looks like the following:

/etc/php-php.d/www.conf — 1 of 3
listen = /var/run/php-fpm/php-fpm.sock

Next, find the lines that set the listen.owner and listen.group and uncomment them. They should look like this:

/etc/php-php.d/www.conf — 2 of 3
listen.owner = nobody
listen.group = nobody

Lastly, find the lines that set the user and group and change their values from “apache" to “nginx":

/etc/php-php.d/www.conf — 3 of 3
user = nginx
group = nginx

Then save and quit.

Now, we just need to start our PHP processor by typing:

  • sudo systemctl start php-fpm

This will implement the change that we made.

Next, enable php-fpm to start on boot:

  • sudo systemctl enable php-fpm

Step Four — Configure Nginx to Process PHP Pages

Now, we have all of the required components installed. The only configuration change we still need to do is tell Nginx to use our PHP processor for dynamic content.

We do this on the server block level (server blocks are similar to Apache’s virtual hosts). Open the default Nginx server block configuration file by typing:

  • sudo vi /etc/nginx/conf.d/default.conf

Currently, with the comments removed, the Nginx default server block looks like this:

/etc/nginx/conf.d/default.conf — original
server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

We need to make some changes to this file for our site.

  • First, we need to add an index.php option as the first value of our index directive to allow PHP index files to be served when a directory is requested
  • We also need to modify the server_name directive to point to our server’s domain name or public IP address
  • The actual configuration file includes some commented out lines that define error processing routines. We will uncomment those to include that functionality
  • For the actual PHP processing, we will need to uncomment a portion of another section. We will also need to add a try_files directive to make sure Nginx doesn’t pass bad requests to our PHP processor

The changes that you need to make are in red in the text below. If you prefer, you may just copy and paste everything, then replace the value of server_name with the appropriate domain name or IP address:

/etc/nginx/conf.d/default.conf — updated
server {
    listen       80;
    server_name  server_domain_name_or_IP;

    # note that these lines are originally from the "location /" block
    root   /usr/share/nginx/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

When you’ve made the above changes, you can save and close the file.

Restart Nginx to make the necessary changes:

  • sudo systemctl restart nginx

Step Five — Test PHP Processing on your Web Server

In order to test that our system is configured properly for PHP, we can create a very basic PHP script.

We will call this script info.php. In order for Apache to find the file and serve it correctly, it must be saved to a very specific directory, which is called the “web root".

In CentOS 7, this directory is located at /usr/share/nginx/html/. We can create the file at that location by typing:

  • sudo vi /usr/share/nginx/html/info.php

This will open a blank file. We want to put the following text, which is valid PHP code, inside the file:

Test PHP Script
<?php phpinfo(); ?>

When you are finished, save and close the file.

Now we can test whether our web server can correctly display content generated by a PHP script. To try this out, we just have to visit this page in our web browser. You’ll need your server’s public IP address again.

The address you want to visit will be:

Open in a web browser:
http://your_server_IP_address/info.php

 

 

 

一些常見的SQL效能問題

標準

本文轉貼自 : http://blog.csdn.net/Avan_Lau/archive/2010/03/02/5338205.aspx

1.  對查詢進行優化,應儘量避免全表掃描,首先應考慮在 where 及 order by 涉及的列上建立索引。

2.  應儘量避免在 where 子句中對欄位進行 null 值判斷,否則將導致引擎放棄使用索引而進行全表掃描,如:
select id from t where num is null
可以在num上設置預設值0,確保表中num列沒有null值,然後這樣查詢:
select id from t where num=0

3.  應儘量避免在 where 子句中使用!=或<>操作符,否則將使引擎放棄使用索引而進行全表掃描。

4.  應儘量避免在 where 子句中使用 or 來連接條件,否則將導致引擎放棄使用索引而進行全表掃描,如:
select id from t where num=10 or num=20
可以這樣查詢:
select id from t where num=10
union all
select id from t where num=20

5.  in 和 not in 也要慎用,否則會導致全表掃描,如:
select id from t where num in(1,2,3)
對於連續的數值,能用 between 就不要用 in 了:
select id from t where num between 1 and 3

6.  下面的查詢也將導致全表掃描:
select id from t where name like ‘abc’
若要提高效率,可以考慮全文檢索。

7.  應儘量避免在 where 子句中對欄位進行運算式操作,這將導致引擎放棄使用索引而進行全表掃描。如:
select id from t where num/2=100
應改為:
select id from t where num=100*2

8.  應儘量避免在where子句中對欄位進行函數操作,這將導致引擎放棄使用索引而進行全表掃描。如:
select id from t where substring(name,1,3)=’abc’–name以abc開頭的id
select id from t where datediff(day,createdate,’2005-11-30′)=0–‘2005-11-30’生成的id應改為:
select id from t where name like ‘abc’
select id from t where createdate>=’2005-11-30′ and createdate<‘2005-12-1’

9.  不要在 where 子句中的“=”左邊進行函數、算術運算或其他運算式運算,否則系統將可能無法正確使用索引。

10.  在使用索引欄位作為條件時,如果該索引是複合索引,那麼必須使用到該索引中的第一個欄位作為條件時才能保證系統使用該索引,否則該索引將不會被使用,且應盡可能的讓欄位順序與索引順序一致。

11.  很多時候用 exists 代替 in 是一個好的選擇:
select num from a where num in(select num from b)
用下面的語句替換:
select num from a where exists(select 1 from b where num=a.num)

12.  並不是所有索引對查詢都有效,SQL是根據表中資料來進行查詢優化的,當索引列有大量資料重複時,SQL查詢可能不會去利用索引,如一表中有欄位sex,male、female幾乎各一半,那麼
即使在sex上建了索引也對查詢效率起不了作用。

13.  索引並不是越多越好,索引固然可以提高相應的 select 的效率,但同時也降低了 insert 及 update 的效率,因為 insert 或 update 時有可能會重建索引,所以怎樣建索引需要慎重考慮,
視具體情況而定。一個表的索引數最好不要超過6個,若太多則應考慮一些不常使用到的列上建的索引是否有 必要。

14.  應盡可能的避免更新 clustered 索引資料列,因為 clustered 索引資料列的順序就是表記錄的物理存儲順序,一旦該列值改變將導致整個表記錄的順序的調整,會耗費相當大的資源。若應
用系統需要頻繁更新 clustered 索引資料列,那麼需要考慮是否應將該索引建為 clustered 索引。

15.  儘量使用數字型欄位,若只含數值資訊的欄位儘量不要設計為字元型,這會降低查詢和連接的性能,並會增加存儲開銷。這是因為引擎在處理查詢和連接時會逐個比較字串中每一個字元,而
對於數字型而言只需要比較一次就夠了。

16.  盡可能的使用 varchar/nvarchar 代替 char/nchar ,因為首先變長欄位存儲空間小,可以節省存儲空間,其次對於查詢來說,在一個相對較小的欄位內搜索效率顯然要高些。

17.  盡量不要用 select * from t ,用具體的欄位列表代替“*”,不要返回用不到的任何欄位。

fcgid

標準

cd /etc/httpd/conf/sites-available;ls

How to install mod_fcgid on CentOS / RHEL

Now we will install mod_fcgid by using yum command.

Step 1: To install mod_fcgid on CentOS / RHEL , first we will install EPEL repo .

rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm OR yum install wget wget dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm rpm -ivh epel-release-6-8.noarch.rpm

OR

yum install wget
wget dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm -ivh epel-release-6-8.noarch.rpm

Step 2: Now install the mod_fcgid

yum install mod_fcgid

To confirm mod_fcgi package is installed

Run the command –

rpm -qa|grep mod_fcgid

Below given is the reference from our Server :
In below given output you can see, mod_fcgi version is 2.3.9-1

[root@Server ~]# rpm -qa|grep mod_fcgid mod_fcgid-2.3.9-1.el6.x86_64 [root@Server ~]#

[root@Server ~]# rpm -qa|grep mod_fcgid
mod_fcgid-2.3.9-1.el6.x86_64
[root@Server ~]#

How to change date, time, timezone on centos

標準

Display Date and Time in Linux

# date

and its output
Sat Mar 15 03:14:29 PDT 2014

Change Timezone in Linux

Fist, we are going to get a list of timezone that came with Linux.
# cd /usr/share/zoneinfo/ # ls

Africa Canada GB Indian Mexico ROK iso3166.tab America Chile GB-Eire Iran NZ Singapore posix Antarctica Cuba GMT Israel NZ-CHAT Turkey posixrules Arctic EET GMT+0 Jamaica Navajo UCT right Asia EST GMT-0 Japan PRC US zone.tab Atlantic EST5EDT GMT0 Kwajalein PST8PDT UTC Australia Egypt Greenwich Libya Pacific Universal Brazil Eire HST MET Poland W-SU CET Etc Hongkong MST Portugal WET CST6CDT Europe Iceland MST7MDT ROC Zulu
Africa Canada GB Indian Mexico ROK iso3166.tab
America Chile GB-Eire Iran NZ Singapore posix
Antarctica Cuba GMT Israel NZ-CHAT Turkey posixrules
Arctic EET GMT+0 Jamaica Navajo UCT right
Asia EST GMT-0 Japan PRC US zone.tab
Atlantic EST5EDT GMT0 Kwajalein PST8PDT UTC
Australia Egypt Greenwich Libya Pacific Universal
Brazil Eire HST MET Poland W-SU
CET Etc Hongkong MST Portugal WET
CST6CDT Europe Iceland MST7MDT ROC Zulu

There are countries, or continent with different timezone, for example when you list all file and directories in zoneinfo directory as I did above, there are timezone for countries with only one timezone, and directories for countries and continent with several timezones. For example my timezone is America Pacific, I will need to go to America directory to get my timezone.

# cd /usr/share/zoneinfo/America/
# ls

Adak Coral_Harbour Hermosillo Moncton Santarem Anchorage Cordoba Indiana Monterrey Santiago Anguilla Costa_Rica Indianapolis Montevideo Santo_Domingo Antigua Creston Inuvik Montreal Sao_Paulo Araguaina Cuiaba Iqaluit Montserrat Scoresbysund Argentina Curacao Jamaica Nassau Shiprock Aruba Danmarkshavn Jujuy New_York Sitka Asuncion Dawson Juneau Nipigon St_Barthelemy Atikokan Dawson_Creek Kentucky Nome St_Johns Atka Denver Knox_IN Noronha St_Kitts Bahia Detroit Kralendijk North_Dakota St_Lucia Bahia_Banderas Dominica La_Paz Ojinaga St_Thomas Barbados Edmonton Lima Panama St_Vincent Belem Eirunepe Los_Angeles Pangnirtung Swift_Current Belize El_Salvador Louisville Paramaribo Tegucigalpa Blanc-Sablon Ensenada Lower_Princes Phoenix Thule Boa_Vista Fort_Wayne Maceio Port-au-Prince Thunder_Bay Bogota Fortaleza Managua Port_of_Spain Tijuana Boise Glace_Bay Manaus Porto_Acre Toronto Buenos_Aires Godthab Marigot Porto_Velho Tortola Cambridge_Bay Goose_Bay Martinique Puerto_Rico Vancouver Campo_Grande Grand_Turk Matamoros Rainy_River Virgin Cancun Grenada Mazatlan Rankin_Inlet Whitehorse Caracas Guadeloupe Mendoza Recife Winnipeg Catamarca Guatemala Menominee Regina Yakutat Cayenne Guayaquil Merida Resolute Yellowknife Cayman Guyana Metlakatla Rio_Branco Chicago Halifax Mexico_City Rosario Chihuahua Havana Miquelon Santa_Isabel
Adak Coral_Harbour Hermosillo Moncton Santarem
Anchorage Cordoba Indiana Monterrey Santiago
Anguilla Costa_Rica Indianapolis Montevideo Santo_Domingo
Antigua Creston Inuvik Montreal Sao_Paulo
Araguaina Cuiaba Iqaluit Montserrat Scoresbysund
Argentina Curacao Jamaica Nassau Shiprock
Aruba Danmarkshavn Jujuy New_York Sitka
Asuncion Dawson Juneau Nipigon St_Barthelemy
Atikokan Dawson_Creek Kentucky Nome St_Johns
Atka Denver Knox_IN Noronha St_Kitts
Bahia Detroit Kralendijk North_Dakota St_Lucia
Bahia_Banderas Dominica La_Paz Ojinaga St_Thomas
Barbados Edmonton Lima Panama St_Vincent
Belem Eirunepe Los_Angeles Pangnirtung Swift_Current
Belize El_Salvador Louisville Paramaribo Tegucigalpa
Blanc-Sablon Ensenada Lower_Princes Phoenix Thule
Boa_Vista Fort_Wayne Maceio Port-au-Prince Thunder_Bay
Bogota Fortaleza Managua Port_of_Spain Tijuana
Boise Glace_Bay Manaus Porto_Acre Toronto
Buenos_Aires Godthab Marigot Porto_Velho Tortola
Cambridge_Bay Goose_Bay Martinique Puerto_Rico Vancouver
Campo_Grande Grand_Turk Matamoros Rainy_River Virgin
Cancun Grenada Mazatlan Rankin_Inlet Whitehorse
Caracas Guadeloupe Mendoza Recife Winnipeg
Catamarca Guatemala Menominee Regina Yakutat
Cayenne Guayaquil Merida Resolute Yellowknife
Cayman Guyana Metlakatla Rio_Branco
Chicago Halifax Mexico_City Rosario
Chihuahua Havana Miquelon Santa_Isabel

I will use Toronto for Pacific time, so I will need to copy/set Toronto file as my localtime on my server (this will work with any linux system I believe).

# cp /usr/share/zoneinfo/America/Toronto /etc/localtime

Change Date and Time in Linux

Now your Linux system should have the timezone that you want. If the date or time is not correct, you can set date and time with date command.

# date MMDDhhmmYYYY

MM: two digit month number
DD: two digit date
hh: two digit hour (24 hour system)
mm: two digit minute
YYYY: four digit of year

You can also sync your system time and date with ntp (Network Time Protocol) over a network or internet. Normally ntp should be installed to your Linux system as default, in case your system does not have ntp, you can easily install ntp package with a simple command.

To install ntp on Ubuntu, Linux or debian based distribitions
# apt-get update
# apt-get install ntp

To install ntp on Fedora, Centos or redhat based distribitions
# yum install ntp

After you set your timezone correcly, you should be able to synce your date and time with ntp command
# ntpdate pool.ntp.org

Note: if you have a VPS (virtual private server) and not a real dedicated server, you will properly won’t be able to adjust time on your VPS. When you are on VPS, you do not have the right to modify or update time and date, only the VPS owner or provider have the rights to adjust time and date on that VPS node.

You will properly get one of this error if you are on a VPS.

Can’t adjust the time of day: Operation not permitted
rdate: could not set system time: Operation not permitted
ntpdate: Can’t adjust the time of day: Operation not permitted

Centos SVN

標準

1. 系統

CentOS 4.x/RHEL 4
CentOS 5.1/RHEL 5

 

2. 參考

Subversion: http://subversion.tigris.org/
Version Control with Subversion: http://svnbook.red-bean.com/

 

3. 安裝

 

[root@lucifer ~]# yum install mod_dav_svn subversion

你首先要做的事情就是安裝我在上面所提及的兩個套件。如果你未安裝 Apache,這樣做亦會為你把它拉進來。

當你用 yum 安裝時,它會列出上面兩個套件以外的套件。其它東西亦會自動被安裝。視乎你已有的套件,你的經歷也許會不同。

 

4. 設定

 

4.1. Apache

在你進一步學習之先,你需要確定 Apache 已經被設置好。我假設你在做一個全新的安裝,因此如果你的 Apache 已經在運作中……請小心你所做的改動。我亦會解釋如何設置基本的密碼保護。然而,假若你想容許任何人存取版本庫,你可輕易地省略這部份。

你首先要做的事情就是開啟 /etc/httpd/conf/httpd.conf,而且最低限度更改 ServerName 這個指引。如果你需要協助或擁有更複雜的設定,請參詳 Apache 的文件。

 

[root@lucifer ~] vim /etc/httpd/conf/httpd.conf —— 作所需修改並存檔
[root@lucifer ~] service httpd start
[root@lucifer ~] chkconfig httpd on

在網絡上瀏覽你的機器,http://yourmachine ,看看你能否取得測試頁。做得到嗎?好了,讓我們進到更有趣的事情。

 

4.2. Subversion 給 Apache 的設定

下一步就是在 Apache 內設置某些設定,好讓 Subversion 與 Apache 能夠融洽相處。現在讓我們看看 Subversion 為你安裝的範例設定檔。

 

[root@lucifer ~] cd /etc/httpd/conf.d/
[root@lucifer ~] vim subversion.conf

# 有需要的話,請確定你移除這兩行的註釋
LoadModule dav_svn_module     modules/mod_dav_svn.so
LoadModule authz_svn_module   modules/mod_authz_svn.so

# 加入下列內容來支援基本驗證,並將 Apache 指向實際放置版本庫的地方。
&lt;Location /repos&gt;
        DAV svn
        SVNPath /var/www/svn/repos
        AuthType Basic
        AuthName "Subversion repos"
        AuthUserFile /etc/svn-auth-conf
        Require valid-user
&lt;/Location&gt;

上面的位置是 Apache 在 URL 列上使用的。舉個例說:http://yourmachine/repos 指向你所指定的 SVNPath。上面只是一個範例,因此請按你的喜好放置東西。請確定你在完成編輯後儲存檔案。

然後我們須要建立你在上一部所指定的密碼檔。開始時你要利用 -cm 這個選項。它會建立檔案並用 MD5 將密碼加密。如果你需要加用戶,請確定你只使用 -m 選項,而不包含初次建立時的 -c。

 

[root@lucifer ~] htpasswd -cm /etc/svn-auth-conf yourusername
New password:
Re-type new password:
Adding password for user yourusername
[root@lucifer ~] htpasswd -m /etc/svn-auth-conf anotherusername
New password:
Re-type new password:
Adding password for user anotherusername

 

4.3. 設定你的版本庫

你接著要做的事情就是建立你用來提交及取出檔案的版本庫。利用 svn 所包含的工具,這是很容易的。

 

[root@lucifer ~] cd /var/www/ —— 或者你在上面所指定的路徑
[root@lucifer ~] mkdir svn
[root@lucifer ~] cd svn
[root@lucifer ~] svnadmin create repos
[root@lucifer ~] chown -R apache.apache repos
[root@lucifer ~] service httpd restart

現在去測試你能否透過網頁瀏覽器存取你的版本庫:http://yourmahcine/repos 。你應該取得一個對話方塊詢問用戶名稱及密碼。若然是這樣,請輸入你的憑證,然後你應該看見一版 Revision 0:/ 的頁面。這樣的話,版本庫的設定便大工告成了。如果你須要多個版本庫,請參考上面連結內的文檔。這裡只示範如何設置一個版本庫及開始應用它。話說回來,讓我們就這樣做。

 

5. 運用 subversion

 

5.1. 配置你的版本庫

如果一切順列,你現在應該已準備好使用你所建立的版本庫。subversion 的 svn 工具是你將要用來與資料庫溝通的指令行客戶端。若要看這個工具的用法:

 

[root@lucifer ~] svn --help

你 最有機會使用的選項是:svn import、svn commit(ci)、svn checkout(co)。開始時你會用 import 將檔案匯入你的版本庫;你會用 checkout 取出它們作修改;你會用 commit 將改動提交到資料庫內。一旦你見過它們被應用數次,這便頗為簡單。

在我繼續之先,我希望解釋關於目錄架構的配置。幾乎所有文件都談及用某個配置方法建立你的目錄。他們會提及要確定你在根目錄下設有 branches、tags 及 trunk 目錄,當中 trunk 會包含你的所有檔案。就例如:

 

.
|-- project1
|   |-- branches
|   |-- tags
|   `-- trunk
`-- project2
    |-- branches
    |-- tags
    `-- trunk

參考書會較詳細解釋這樣做的原因,但我基本上不會採用這種配置……這是因為我不算是寫程式或維護「項目」。我主要用它來儲存相對簡單的設定檔及文件。請用適合你的方式來設置。

作為一個範例,我會建立一些示範用的目錄及放置一些檔案在其中。這是在 SVN 伺服器上做的。請照樣做。

 

[root@lucifer ~] cd /tmp
[root@lucifer ~] mkdir mytestproj
[root@lucifer ~] cd mytestproj
[root@lucifer ~] mkdir configurations options main
[root@lucifer ~] vim configurations/testconf1.cfg —— 放你喜歡的東西在這些檔案內。
[root@lucifer ~] vim options/testopts1.cfg
[root@lucifer ~] vim main/mainfile1.cfg

請記得你可以隨你的自己的意思進行配置。當你完成了你所須的首個配置後,讓我們向前進並將它匯入 subversion 內。

 

5.2. 匯入

 

[root@lucifer ~] svn import /tmp/mytestproj/ file:///var/www/svn/repos/mytestproj -m "Initial repository layout for mytestproj"
Adding         /tmp/mytestproj/main
Adding         /tmp/mytestproj/main/mainfile1.cfg
Adding         /tmp/mytestproj/configurations
Adding         /tmp/mytestproj/configurations/testconf1.cfg
Adding         /tmp/mytestproj/options
Adding         /tmp/mytestproj/options/testopts1.cfg

 

5.3. 取出

現在利用網頁瀏覽器取出檔案:http://yourmachine/repos 。你應該能夠取出你所匯入的東西。一旦你在本地的 SVN 伺服器上載了你最初的配置,你只需用先前所建立的戶口連線到 subversion 伺服器,便可以在另一台遠端的機器上使用它。讓我們測試一下。

 

[me@mylappy ~] cd /tmp
[me@mylappy ~] svn co http://yoursvnserver/repos/mytestproj
Authentication realm: &lt;http://yoursvnserver:80&gt; Subversion repos
Password for 'youruser':
A    mytestproj/main
A    mytestproj/main/mainfile1.cfg
A    mytestproj/configurations
A    mytestproj/configurations/testconf1.cfg
A    mytestproj/options
A    mytestproj/options/testopts1.cfg
Checked out revision 1.

 

5.4. 編輯及提交

正如你所見,你已經從 subversion 伺服器取出了第 1 版。現在你可以編輯一些東西,然後將改動提交給 subversion 伺服器。

 

[me@mylappy ~] cd mytestproj
[me@mylappy ~] vim configurations/testconf1.cfg —— 加入或刪除某些東西並存檔。
[me@mylappy ~] svn commit -m "Added a line to testconf1.cfg."
Sending        configurations/testconf1.cfg
Transmitting file data .
Committed revision 2.

這樣做的一個好處,就是你可以刪除剛才在你的機器上所取出的所有目錄。你取出它們的唯一目的,就是要編輯它們,然後將它們送返主機。請瀏覽你的伺服器來取出不同檔案。

 

5.5. 新增/刪除物件

這一切都很好,但你如何在現存的版本庫內加入更多檔案?很簡單,就是利用 add 這個引數。請你現在就取出你最新的版本,複製一個檔案到目錄內,加入這個檔案,然後提交你的改動。

 

[me@mylappy ~] svn co http://yoursvnserver/repos/mytestproj
A    mytestproj/main
A    mytestproj/main/mainfile1.cfg
A    mytestproj/configurations
A    mytestproj/configurations/testconf1.cfg
A    mytestproj/options
A    mytestproj/options/testopts1.cfg
Checked out revision 2.

[me@mylappy ~] cd mytestproj
[me@mylappy ~] cp /etc/yum.repos.d/CentOS-Base.repo configurations/
[me@mylappy ~] svn add configurations/CentOS-Base.repo
A         configurations/CentOS-Base.repo

[me@mylappy ~] svn commit -m "Added the CentOS Yum repo file."
Adding         configurations/CentOS-Base.repo
Transmitting file data .
Committed revision 3.

要刪除物件,你只須以 delete 代替 add。提交你的改動,然後便成了。就是這麼簡單。再次返回你的瀏覽器,你會發現版本號碼是 3。你應該可以點擊檔案並找出你所做的改動。

 

5.6. 還原

這些都非常好,但我如何還原到舊的版本……這不是 subversion 的用意嗎?對,這很容易。好果你不清楚你現有的版本……看看 log 這個指令。這解釋了你為何每次提交時要放入信息。它們要簡潔,但含足夠資料來提醒你或許忘掉了的事情。

 

[me@mylappy ~] svn log http://yoursvnserver/repos —— 這針對整個版本庫
[me@mylappy ~] svn log http://yoursvnserver/repos/mytestproj —— 這針對個別項目

你會取得一個完整的版本列表連同註解,正如我上面所講。這樣你便能選擇你現在想取回哪個版本。

 

[me@mylappy ~] svn co -r 1 http://yoursvnserver/repos/mytestproj

這個指令會取回第 1 個版本。

 

6. 存取控制清單

一般來說,你不會想將所有版本庫的存取權給予所有用戶。你可以利用 ACL 來限制每位用戶存取版本庫的權限。ACL 可以透過 AuthzSVNAccessFile 這個檔案選項來啟用,它以一個檔名作為參數。例如:

 

AuthzSVNAccessFile /etc/svn-acl-conf

你可以在相關的 Location 部份加入它:

 

<Location /repos>
        DAV svn
        SVNParentPath /var/www/svn/repos
        AuthzSVNAccessFile /etc/svn-acl-conf
        AuthType Basic
        AuthName "Subversion repos"
        AuthUserFile /etc/svn-auth-conf
        Require valid-user
</Location>

然後你可以建立 /etc/svn-acl-config。這個檔案內藏有這個格式的分段:

 

[版本庫名稱:版本庫路徑]
用戶 = 存取權

當中存取權可以是 r(唯讀)、rw(讀寫)、或空白(禁止存取)。預設的 ACL 是禁止用戶存取版本載。假設你有一個名叫 framework 的版本庫,而你想給 john 唯讀的權限,及 joe 讀寫的權限。你可以加入下面這個分段:

 

[framework:/]
john =  r
joe = rw

你亦可以在名叫 groups 的分段內建立群組,然後在存取控制清單內將 @ 符號放在群組前面。例如:

 

[groups]
staff = joe, george

[framework:/]
john =  r
@staff = rw

如果你想令所有用戶能閱讀每個版本庫,你可以為每個版本庫的根目錄加入以下一個分段:

 

[/]
* = r

 

7. 後記

這只是 subversion 能賦予你的能力的很小部份。這份入門指南能助你一把,初步示範如何使用它,並理解它的運作。你可以用 subversion 的工具做各樣事情,所以請記得查閱文檔來學習關於可以協助你的不同選項。另外請留意為你的需要安裝 Apache 也許是小題大做。要完全使用 subversion 的工具,你可以在本地的機器上指定 file:///path/to/repo ,而不必像我的 Apache 範例般使用 http://yoursvnserver/repos/whatever 。據聞很多人都在他們的主機上用它來管理大型項目及設定檔,免得自己發瘋。祝你好運。

 

8. 進深閱讀

Version Control with Subversion

Centos install tomcat7

標準

一、      安裝JDK

1、         卸載伺服器自帶的jdk

# java –version                         –查看java版本

# rpm –qa | grep jdk                –查看伺服器自帶的jdk

# yum -y remove xxxx                  –卸載伺服器自帶jdk(xxx為顯示的版本資訊)

# rpm –qa | grep gcj

# yum –y remove xxxx

#sudo find /usr/ -name *jdk  –check java home

2、         安裝JDK

# cd /usr/local/src                    –進入套裝軟體的存放路徑

# mv /usr/local/src/jdk-6u35-linux-x64.bin /usr/local/jdk-6u35-linux-x64.bin  –把套裝軟體剪切到安裝目錄

# cd /usr/local                         –進入安裝目錄

# chmod 755 jdk-6u35-linux-x64.bin      –給套裝軟體添加執行許可權

# ./ jdk-6u35-linux-x64.bin        –執行安裝

3、         配置java環境變數

# cp /etc/profile /etc/profile.bak            –備份系統設定檔

# vi /etc/profile                                      –編輯設定檔(在最後追加如下內容)

export JAVA_HOME=/usr/local/jdk1.6.0_35

export JRE_HOME=/usr/local/jdk1.6.0_35/jre/

export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar

export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin

# source /etc/profile                                          –重新載入設定檔

# java –version                                                   –查看安裝的jdk版本資訊

二、      安裝Tomcat

1、安裝Tomcat

# cd /usr/local/src                                      –進入安裝包存放目錄

# tar zxvf apache-tomcat-7.0.35.tar.gz        –解壓

# mv apache-tomcat-7.0.35 /usr/local/apache-tomcat-7.0.35          –資料夾剪切到安裝目錄

# /usr/local/apache-tomcat-7.0.35/bin/startup.sh         –啟動

# /usr/local/apache-tomcat-7.0.35/bin/shutdown.sh     –關閉

2、添加系統服務,自動開機啟動

# cd /etc/rc.d/init.d                               –進入系統服務資料夾

# vi tomcat                                           –創建tomcat系統服務設定檔(添加如下內容)

#

# chkconfig: 345 80 15

# description: Tomcat is a Servlet+JSP Engine.

# Enter the jdk installation directory

jdk_File=/usr/local/jdk1.6.0_35

# Enter the tomcat installation directory

tomcat_File=/usr/local/apache-tomcat-7.0.35

export JAVA_HOME=$jdk_File

# Source function library.

. /etc/rc.d/init.d/functions

start(){

if [ -z $(/sbin/pidof java) ]; then

echo “Starting tomcat"

$tomcat_File/bin/startup.sh

touch /var/lock/subsys/tomcat

else

echo “tomcat allready running"

fi

}

stop(){

if [ ! -z $(/sbin/pidof java) ]; then

echo “Shutting down tomcat"

$tomcat_File/bin/shutdown.sh

until [ -z $(/sbin/pidof java) ]; do :; done

rm -f /var/lock/subsys/tomcat

else

echo “tomcat not running"

fi

}

case “$1″ in

start)

start

;;

stop)

stop

;;

restart)

stop

start

;;

status)

$tomcat_File/bin/catalina.sh version

;;

*)

echo “Usage: $0 {start|stop|restart|status}"

esac

exit 0

:wq                                     –退出

# chmod 755 /etc/rc.d/init.d/tomcat                             –添加執行許可權

#chkconfig –add tomcat                                                   –添加系統服務

#chkconfig tomcat on                                                       –添加開機啟動

# service tomcat stop                                                  –停止

# service tomcat start                                                       –啟動

# service tomcat restart                                                –重啟

# service tomcat status                                                –查看狀態

開啟防火牆相應埠

開放8080埠的解決步驟如下:

1、修改/etc/sysconfig/iptables檔

# vi /etc/sysconfig/iptables

在22埠下添加如下一行:

-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 8080 -j ACCEPT

2、重啟防火牆,這裡有兩種方式重啟防火牆

a) 重啟後生效

開啟: chkconfig iptables on

關閉: chkconfig iptables off

b) 即時生效,重啟後失效

開啟: service iptables start

關閉: service iptables stop

 

Another config

 

#!/bin/sh
#
# Tomcat Server
#
# chkconfig: 345 96 30
# description: Java servlet container

JAVA_HOME=/opt/jdk

PATH=${JAVA_HOME}/bin:${PATH}

TOMCAT_START=/opt/tomcat/bin/startup.sh

TOMCAT_STOP=/opt/tomcat/bin/shutdown.sh

export JAVA_HOME PATH

start()
{
        if [ -x ${TOMCAT_START} ]; then
                echo "Starting tomcat server..."
                ${TOMCAT_START} &
        else
                echo "Cannot start tomcat server"
        fi
}

stop()
{
        if [ -x ${TOMCAT_STOP} ]; then
                echo "Stopping tomcat server..."
                ${TOMCAT_STOP} &
        else
                echo "Cannot stop tomcat server"
        fi
}

restart()
{
        stop
        sleep 10
        start
}

status()
{
        echo "No status available for tomcat server"
}

case "$1" in
'start')
        start
        ;;
'stop')
        stop
        ;;
'restart')
        restart
        ;;
'status')
        status
        ;;
*)
        echo "Please supply an argument [start|stop|restart]"
esac